[Interest] Best way to QObject-wrap simple objects

Thiago Macieira thiago.macieira at intel.com
Wed Feb 20 04:36:14 CET 2013


On terça-feira, 19 de fevereiro de 2013 21.47.08, K. Frank wrote:
> Thanks for noting that.
> 
> It seems a little odd, though.  I could of sworn I tried passing a
> reference:
> 
>    void my_signal (const POD &data)
> 
> and it failed at run time when I tried to pass the reference through a
> queued connection.

I've seen some people report this problem if they forgot to #include the full 
declaration of the type. I really don't know how anything even compiled, but 
it seems to cause the problem.

> Of course, pointers and references are not identical, but, under the
> hood, you would think that if a queued connection could handle one,
> it could handle the other.

Quite to the contrary! Const-references and pointers are *very* different. 
Const-references are identical to passing by value as far as the signal-slot 
connection mechanism is concerned. When you write:

	void my_signal(const POD &data);

you may see that the meta object registers a signal "my_signal(POD)". 
Therefore, the metatype system will try to call POD's copy constructor which 
was registered with qRegisterMetaType.

If you pass a pointer, then it simply copies the pointer. It's your 
responsibility to ensure that the pointer remains valid at the indeterminate 
time later when the slot is called in the other thread.

And finally, non-const references cannot be used in a queued connection.

> Is it in fact the case that queued connections can handle pointers
> but not references (to non-qmetatypes), or am I just misremembering
> what I did?

You were remembering correctly.

> > If it really must be POD by value, then use Samuel's approach.
> 
> Well, in my case it certainly doesn't have to be by value, but by
> value or by reference stylistically reflects the by-value semantics
> of the POD.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130219/1837b2d4/attachment.sig>


More information about the Interest mailing list