[英]Qt Closing Previous Window
因此,這是main.cpp的代碼:
#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();
}
我想要一個來自“小部件”的按鈕來關閉“ Hello world!” 窗口。 我已經在“ widget”中添加了該按鈕,並在mainwidget.h(還顯示了一個消息框)中為其創建了一個函數,並將它們連接起來。 我只想知道相同的按鈕如何關閉Hello World窗口。 我想我必須在mainwidget.h中的函數中添加一些內容,但我不知道它是什么。 講師說我們應該使用QWidget close()函數。
我將回答您的問題,但在向您解釋基本Qt應用程序的外觀之前,我將作答。
在Qt中,基本的main是這樣的:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
如您所見,這里創建了QApplication,然后創建了MainWindow。 那么什么是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信號。 現在,如果您創建一個Empty構造函數和析構函數,則將獲得一個空窗口:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
}
MainWindow::~MainWindow()
{
}
然后在您希望寫“ Hello world!”之后 在窗口中,以便在Qt中編寫文本可以使用QLabel。 所以寫“你好世界!” 你得到:
#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的中央Widget通常是一個QWidget,稍后我將向您解釋原因) 。 但是,當您單擊它時,沒有任何追加。 為什么呢 因為沒有東西鏈接按鈕和窗口。 要在Qt中鏈接它,我們使用connect函數。 [ http://qt-project.org/doc/qt-4.8/qobject.html][1]
因此,在單擊按鈕時使用connect關閉窗口,您將獲得:
#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()
{
}
如您在連接中看到的。 在第一個參數中,我們將要發送信號的對象放在此處。 在第二個參數中,我們將信號鏈接到此處的clicked()中,以編寫SIGNAL(clicked())。 第三,將接收信號的對象,此處關閉窗口。 第四個參數是接收到信號時啟動的功能。 我們寫這個SLOT(close())。
為什么選擇信號和插槽? 因為在Qt中創建信號,我們在.hh中使用signal:並創建一個插槽並使用(公共或受保護或專用)插槽:例如:
Class Object
{
Q_OBJECT
...
signals:
void aSignal();
public slots:
void aSlot();
...
};
注意:信號和插槽必須具有相同的返回和參數。
組織完對象后,將在centralWidget的QWidget中使用標簽,以便您:
#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()
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.