Reputation: 13254
I'm creating a small application with QtCreator. It compiles, but when executing from QtCreator I'm getting "The program has inexpectedly finished" error.
If I try to execute the binary from console, I get a Segementation fault (core dumped).
Since this is the first time I start a Qt code on my own, I guess I'm missing something. Please check the following code:
Header mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow;
class QLabel;
class QLineEdit;
class QPushButton;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void createGUI();
private:
QLineEdit *mysqlUserLineEdit;
QLineEdit *mysqlPasswordLineEdit;
QLineEdit *albatrossIPLineEdit;
QLineEdit *albatrossPortLineEdit;
QPushButton *exitButton;
QPushButton *startButton;
};
#endif // MAINWINDOW_H
Source mainwindow.cpp:
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
createGUI();
//connect(...)
//connect(...)
setWindowTitle(tr("Albatross MySQL simulator"));
}
MainWindow::~MainWindow()
{
}
void MainWindow::createGUI()
{
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *settingsLayout = new QHBoxLayout;
QVBoxLayout *mysqlSettingsLayout = new QVBoxLayout;
QHBoxLayout *mysqlUserLayout = new QHBoxLayout;
mysqlUserLineEdit = new QLineEdit();
QLabel *mysqlUserLabel = new QLabel(tr("&User:"));
mysqlUserLabel->setBuddy(mysqlUserLineEdit);
mysqlUserLayout->addWidget(mysqlUserLabel);
mysqlUserLayout->addWidget(mysqlUserLineEdit);
QHBoxLayout *mysqlPasswordLayout = new QHBoxLayout;
mysqlPasswordLineEdit = new QLineEdit();
QLabel *mysqlPasswordLabel = new QLabel(tr("&Password:"));
mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);
mysqlSettingsLayout->addLayout(mysqlUserLayout);
mysqlSettingsLayout->addLayout(mysqlPasswordLayout);
QVBoxLayout *networkSettingsLayout = new QVBoxLayout;
QHBoxLayout *albatrossIPLayout = new QHBoxLayout;
albatrossIPLineEdit = new QLineEdit();
QLabel *albatrossIPLabel = new QLabel(tr("&IP:"));
albatrossIPLabel->setBuddy(albatrossIPLineEdit);
albatrossIPLayout->addWidget(albatrossIPLabel);
albatrossIPLayout->addWidget(albatrossIPLineEdit);
QHBoxLayout *albatrossPortLayout = new QHBoxLayout;
albatrossPortLineEdit = new QLineEdit();
QLabel *albatrossPortLabel = new QLabel(tr("P&ort:"));
albatrossPortLabel->setBuddy(albatrossPortLineEdit);
albatrossPortLayout->addWidget(albatrossPortLabel);
albatrossPortLayout->addWidget(albatrossPortLineEdit);
networkSettingsLayout->addLayout(albatrossIPLayout);
networkSettingsLayout->addLayout(albatrossPortLayout);
settingsLayout->addLayout(mysqlSettingsLayout);
settingsLayout->addLayout(networkSettingsLayout);
QHBoxLayout *buttonsLayout = new QHBoxLayout;
exitButton = new QPushButton(tr("&Exit"));
buttonsLayout->addWidget(exitButton);
startButton = new QPushButton(tr("&Start"));
startButton->setDefault(true);
buttonsLayout->addWidget(startButton);
mainLayout->addLayout(settingsLayout);
mainLayout->addLayout(buttonsLayout);
centralWidget()->setLayout(mainLayout);
}
And finally main.cpp,, which was generated automatically with QtCreator:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
EDIT: Ok, the problem is when using mainLayout to attach it to the mainWindow, and the last line of mainwindow.cpp. That's where it's throwing a segmentation fault. What should I set as central widget? Or is there any other way of attaching the layout to the mainwindow widget?
Upvotes: 0
Views: 356
Reputation: 22890
In general this behavior in creator is due to a SEGFAULT or a missing library.
Your code
mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);
is the cause. You don't initialize mysqlPasswordLineEdit
which causes a SEGFAULT
Upvotes: 1