[Interest] how can I call qRegisterMetaType?
Hamish Moffatt
hamish at risingsoftware.com
Tue May 17 03:41:59 CEST 2016
On 17/05/16 11:04, Thiago Macieira wrote:
> On terça-feira, 17 de maio de 2016 09:47:18 PDT Hamish Moffatt wrote:
>> And do I need to call
>> Q_DECLARE_METATYPE(QList<MyQuestion::QuestionType>) or not? It doesn't
>> seem to be necessary in my test cases.
> Note that it's a macro declaring a template specialisation, so it's not a
> call.
>
> Yes, you must declare your metatypes before registering them. Remember the
> part about there being a variable that avoids the multiple registrations?
> That's created by this macro.
>
It seems that for a metatype that is a QList<MyClass::MyEnum> I must
call qRegisterMetaType with the name specified, else reading properties
complains that the metatype isn't registered. What worries me is that
the type name must be specified without the class, as in
qRegisterMetaType<QList<MyClass::MyEnum>>("QList<MyEnum>");
If I have another enum MyOtherClass::MyEnum am I going to have collisions?
Sample code:
#include <QObject>
#include <QList>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(MyEnum myEnum MEMBER myEnum)
Q_PROPERTY(QList<MyEnum> myEnumList MEMBER myEnumList)
public:
enum MyEnum { EA, EB, EC };
Q_ENUM(MyEnum)
MyEnum myEnum;
QList<MyEnum> myEnumList;
MyClass()
: QObject(nullptr)
, myEnum(EB)
, myEnumList({ EB, EB, EC })
{}
};
Q_DECLARE_METATYPE(QList<MyClass::MyEnum>)
int main(int argc, char** argv)
{
qRegisterMetaType<MyClass::MyEnum>();
qRegisterMetaType<QList<MyClass::MyEnum>>();
MyClass c;
QVariant v = c.property("myEnum");
qDebug() << v;
v = c.property("myEnumList"); // will print an error
qDebug() << v;
qRegisterMetaType<QList<MyClass::MyEnum>>("QList<MyClass::MyEnum>");
v = c.property("myEnumList"); // will print an error
qDebug() << v;
qRegisterMetaType<QList<MyClass::MyEnum>>("QList<MyEnum>");
v = c.property("myEnumList"); // will succeed
qDebug() << v;
return 0;
}
#include "meta.moc"
Hamish
More information about the Interest
mailing list