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.