[Qt-interest] Howto: multiple emissions of signal cause one single slot execution.

Eirik Ulvik eiriku at simsurgery.com
Fri Jun 11 14:13:51 CEST 2010


Den 11.06.2010 14:04, skrev Davor J.:
> Suppose you have a signal "raise()" and a slot "raiseHand()". Suppose that 
> several objects can emit the "raise()" signal, simultaneously, during their 
> function executions, etc... Suppose now that during execution the "raise()" 
> signal is emitted several times in a continuous execution while the event 
> loop is non-idle. Suppose all this happens in one main thread.
> 
> Now the question: How would you execute "raiseHand()" only once in such a 
> scenario? (In other words: You don't want to raise the hand on every single 
> yell, but as a confirmation on all the yells, given a certain happening.)
> 
> I was thinking on something as QTimer::singleshot(0, this, 
> SLOT(raiseHand())), as it executes "raiseHand()" when the event loop becomes 
> idle. But still, it would execute as many times as "raise()" is called.
> 
> An other option is to use a timer that resets itself after some 5-10msec, 
> and only triggers the "raiseHand()" if it is allowed to timeout. But this 
> seems a workaround and isn't pretty. Best would be to execute it only once 
> when the event loop is idle.
> 
> Has Qt framework support for this? Or has anyone better suggestions?
> 
> Regards,
> Davor 
> 
> 
I do not know of if what you want is possible. You could always check if
raiseHand has been called in the raiseHand function itself, returning
straight away if this is not the first time it executes.

The other option I thought about would be to programatically disconnect
other signals to the raiseHand slot the first time it is executed, but I
do not know if this would work if signals have already been placed in
the event queue of the receiving object. Might be worth a try.

Regards,
Eirik


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100611/aa609ce3/attachment.bin 


More information about the Qt-interest-old mailing list