【问题标题】:Qt program doesn't work: The program has inexpectedly finishedQt程序不工作:程序意外完成
【发布时间】:2012-09-27 18:45:55
【问题描述】:

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

【问题讨论】:

  • 有趣的代码格式...
  • @SingerOfTheFall 为什么会这样?请发表评论,如果我做错了,我很高兴知道
  • 1.在第一行设置断点; 2. 以Debug模式运行程序; 3. 逐行遍历你的程序,找出在哪一行。遇到故障,我打赌://连接,据我所知,您将小部件连接到所需的插槽
  • @spin_eight 我当前的代码有连接注释,我没有评论它们只是为了显示在这里。所以问题一定出在其他地方。无论如何,我会听从你的建议
  • @Roman,我没说这是错的,我说这很有趣;)我只是没见过像你之前在createGUI 中使用的缩进。一旦你明白了,它确实更容易阅读。

标签: c++ qt


【解决方案1】:

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

你的代码

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

是原因。你没有初始化 mysqlPasswordLineEdit 这会导致 SEGFAULT

【讨论】:

  • 好的,我没有初始化任何 QLineEdits。但还是不行。我还更改了 centralWidget()->setLayout(mainLayout) 的 setLayout(mainLayout)。查看更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多