[Interest] Best way to invoke a slot after some time with QObject::sender

Preet prismatic.project at gmail.com
Tue Aug 28 04:10:48 CEST 2012


Hiya,

I have a project where I'm trying to asynchronously reply to a signal.
So if ObjectA sends ObjectB a signal, ObjectB will send ObjectA a
reply signal at some later point in time. ObjectA is guaranteed to be
alive when ObjectB decides to send a reply.  ObjectAs and ObjectB also
live in different QThreads. ObjectB doesn't know about ObjectA in
advance, so I use QObject::sender() to store ObjectA's pointer in a
queue for when ObjectB is ready to reply. There are many such
"ObjectA"s out there that ObjectB can reply to.

I see three ways to implement this in an application:

When ObjectB is ready to reply,
1  have it connect to the right ObjectA, emit a signal, then disconnect.
2  use QMetaObject::invoke() to invoke the right ObjectA's corresponding slot
3  be connected to all ObjectA's and just emit a signal with the
QObject sender pointer; this way the ObjectAs can filter out which
signals are for them in the slot itself

I'd like to know what the best method is in terms of speed/minimal
complexity, or if there's a better method I haven't listed. ObjectB
may be required to send hundreds of replies every second, so speed is
important. 3 seems like its the worst, since every reply will invoke
multiple slots. 1 and 2 seem similar; I'm kind of concerned about
calling "connect" and "disconnect" so often in 1.


Regards,

Preet



More information about the Interest mailing list