2012-09-27 60 views
0

我正在用QtCreator創建一個小應用程序。它編譯,但從QtCreator執行時,我得到「該程序已經無法完成」的錯誤。Qt程序不起作用:該程序已經無法完成

如果我試圖執行從控制檯的二進制文件,我得到一個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.cpp的最後一行。這就是拋出分段錯誤的地方。我應該如何設置中央部件?或者是否有任何其他方式將佈局附加到主窗口小部件?

+2

有趣的代碼格式... – SingerOfTheFall

+0

@SingerOfTheFall這是爲什麼?請評論,我很高興得知如果我做錯了 –

+0

1.在第一行設置斷點; 2.以調試模式運行程序; 3.一行一行地遍歷你的程序,並找出哪一行seg。遇到了錯誤,我敢打賭://連接,據我所知,將小部件連接到需要的插槽 –

回答

1

一般而言,創作者中的這種行爲是由於SEGFAULT或缺少的庫引起的。

您的代碼

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

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

+0

好吧,我不初始化任何QLineEdits的變得有點混亂。但仍然不起作用。我也改變了setWay(mainLayout)爲centralWidget() - > setLayout(mainLayout)。查看更改 –