[Qt-interest] emit custom built objects in signal/slot
Colin S. Miller
no-spam-thank-you at csmiller.demon.co.uk
Tue Sep 15 17:06:16 CEST 2009
Srdjan Todorovic wrote:
> Hi all,
>
> On 15/09/2009, Ulf-Alexander v. Ceumern-Lindenstjerna
> <ceumern at vrmagic.com> wrote:
>
>> void stuff
>> {
>> MyClass l_data;
>> emit stuffHappened(l_data);
>> }
>
>> But now, as I think about it, what about l_data lifetime? Do I need to
>> make sure it exists long enough?
>
> I've not looked up the documentation specifically, but as far as I
> understand it, if the connection is a direct connection, then the emit
> line will be effectively:
>
> stuffHappened(l_data);
>
> as in a function call, and things will be OK. If the connection is a
> queued connection, then there may be a problem that the object goes
> out of scope before/during use in the slot.
>
> Srdjan
Srdjan, Ulf-Alexander,
I'm still an newbie with Qt, but according to the docs at
http://doc.trolltech.com/4.3/signalsandslots.html#signals
"When a signal is emitted, the slots connected to it are usually executed immediately,
just like a normal function call. When this happens, the signals and slots mechanism is
totally independent of any GUI event loop. Execution of the code following the emit statement
will occur once all slots have returned. The situation is slightly different when using
queued connections; in such a case, the code following the emit keyword will continue
immediately, and the slots will be executed later."
Thus, as long as you haven't enabled "queued connections", your code is correct.
Some of my code relies on this being the case as well.
HTH,
Colin S. Miller
--
Replace the obvious in my email address with the first three letters of the hostname to reply.
More information about the Qt-interest-old
mailing list