版本: qt creator 4.0.3 和 qt 5.6.2
简单实例: (并未使用.ui文件, 只用了代码)
test.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test1
TEMPLATE = app
SOURCES += main.cpp\
test.cpp
HEADERS += test.h
test.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
class QPushButton;
class QDialogButtonBox;
class test : public QDialog
{
Q_OBJECT
public:
test(QWidget *parent = 0);
private slots:
void end();
private:
QPushButton *findButton;
QDialogButtonBox *buttonBox;
};
#endif
test.cpp
#include <QtWidgets>
#include "test.h"
test::test(QWidget *parent)
: QDialog(parent)
{
findButton = new QPushButton(tr("关闭"));
buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
connect(findButton, SIGNAL(clicked()), this, SLOT(end()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(buttonBox, 0, 2, 2, 1);
setLayout(mainLayout);
}
void test::end()
{
this->close();
}
可以直接运行, 实现功能是: 点击关闭按钮关闭界面