c++ - Qt 程序不工作 : The program has inexpectedly finished

标签 c++ qt

我正在使用 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

来源主窗口.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 的最后一行。这就是它抛出段错误的地方。我应该将什么设置为中央小部件?或者有没有其他方法可以将布局附加到主窗口小部件?

最佳答案

一般来说,creator 中的这种行为是由于 SEGFAULT 或缺少库造成的。

你的代码

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

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

关于c++ - Qt 程序不工作 : The program has inexpectedly finished,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618563/

相关文章:

c++ - 使用 JsonCpp 创建 Json 消息

c++ - 删除指针 vector 中的指针指向的对象

c++ - 通过 C++ 接口(interface)访问 PostgreSQL(链接器错误)

c++ - Qt/C++ 将通用对象序列化为 QSettings

python - 将多个 QSlider 连接到一个插槽

c++ - 结构体声明中的冒号是什么意思,例如 :1, :7, :16, 或 :32?

c++ - EnumResTypeProc 空白 LPTSTR 参数

javascript - Qt QJSEngine导入js文件

c++ - Qt QTextBrowser 如何捕获文本并更改其光标

c++ - Qmake 在创建 Visual Studio 项目时不替换环境变量?