我正在使用QtCreator创建一个小应用程序。它编译,但是当从QtCreator执行时,我得到“程序已经不可思议地完成”错误。
如果我尝试从控制台执行二进制文件,则会出现Segementation故障(核心转储)。
由于这是我第一次独立开始使用Qt代码,我想我错过了一些东西。请检查以下代码:
标题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
来源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);
}
最后是main.cpp ,,它是用QtCreator自动生成的:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
编辑:好的,问题是当使用mainLayout将它附加到mainWindow和mainwindow.cpp的最后一行时。这就是它抛出分段错误的地方。我应该将其设置为中央窗口小部件?或者是否有其他方法将布局附加到主窗口小部件?
答案 0 :(得分:1)
通常,创建者中的这种行为是由于SEGFAULT或缺少库。
您的代码
mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);
是原因。您没有初始化导致SEGFAULT
的mysqlPasswordLineEdit