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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值