欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > C++ 实现QT信号槽

C++ 实现QT信号槽

2024/11/29 19:32:53 来源:https://blog.csdn.net/qq_30220519/article/details/140162260  浏览:    关键词:C++ 实现QT信号槽

https://github.com/libsigcplusplus/libsigcplusplus

#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::connectionmydetector.run();target.disconnect();return 0;
}


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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com