passing_slots.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Example program showing passing of slots through an interface.
  2. //
  3. // Copyright Douglas Gregor 2001-2004.
  4. // Copyright Frank Mori Hess 2009.
  5. //
  6. // Use, modification and
  7. // distribution is subject to the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // For more information, see http://www.boost.org
  11. #include <iostream>
  12. #include <boost/signals2/signal.hpp>
  13. //[ passing_slots_defs_code_snippet
  14. // a pretend GUI button
  15. class Button
  16. {
  17. typedef boost::signals2::signal<void (int x, int y)> OnClick;
  18. public:
  19. typedef OnClick::slot_type OnClickSlotType;
  20. // forward slots through Button interface to its private signal
  21. boost::signals2::connection doOnClick(const OnClickSlotType & slot);
  22. // simulate user clicking on GUI button at coordinates 52, 38
  23. void simulateClick();
  24. private:
  25. OnClick onClick;
  26. };
  27. boost::signals2::connection Button::doOnClick(const OnClickSlotType & slot)
  28. {
  29. return onClick.connect(slot);
  30. }
  31. void Button::simulateClick()
  32. {
  33. onClick(52, 38);
  34. }
  35. void printCoordinates(long x, long y)
  36. {
  37. std::cout << "(" << x << ", " << y << ")\n";
  38. }
  39. //]
  40. int main()
  41. {
  42. //[ passing_slots_usage_code_snippet
  43. Button button;
  44. button.doOnClick(&printCoordinates);
  45. button.simulateClick();
  46. //]
  47. return 0;
  48. }