Correct way to register with QMetaType
-
How should I register my class/struct that is inside a namespace:
namespace base { namespace impl { class A { ... }; } } Q_DECLARE_METATYPE(base::impl::A);
I want to use this class inside
base::impl
namespace as well asbase::util
namespace.
How would I register this withqRegisterMetaType
:qRegisterMetaType<base::impl::A>("base::impl::A"); // or qRegisterMetaType<base::impl::A>("A"); // or qRegisterMetaType<base::impl::A>();
How would I register this with
qRegisterMetaTypeStreamOperators
:qRegisterMetaTypeStreamOperators<base::impl::A>("base::impl::A"); // or qRegisterMetaTypeStreamOperators<base::impl::A>("A"); // or qRegisterMetaTypeStreamOperators<base::impl::A>();
Also, I will be using this class in different ways, like
QVector<QVector<base::impl::A>>
,std::shared_ptr<base::impl::A>
andQVector<QVector<std::shared_ptr<base::impl::A>>>
should I register these as well withqRegisterMetaType
andqRegisterMetaTypeStreamOperators
or just registering the class itself (i.e.base::util::A
) will be enough? If I should then what will be the signature? -
Hi,
How exactly are you using your class ?
Because just using it in a different namespace does not require to register it. Are you using it with QVariant ? -
@SGaist Yes, I am using it with
QVariant
, mostly withQSettings
to save and retrieve data easily by overridingQDataStream
operators. I am also using some in queued signal and slot connections. The ones I am registering for are mostly structs for storing data and just a few simple classes.Due to these requirements, I need to use all three (
Q_DECLARE_METATYPE
,qRegisterMetaType
andqRegisterMetaTypeStreamOperators
) operators, but to me, it seems to work fine if I do not add the string part at all, i.e. register as:qRegisterMetaType<base::impl::A>(); // or qRegisterMetaType<QVector<base::impl::A>>(); // or qRegisterMetaTypeStreamOperators<base::impl::A>(); // or qRegisterMetaTypeStreamOperators<QVector<base::impl::A>>(); ...
But, it also works fine if I add the string part inside the brackets:
qRegisterMetaType<base::impl::A>("base::impl::A"); // or qRegisterMetaType<QVector<base::impl::A>>("QVector<base::impl::A>"); // or qRegisterMetaTypeStreamOperators<base::impl::A>("base::impl::A"); // or qRegisterMetaTypeStreamOperators<QVector<base::impl::A>>("QVector<base::impl::A"); ...
The definition as well as most of the examples I see does provide a string part inside the brackets (especially for
qRegisterMetaTypeStreamOperators
). Which makes me confused as to what is the reason behind adding the string part such as "base::impl::A" inside the brackets and how does it affect the usage?Debugging problems caused by these kinds of ambiguity (especially on VS) is difficult and I do not want to make mistakes now which might give me errors later and would consume a lot of my time in figuring out that I registered the type or stream operators incorrectly.
-
If memory serves well, the version with a QString parameter is geared toward declaring aliases.