所以这是 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!” window 。我已经在“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 编写文本。所以要写“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 的中央小部件通常通常是 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()
{
}
正如您在连接中看到的那样。在第一个参数中,我们将发送信号的对象放在这里按钮。在第二个参数中,我们将信号链接到此处,它是 clicked() 来执行此操作,我们编写 SIGNAL(clicked())。第三,将接收信号的对象,这里是要关闭的窗口。在第四个参数中,接收到信号时启动的功能。我们写这个 SLOT(close())。
为什么是 SIGNAL 和 SLOT?因为在 Qt 中我们使用 signal: 在 .hh 中创建一个信号并创建一个槽 et 使用(公共(public)或 protected 或私有(private))槽: 示例:
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()
{
}
关于c++ - Qt 关闭上一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28373554/