PDA

View Full Version : Slot connected to a generic signal



Momergil
26th June 2014, 13:29
Hello!

I never heard of this and don't remember that on the manual, but... is there a way to connect a generic signal to a object's slot? :)

What I mean is: the normal way of doing signal/slot connection is to take a speific object's signal and connect it to a specific object's slot. But lets suppose I create an object that should have many other objects related to it by means of passing some info. The normal way would be that for each new object created, I would have to once again to a QObject::connect() call between signal and slot. So instead of having to do such an effort, why don't create an specification that tells "all signals with the exact name "xxxx", no matter from which object it comes from, shall be connected to this object's slot"? Something like:



connect(0,SIGNAL(mySignal(QString)),myObject,SLOT( slotMySlot(QString)));


That would be something interesting :) So is there a way of doing this?

Thanks,

Momergil

anda_skoa
26th June 2014, 14:53
The way signal/slot works is that the sender object knows a list of receivers and when the signal is "emitted", the code iterates over this list and calls the receiver slots.

Without sender there is no such list, so no where to store the connect's information.

Cheers,
_

Momergil
26th June 2014, 18:52
Without sender there is no such list, so no where to store the connect's information.

Well, that is understandable, but still it shouldn't be an actual problem to my "proposal". I mean, the qmake system could simply, once the order is given, create this list inserting the connection for all senders that has that particular signal implemented; it doesn't look like a blocker to me.

Anyway, thanks for the reply.

Momergil

anda_skoa
26th June 2014, 19:57
Even if we do not consider objects of classes that are not known at build time, e.g. from plugins, this would still not solve the problem.

The QObject class declares the list, but without an instance there is no list instance.

If you need this for a class of your own, you can implement that in that class.
Implement a static "pre-connect" method that stores lists of signal,receiver,slot triples and each instance works through that list in its constructor.

Cheers,
_

Momergil
27th June 2014, 15:48
If you need this for a class of your own, you can implement that in that class.
Implement a static "pre-connect" method that stores lists of signal,receiver,slot triples and each instance works through that list in its constructor.

Very interesting idea (at least if I understood it properly :p). Thanks!

Momergil

d_stranz
28th June 2014, 23:42
connect(0,SIGNAL(mySignal(QString)),myObject,SLOT( slotMySlot(QString)));

This reminds me of the famous "COME FROM (http://www.fortran.com/come_from.html)" statement in FORTRAN.