C++ 实现QT信号槽

https://github.com/libsigcplusplus/libsigcplusplus

cpp 复制代码
#include <iostream>

/*
在sigslot.h的420,将:
//typedef sender_set::const_iterator const_iterator;
改为:
//typedef typename sender_set::const_iterator const_iterator;

#include <sigslot.h>
using namespace sigslot; //必须加上sigslot的命名空间
//在用vs调试时还需要将sigslot.h中很多的自定义模板结构类型前加typename
const int TTRUE = 1;
const int TFALSE = 0;
class Switch
{
public:
	signal0<> Clicked;
	//这里的信号是不带参数的,signaln表示带几个参数
};
class Light : public has_slots<>
{
public:
	Light(bool state) { b_state = state; Displaystate(); }
	void ToggleState() { b_state = !b_state; Displaystate(); } //作为消息的响应
	void TurnOn() { b_state = TTRUE; Displaystate(); }
	void TurnOff() { b_state = TFALSE; Displaystate(); }
	void Displaystate() { std::cout << "The state is " << b_state << std::endl; }
private:
	bool b_state;
};
void main()
{
	Switch sw1, sw2, all_on, all_off;
	Light lp1(TTRUE), lp2(TFALSE);
	sw1.Clicked.connect(&lp1, &Light::ToggleState); //绑定
	sw2.Clicked.connect(&lp2, &Light::ToggleState);
	all_on.Clicked.connect(&lp1, &Light::TurnOn);
	all_on.Clicked.connect(&lp2, &Light::TurnOn);
	all_off.Clicked.connect(&lp1, &Light::TurnOff);
	all_off.Clicked.connect(&lp2, &Light::TurnOff);

	sw1.Clicked();
	sw2.Clicked();
	all_on.Clicked();
	all_off.Clicked();

	sw1.Clicked.disconnect(&lp1);
	sw2.Clicked.disconnect(&lp2);
	all_on.Clicked.disconnect(&lp1);
	all_on.Clicked.disconnect(&lp2);
	all_off.Clicked.disconnect(&lp1);
	all_off.Clicked.disconnect(&lp2);
}
*/

#include <sigc++/sigc++.h>
class AlienDetector
{
public:

	void run() { signal_detected.emit("test"); }

	sigc::signal<void(std::string)> signal_detected;	// changed
};

void warn_people(std::string where)
{
	std::cout << "There are aliens in " << where << "!" << std::endl;
}

int main_callback()
{
	AlienDetector mydetector;
	auto target = mydetector.signal_detected.connect(sigc::ptr_fun(warn_people));  // return sigc::connection


	mydetector.run();

	target.disconnect();
	return 0;
}

创作不易,小小的支持一下吧!

相关推荐
T***16079 分钟前
JavaScript打包
开发语言·javascript·ecmascript
qq_3363139310 分钟前
java基础-常用的API
java·开发语言
道一2326 分钟前
C# 读取文件方法介绍
开发语言·c#
蒋星熠26 分钟前
常见反爬策略与破解反爬方法:爬虫工程师的攻防实战指南
开发语言·人工智能·爬虫·python·网络安全·网络爬虫
是店小二呀28 分钟前
在家搭个私人影院?LibreTV+cpolar,随时随地看片自由
开发语言·人工智能
利刃大大1 小时前
【c++中间件】Elasticsearch介绍与安装 && 核心概念 && Kibana && 二次封装
c++·elasticsearch·中间件
catchadmin1 小时前
使用 PHP 和 Raylib 也可以开发贪吃蛇游戏
开发语言·游戏·php
p***43481 小时前
JavaScript数据分析实战
开发语言·javascript·ecmascript
蓝天智能2 小时前
立体字型效果的Qt QSS实现
开发语言·qt