[Interest] how can I call qRegisterMetaType?

Thiago Macieira thiago.macieira at intel.com
Tue May 17 03:04:34 CEST 2016


On terça-feira, 17 de maio de 2016 09:47:18 PDT Hamish Moffatt wrote:
> On 17/05/16 01:27, Thiago Macieira wrote:
> > The overhead is almost null because the inline parts of qRegisterMetaType
> > function check whether it's already registered before calling the
> > non-inline parts.
> 
> How about QMetaType::registerConverter? The documentation says all
> QMetaType functions are thread-safe. Is it also fast enough to call
> repeatedly?

It's fast, but that one prints a warning:

    if (!customTypesConversionRegistry()->insertIfNotContains(qMakePair(from, 
to), f)) {
        qWarning("Type conversion already registered from type %s to type %s",
                 QMetaType::typeName(from), QMetaType::typeName(to));
        return false;
    }
    return true;

> > PS: DO NOT use the qRegisterMetaType overload with 1 argument. Use the one
> > with zero.
> 
> What's the difference? When is the one with the char* typeName parameter
> needed?

The name comes from your Q_DECLARE_METATYPE macro, so you don't need to pass 
it. Therefore, using the version with the name means you're typing more than 
you have to.

And you're bypassing the check to see if the type has already been registered.

> I have a Q_PROPERTY of type QList<MyClass::MyEnum>, and I find that if I
> call
> 
> qRegisterMetaType<QList<MyEnum>>();
> 
> (ie without a typeName parameter), then I am unable to read the property
> through QMetaProperty::read(), and a message is printed: "Unable to
> handle unregistered data type 'QList<MyEnum>' for property
> 'MyClass::propertyName'". Adding the name parameter solves it. However a
> QList<int> property works without giving a name.

Yeah, qmetatype.h is a mess. I can't read it. My only guess is this:

> 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.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list