qt5 槽的使用的简单例子(connect函数)

来源:互联网 发布:mac解压缩软件zip 编辑:程序博客网 时间:2024/06/14 21:00

版本: qt creator 4.0.3 和 qt 5.6.2

简单实例: (并未使用.ui文件, 只用了代码)

test.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = test1TEMPLATE = appSOURCES += main.cpp\        test.cppHEADERS  += test.h

test.h

#ifndef FINDDIALOG_H#define FINDDIALOG_H#include <QDialog>class QPushButton;class QDialogButtonBox;class test : public QDialog{    Q_OBJECTpublic:    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();}

可以直接运行, 实现功能是: 点击关闭按钮关闭界面