0

我正在使用 QtCreator 创建一个小型应用程序。它可以编译,但是从 QtCreator 执行时,我收到“程序意外完成”错误。

如果我尝试从控制台执行二进制文件,则会出现分段错误(核心转储)。

由于这是我第一次自己编写 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.cpp 的最后一行。这就是它引发分段错误的地方。我应该将什么设置为中心小部件?或者有没有其他方法可以将布局附加到主窗口小部件?

4

1 回答 1

1

一般来说,创建者中的这种行为是由于 SEGFAULT 或缺少库。

你的代码

            mysqlPasswordLabel->setBuddy(mysqlPasswordLineEdit);
            mysqlPasswordLayout->addWidget(mysqlPasswordLabel);
            mysqlPasswordLayout->addWidget(mysqlPasswordLineEdit);

是原因。您没有初始化mysqlPasswordLineEdit导致 SEGFAULT

于 2012-09-27T09:53:28.580 回答