[Qt-interest] Safety of Passing by Reference with Signals and Slots
Thiago Macieira
thiago.macieira at trolltech.com
Fri May 1 20:47:20 CEST 2009
Will Rutherdale (rutherw) wrote:
>For this purpose I use connect() with Qt::QueuedConnection and emit.
>Right now the signal and slot prototypes look something like this:
>
>
>
>signals: // in computation class
>
> void sigSendResult( ResultStruct const &ppr );
>
>
>
>private slots: // in control class
>
> void reportResult( ResultStruct const &ppr );
>
>
>
>My question is this: how safe is it to pass by reference in this
>situation?
Very safe.
All arguments in a QueuedConnection are copied onto the event that
transports the signal from one thread into another. So your emitting code
can safely have the objects for which the references were created
destroyed. They can even be temporaries.
The queued emission system also takes care of properly deleting the copies
when the event has been delivered.
Also note that the signal-slot system treats pass-by-constant-reference to
be the same as pass-by-value.
That also has two important implications:
1) you can only emit signals via queued connections if all argument types
are copiable, declared with Q_DECLARE_METATYPE and registered with
qRegisterMetaType. The registering has to happen before the signal is
emitted.
2) passing by pointer is possible, but all it accomplishes is copying of
the pointer value, not of the object being pointed to
--
Thiago Macieira - thiago.macieira (AT) nokia.com
Senior Product Manager - Nokia, Qt Software
Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090501/1643bf4b/attachment.bin
More information about the Qt-interest-old
mailing list