簡體   English   中英

Qt程序不起作用:程序意外完成

[英]Qt program doesn't work: The program has inexpectedly finished

我正在使用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和mainwindow.cpp的最后一行時。 那就是拋出分段錯誤的地方。 我應該將什么設置為中央小部件? 還是有其他將布局附加到主窗口小部件的方法?

通常,創建者中的此行為歸因於SEGFAULT或缺少庫。

您的密碼

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

是原因。 你不初始化mysqlPasswordLineEdit導致段錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM