您当前的位置: 首页 >  qt

命运之手

暂无认证

  • 2浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【零基础学QT】【028】Qt中的信号与槽机制

命运之手 发布时间:2019-10-30 14:55:45 ,浏览量:2

? 信号与槽机制 信号与槽机制(Signal & Slot)是QT独创的事件响应与消息处理机制 它允许一个对象在触发事件时,发出信号,其它对象接收信号,然后调用相应的槽函数进行响应处理 信号与槽是一种形象的比喻,一个对象发射出信号,其它对象通过槽来接收信号,然后处理信号

? 信号槽机制的使用方法


	#include 
	#include 
	
	int main(int argc, char *argv[]) {
	    QApplication app(argc, argv);
	    QPushButton *button = new QPushButton("Exit");
	    button->show();
	
	    // 一个最简单的信号槽案例:Button点击时APP退出
	    // connect:连接信号与槽
	    // sender:事件发送者
	    // SIGNAL:发射事件
	    // receiver:事件接收者
	    // SLOT:响应事件
	    // type:槽函数使用的同步方式
	    QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()), Qt::ConnectionType::AutoConnection);
	
	    return app.exec();
	}

? 信号槽机制的使用细节

  • 一个信号可以连接多个槽,一个槽也可以接收多个信号
  • 一个信号可以连接另一个信号,这样当一个信号发射时,另一个信号也会发射

	QLineEdit *edit;
	QPushButton *button;
	QObject::connect(edit, SIGNAL(textChanged(const QString &)), button, SIGNAL(clicked()));

  • 可以通过disconnect方法移除信号与槽之间的连接

    QPushButton *button;
    QApplication *app;
    QObject::disconnect(button, SIGNAL(clicked()), app, SLOT(exit()));

  • connect方法的第五个参数可以指定信号和槽的连接方式,即线程同步方式

    // DirectConnection,单线程,直接调用槽函数,如果有多个槽,则按连接顺序依次调用
    QObject::connect(button, SIGNAL(clicked()), app, SLOT(exit()), Qt::DirectConnection);

    // QueuedConnection,将信号放到消息队列中,再开一个线程取消息执行槽函数,当前线程立刻执行其它代码
    QObject::connect(button, SIGNAL(clicked()), app, SLOT(exit()), Qt::QueuedConnection);

    // BlockingQueuedConnection,将信号放到消息队列中,再开一个线程取消息执行槽函数,当前线程等待槽线程执行完毕才执行其它代码
    QObject::connect(button, SIGNAL(clicked()), app, SLOT(exit()), Qt::BlockingQueuedConnection);

    // AutoConnection,如果发射信号和接收信号的代码处于同一个线程当中,则使用DirectConnection,否则使用QueuedConnection
    QObject::connect(button, SIGNAL(clicked()), app, SLOT(exit()), Qt::AutoConnection);

    // UniqueConnection,如果信号和槽已经连接过,则不再连接,这不是一种连接方式,而是一种连接选项,可以与以上四种连接方式通过位运或算来联合使用
    auto type = static_cast(Qt::UniqueConnection | Qt::AutoConnection);
    QObject::connect(button, SIGNAL(clicked()), app, SLOT(exit()), type);

? 信号槽机制用于普通对象 信号槽机制实际上是定义于QObject类中的功能特性 所以它不仅可以用于控件之间的事件响应,还可以应用于任意继承自QObject的普通QT对象

以下是一个钱包类,当钱包里的金钱发生变更时,会发出金钱变更的信号,槽函数接收到这个信号后,会打印最新的金钱数量


	//wallet.h
	
	#ifndef WALLET_H
	#define WALLET_H
	
	#include 
	#include 
	
	using namespace std;
	
	class Wallet : public QObject {
	    Q_OBJECT
	
	private:
	    long money = 0;
	
	public:
	    explicit Wallet(QObject* parent = nullptr);
	    void initMoney(long money);
	    void addMoney(long extraMoney);
	
	signals:
	    void moneyChange(long money);
	
	public slots:
	    void onMoneyChange(long money);
	};
	
	#endif


	//wallet.cpp
	
	#include "wallet.h"
	
	Wallet::Wallet(QObject *parent) : QObject(parent) {}
	
	void Wallet::initMoney(long money) { emit moneyChange(money); }
	
	void Wallet::addMoney(long extraMoney) { emit moneyChange(money + extraMoney); }
	
	void Wallet::onMoneyChange(long money) {
	    this->money = money;
	    cout             
关注
打赏
1654938663
查看更多评论
0.1390s