PDA

View Full Version : Emit one signal from multiple classes



Ishmael
28th June 2010, 21:02
According to the documentation on signals and slots: "Only the class that defines a signal and its subclasses can emit the signal." Is there an elegant way to circumvent this restriction? I would like to be able to emit a signal from multiple classes. Thanks for your help!

Lykurg
28th June 2010, 22:20
Well you can define the same signal in all your classes or create a minimal class defining the signal and use it as a "global" base class. (I am not sure why someone want to do so, so can you tell us what you are trying.)

Or if I don't get you right and you want emit a signal of a foreign class, then you have to create a public function which emits the signal, so that you can call that function from your other classes. (?)

Ishmael
28th June 2010, 22:53
Those are good suggestions, thanks! Just to clarify, the situation I have is this: I have a simple GUI (QMainWindow) as well as a class that reads from a serial port (I'm using qextserialport, which has its own thread). When certain bytes are read from the serial port, I would like to emit a signal that the main GUI responds to. The same signal can be emitted from within the GUI too (which is where the signal is defined), hence the desire to have a 'global' signalling mechanism. For now, I've been able to work around the issue by simply avoiding the signal/slot mechanism in my serial port class and calling the required functions in my GUI class directly instead of emitting a signal.

squidge
28th June 2010, 23:18
Your serial thread is calling functions in your gui class directly? :eek:

But it would seem that you would be better off using an event. They can be generated and send from anywhere.

ChrisW67
28th June 2010, 23:57
For now, I've been able to work around the issue by simply avoiding the signal/slot mechanism in my serial port class and calling the required functions in my GUI class directly instead of emitting a signal.

I don't see what needs a work-around here. Your GUI class defines a signal that you connect to a slot that is also defined in the GUI class (although you could call it directly from here). The serial port management class also defines a signal that you connect to the GUI object's slot. Whether you name the signals identically is irrelevant.