2017-04-19 65 views
2

我剛開始學習QT,我想完成的是在按鈕上點擊彈出消息。 這裏是我的文件看起來像:QT消息框不彈出按鈕點擊

的main.cpp

#include "mainwindow.h" 
#include <QApplication> 
#include <QDialog> 
#include <QLabel> 
#include <QMessageBox> 
#include <QPushButton> 
#include <QHBoxLayout> 
#include <QVBoxLayout> 
#include <QWidget> 


int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    MainWindow *mainWindow = new MainWindow(); 


    QLabel *text = new QLabel("Some text"); 
    QPushButton *btn = new QPushButton("Click"); 

    QHBoxLayout *layout = new QHBoxLayout; 

    layout->addWidget(btn); 
    layout->addWidget(text); 

    QObject::connect(btn, SIGNAL(clicked()), &app, SLOT(popup())); 

    mainWindow->setLayout(layout); 
    mainWindow->show(); 

    return app.exec(); 
} 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::popUp() 
{ 
    QMessageBox::information(this, "New Box", "Message"); 
} 

mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QMessageBox> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 


private slots: 
    void popUp(); 

private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

你能解釋一下我做錯了什麼,或者在我的代碼中缺少某些東西。

回答

2

我推薦圖形部分在類MainWindow裏面實現它自私的成員ui處理設計。

#include <QMessageBox> 
#include <QLabel> 
#include <QLayout> 
#include <QPushButton> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    QLabel *text = new QLabel("Some text"); 
    QPushButton *btn = new QPushButton("Click"); 

    QHBoxLayout *layout = new QHBoxLayout; 

    layout->addWidget(btn); 
    layout->addWidget(text); 

    ui->centralWidget->setLayout(layout); 

    connect(btn, &QPushButton::clicked, this, &MainWindow::popUp); 
} 

不要對main.cpp任何更改,代碼應該是這樣的:使用[新信號插槽語法]時(HTTPS

#include "mainwindow.h" 
#include <QApplication> 



int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 

    w.show(); 

    return a.exec(); 
} 
+0

什麼是中央控件?我得到這個小部件的錯誤:mainwindow.cpp:19:error:invalid use of incomplete type'class QLayout' ui-> centralWidget-> layout() - > addWidget(btn); ^ – Viktor

+0

測試返回請更新我的答案。 – eyllanesc

+0

@感謝你的工作!你能解釋爲什麼在這一行連接(btn,&QPushButton :: clicked,this,&MainWindow :: popUp);我們必須使用&QPushButton :: clicked和&MainWindow :: popUp,而不是connect(btn,SIGNAL(clicked()),this,SLOT(popUp())); – Viktor

0

連接錯誤。您應該將clicked()信號連接到mainWindow的插槽popUp(),因爲它是MainWindow類的成員。而且不要忘了關於C的大小寫++(彈出截至,不彈出):

QObject::connect(btn, SIGNAL(clicked()), mainWindow, SLOT(popUp())); 
+0

區分大小寫問題將不會是一個問題: //wiki.qt.io/New_Signal_Slot_Syntax),因爲編譯器錯誤會被拋出。 – MrEricSir

+0

@Vladimir Bershov我做了修改,但仍然不能正常工作 – Viktor

0

你連接到外部主窗口的私人插槽。 應用輸出可能會顯示這樣的警告:

QObject::connect: No such slot QApplication::popup()

通常你會想要做的就是把裏面的主窗口使用的東西,MainWindow類裏面是什麼。

例如:在構造函數中創建佈局。
創建對象時可以使用父參數,以便在完成mainwindow時將其銷燬。

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    QLabel *text = new QLabel("Some text", this); 
    QPushButton *btn = new QPushButton("Click", this); 

    QHBoxLayout *layout = new QHBoxLayout; 

    QObject::connect(btn, SIGNAL(clicked()), this, SLOT(popUp())); 

    ui->centralWidget->setLayout(layout); 
    this->show(); 
} 

您必須使用ui->centralWidget,因爲這就是mainwindow的工作原理。應用程序輸出也應該給你一個警告。 像這樣:

QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout

0

好吧,你必須在文件mainwindow.cpp工作。您已經創建插槽,所以將它連接在主窗口的構造函數中添加此代碼(如果你不知道女巫是兆瓦的構造是在那裏寫ui->setupUi(this);功能)

connect(ui->btn, SIGNAL(clicked()), SLOT(popUp())); 
現在

在插槽MainWindow::popUp();放驗證碼:

QMessageBox msgBox; 
msgBox.setText("text to write"); 
msgBox.exec(); 

記住,包括在mainwindow.cppQMessageBox,你不應該寫在main.cpp中的代碼,它必須是這樣的:

#include "mainwindow.h" 
#include <QApplication> 

    int main(int argc, char *argv[]) 
    { 
     QApplication a(argc, argv); 
     MainWindow w; 
     w.show(); 

     return a.exec(); 
    } 
+0

給我這個錯誤:mainwindow.cpp:11:error:'class Ui :: MainWindow'沒有成員,名爲'btn' connect(ui-> btn,SIGNAL clicked()),SLOT(popUp())); ^ – Viktor

+1

您是否在圖形視圖中創建了名爲btn的PushButton圖形對象?如果不是,你必須畫它 – EmLe49

+0

你可以創建它打開文件mainwindow.ui(在表單中),在這裏你可以繪製你的對象 – EmLe49