利用boost.signal模仿Qt的Signal-slot

14 篇文章 0 订阅
2 篇文章 0 订阅

 

using boost.signal and boost.bind to simulate Qt signal-slot for UI events

 

jingwenlai 2009-10-23

 

 

  Qt's signal-slot mechanism is an amazing feature. it makes the implementation of callback mechanism. Is there something that similar with Qt ? The answer is yes. Currently, there exists two famous signal-slot implementations: libsigc++ and boost.signals[2], libsigc++ is been used by gtk+, the fundation of GNOME,one of the famous GNU/Linux Destop Environment. And, Boost, it is been seen as the next generation C++ Standard library. After searching the web and learn how to use it. I code a small project to test my thoughts of a compact UI framework.

 

  Actually, the signal-slot mechanism is an Observer Design pattern,we can memorize it as the relationship between the publisher and subscriber: if the subscriber has registered, then once the publisher is updated, it can notify the subscriber, and the subscriber can receive the messages.

  In Boost.Signal library, it use signal to notify, and use connection to establish the relationship of publisher and subscribers.

  Here, I want to create a button, and simulate the clicked() event to invoke a specific function.

  Here is the code, I will explained as the code goes:

   

 

 class Button defines a virtual button to simulate my click event

 

 This is the SignalWrapper code

 

 

 Display Class is use the display the debug message to see whether the invoke is correct.

 

 

The button class's implementation

 

 

  

     After setting up the base testing code, now , we can test how it works, here is the main.cpp file,

 

 

     Here is the cmake file to compile this project, however, you can use Makefile or use another project management tools(eg. vs2005).

 

 

     I like the cmake because it can make your source directories more clean and it's cross-platform  :-)

 

     After compiling and running the project, it will display the message "Display::display...". This imply that the display method is invoked.

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值