[SOLVED] Q_DECLARE_METATYPE and Qt containers



  • It's a simple question but not evident from the docs at http://qt-project.org/doc/qt-5/qmetatype.html#Q_DECLARE_METATYPE

    Let's say I have a class called MyClass and I want to use QList<MyClass> with QVariant.

    Do I need to use only
    @
    Q_DECLARE_METATYPE(MyClass)
    @

    or

    @
    Q_DECLARE_METATYPE(QList<MyClass>)
    @
    (or a typedef of the above if I can't use templates).


  • Lifetime Qt Champion

    Hi,

    IIRC, Qt has built-in support for that. But nothing beats a test :)



  • Stay tuned :)



  • I'm wondering also. Should be mentioned in the documentation.

    So if I want to use MyClass in a QList and if I do that:
    @
    Q_DECLARE_METATYPE(MyClass)
    @

    I've got a compile error:
    @error C2039: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'@

    But

    • If i do that
      @
      Q_DECLARE_METATYPE(MyClass)
      Q_DECLARE_METATYPE(QList<MyClass>)
      @

    No compile error.

    Maybe a specific include is missing or the QList declare is the only thing to do?



  • Tested my code and looks like it works even without specifically using the Q_DECLARE_METATYPE for the QList.

    @r1z1 I'm not sure about your problem. Maybe you want to open another thread and post exactly where you are putting the Q_DECLARE_METATYPE line.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.