欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-01-02 21:25:04
...

版本: 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();
}

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

相关标签: qt5 connect

上一篇:

下一篇: