Important: Please read the Qt Code of Conduct -

[SOLVED] QQmlListProperty: QObject subclass usage

  • I have a QObject subclass like in "this": example, and I have a list property in it:
    @class Subclass: public QObject
    Q_PROPERTY(QQmlListProperty<Subclass> content READ content)
    QList<Subclass*> mContent;
    QQmlListProperty<Subclass> content() {return QQmlListProperty<Subclass>(this, mContent);}

    I don't see any difference, however if I try to set it from QML, I get "Cannot assign multiple values to a singular property". BUT! With the following code everything works:
    @class Subclass: public QObject
    Q_PROPERTY(QQmlListProperty<QObject> content READ content)
    QList<QObject*> mContent;
    QQmlListProperty<QObject> content() {return QQmlListProperty<QObject>(this, mContent);}

    Can you tell me what is wrong? I do want QQmlListProperty<Subclass> to have an automatic typecheck, because I need that only Subclass and it's children to be set as "content".

  • Hi,

    Do you register "Subclass" with the QML type system via qmlRegisterType()?


  • OMG!
    Well, that solved the whole thing :) I were using Sub-Subclasses from QML and those were registered, while Subclass itself wasn't.
    Thank you very much!

  • damm, had the same problem.

    for the record, the error message was

    Invalid property assignment: "roles" is a read-only property

    Q_PROPERTY(QQmlListProperty<ModelRole> roles READ roleObjects)

Log in to reply