QSharedPointer<UserDataType> as signal/slot parameters
-
Off the top of my head:
@
typedef QSharedPointer<QVector<quint16> > SharedIntVectorPointer;
Q_DECLARE_METATYPE(SharedIntVectorPointer)
qRegisterMetaType<SharedIntVectorPointer>();
@But do read up on the documentation of "QMetaType":http://qt-project.org/doc/qt-5/qmetatype.html.
-
Not quite. The typedef and the Q_DECLARE_METATYPE are declarations that belong in a header file, the qRegisterMetaType is something that needs to be called from an implementation somewhere. If you put them together like you show, it won't work (and likely won't compile even).
I either create a section somewhere in my application initialization to call these qRegisterMetaType's, or I use an initialization like this in a .cpp file that is closely related to the type I am trying to register:
@
//in an implementation file outside of any function!
const int MyType::typeId = qRegisterMetaType<MyType>("MyType");
@Note that in this case, I keep the typeId in a static variable in the type itself, but any static variable will do.
-
[quote author="Andre" date="1412256899"]Not quite. The typedef and the Q_DECLARE_METATYPE are declarations that belong in a header file, the qRegisterMetaType is something that needs to be called from an implementation somewhere. [/quote]
That kind of depends on how widely they are used.
[quote]If you put them together like you show, it won't work (and likely won't compile even).[/quote]
Possibly due to the fact that I didn't add the const int instantiation. Other than that, it would work fine in a self-contained cpp file. But again, I didn't test this today, so I might be horribly off.
Edit: fixed horrible quoting
-
Thank you both, got it working finally. I've put typedef and Q_DECLARE_METATYPE in a header file and qRegisterMetaType just before connecting signals&slots. Not sure about the latter one though, is it correct aproach?
-
BTW, why do you have a QVector in a QSharedPointer? It's already "implicitly shared":http://doc.qt.io/qt-5/implicit-sharing.html, so you can just make it a stack/auto variable (not use 'new') and it should work fine and fast.
-
BTW, why do you have a QVector in a QSharedPointer? It's already "implicitly shared":http://doc.qt.io/qt-5/implicit-sharing.html, so you can just make it a stack/auto variable (not use 'new') and it should work fine and fast.
-
Hi all,
Based on your comments and suggestion I build a simple application using QSharedPointer.
So far, the application is running as expected however, when the same is debugging using GDB, a segmentation fault show up.
I just created a new post (as suggested by system) about this.
https://forum.qt.io/topic/65422/qsharedpointer-qregistermetatype-gdb-and-slots
Appreciated your feedback.
Julio
-
@Jakob-Schou
It is, if you pass immutable objects, and besides that post is quite old ...