2015-02-06 34 views
0

所以这是main.cpp中的代码:“Hello World”的Qt的闭幕前一个窗口

#include <QApplication> 
#include <QtGui> 
#include <mainwidget.h> 

int main(int argc, char **argv) 
{ 
QPushButton button ("Hello world!"); 
button.show(); 

mainWidget widget; 
widget.show(); 

return app.exec(); 
} 

我想从“小部件”按钮来关闭窗口。我已经在“widget”中添加了该按钮,并在mainwidget.h(它也显示了一个消息框)中为它创建了一个函数并将它们连接起来。我只想知道该按钮如何关闭Hello World窗口。我想我必须在mainwidget.h中的函数中添加一些东西,但我不知道它是什么。教师说我们应该使用QWidget的close()函数。

回答

2

我会回答你的问题,但在我将解释你是如何基本的Qt应用程序看起来像。

在Qt中,一个基本的主要是这样的:

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

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

    return a.exec(); 
} 

在这里,你可以看到一个QApplication的创建则主窗口。 那么MainWindow类是什么?

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

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

#endif // MAINWINDOW_H 

这是MainWindow.h。正如你所看到的MainWindow继承到QMainWindow类。这是创建图形用户界面的主窗口类。 Q_OBJECT定义为Qmake。的确,qmake我会为这个类创建一个moc_mainwindow.cpp来管理Qt信号。 现在,如果你创建一个空的构造和析构像你得到一个空的窗口:“世界,你好”你希望写后

#include "mainwindow.h" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent) 
{ 
} 

MainWindow::~MainWindow() 
{ 
} 

然后在Qt窗口中编写文本,你可以使用QLabel。所以要写“Hello World!”你得到:

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

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent) 
{ 
    QWidget *widget = new QLabel("Hello world !", this); 
} 

MainWindow::~MainWindow() 
{ 
} 

然后在创建按钮后,你会使用QPushButton类。 等你拿:

#include "mainwindow.h" 
#include <QLabel> 
#include <QPushButton> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent) 
{ 
    QWidget *widget = new QLabel("Hello world !", this); 
    setCentralWidget(widget); 
    QPushButton *button = new QPushButton("close", this); 
} 

MainWindow::~MainWindow() 
{ 
} 

(我选择将QLabel设置到中央的Widget做不了后面的按钮,但之后并实时Qt应用程序QMainWindow中的核心部件主要是usualy一个QWidget我的标签我会解释你为什么后) 现在你有一个按钮。但是当你点击它时什么都没有追加。 为什么?因为没有链接按钮和窗口。要在Qt中链接它,我们使用connect函数。 [http://qt-project.org/doc/qt-4.8/qobject.html][1]

所以用连接关闭窗口,当你点击你的按钮:

#include "mainwindow.h" 
#include <QLabel> 
#include <QPushButton> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent) 
{ 
    QWidget *widget = new QLabel("Hello world !", this); 
    setCentralWidget(widget); 
    QPushButton *button = new QPushButton("close", this); 
    connect(button, SIGNAL(clicked()), this, SLOT(close())); 
} 

MainWindow::~MainWindow() 
{ 
} 

正如你可以看到连接。在第一个参数中,我们把发送信号的对象放在这个按钮上。在第二个参数中,我们把信号链接到这里它被点击()来做到这一点,我们写信号(点击())。第三,将接收信号的对象,在这里窗口关闭。第四个参数是收到信号时启动的功能。我们写这个SLOT(close())。

为什么SIGNAL和SLOT?因为Qt来创建一个信号,我们使用的信号:在.hh和要创建插槽等的使用(公共或受保护或私有)插槽: 例如:

Class Object 
{ 
Q_OBJECT 
... 
signals: 
    void aSignal(); 
public slots: 
    void aSlot(); 
... 
}; 

注:信号和槽必须有相同的回报和参数。

后整理的对象,所以你有,你会使用在QWidget的标签在centralWidget:

#include "mainwindow.h" 
#include <QLabel> 
#include <QPushButton> 
#include <QVBoxLayout> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent) 
{ 
    QWidget* centralWid = new QWidget(this); 
    setCentralWidget(centralWid); 

    QVBoxLayout *layout = new QVBoxLayout(centralWid); 
    QWidget *widget = new QLabel("Hello world !", this); 
    QPushButton *button = new QPushButton("close", this); 
    layout->addWidget(widget); 
    layout->addWidget(button); 
    centralWid->setLayout(layout); 

    connect(button, SIGNAL(clicked()), this, SLOT(close())); 
} 

MainWindow::~MainWindow() 
{ 
} 
+0

这是真正有用的!谢啦! – user3573594 2015-02-24 00:27:23