Templated QAbstractListModel



  • Hello,
    I created templated QAbstractListModel:

    template <class T> class ListModel : public QAbstractListModel
    

    I register ListModel<SomeData> to QML:

    qmlRegisterType<ListModel<SomeData>>("x", 1, 0, "SomeDataList");
    

    I use a list in a class as qproperty:

    Q_PROPERTY(ListModel<SomeData> *someDataList READ someDataList CONSTANT)
    

    However, when I access it from QML, I get:

    QMetaProperty::read: Unable to handle unregistered datatype 'ListModel<SomeData>*' for property 'MyClass::someDataList '
    

    Can you please help me solve the problem. I would like to access templated QAbstractListModel from QML. Thank you.


  • Moderators

    @MartinD
    i believe thats not possible. There might be a high chance that the Q_OBJECT macro is needed at some point, which isn't available for templated classes.

    But your warning/error comes from the fact that the type isn't a known meta-type.

    So at some point you need to add the following (into a global header?) to register the used types:

    Q_DECLARE_METATYPE(ListModel<SomeData>*);
    

    And its also good practice to call qRegisterMetyType<ListModel<SomeData>*>() once at some point.

    Add these lines and lets see if thats maybe already enough to get it working.



  • @raven-worx Thank you, that works.


Log in to reply
 

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