Project

General

Profile

Signal/Slot - 1:1 or 1:M?

Added by Mark Travis about 2 years ago

Is a signal only generated by the child and the slot listened to by the parent?

Can you have multiple objects (classes) listening to one Signal?

Can a grandparent object (sorry for the metaphor!) listen to a grandchild in the tree?

From all I've seen in the documentation, a parent object instantiates a child object, then configures the slot to listen to the Signal defined in the Child. I saw no other way to set this up.


Replies (2)

RE: Signal/Slot - 1:1 or 1:M? - Added by Roel Standaert about 2 years ago

The signal/slot system is entirely separate from the widget tree, so you can connect as many slots to a signal as you like from wherever you want.

You only have to make sure that any data accessed in the slot outlives the signal.

RE: Signal/Slot - 1:1 or 1:M? - Added by Mark Travis about 2 years ago

I couldn't find an example where a slot was implemented by an object that didn't create it.

Using the Hangman example:

HangmanGame instantiates HangmanWidget. HangmanWidget defines "Wt::Signal scoreUpdated_;" and "Wt::Signal& scoreUpdated() {return scoreUpdated_;}"
Then HangmanGame creates a slot "game_->scoreUpdated().connect(std::bind(&Session::addToScore,&session_,std::placeholders::_1));

I completely understand all of those mechanics.

Ohh.... Ha! I think I see now. So, the parent object in this case (HangmanGame) created a slot to connect the Session object's addToScore method (at the address of the instantiated object "session_") to HangmanWidget's Signal.

So, do all of the slots have to be created in the parent object (HangmanGame) on behalf of any of the other objects that want to listen for that signal?

If not, let's say I created another class called "ListenOnly" with a method that should run when scoreUpdated() signaled. Let's say I don't create the slot for ListenOnly in the HangmanGame class. How would I create the slot in the ListenOnly class? Would I have to make the signal in HangmanWidget EXTERN and include the header in ListenOnly?

Sorry if I'm missing something obvious, I'm refreshing my c++ skills while also learning the nuances of Wt, which makes things more difficult sometimes.

    (1-2/2)