Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QList as qml model



  • Hello,

    I have a property of type QList which is then used in a QML file as a model for a list view.
    When I change the various items, the QML display does change to reflect this item's change.
    But if I change the list content itself (add, remove, swap) items, there is no refresh on the QML side of things.

    I declared my property as so:
    Q_PROPERTY(QList<MyObj*> liste READ getliste)
    QList<MyObj*> _liste;
    QList<MyObj*> getliste() { return _liste; }

    Assuming that the problem was that QML was getting a COPY of the list, I tried declaring it as: QList<MyObj*> *, but QML did not seem able to handle that.

    How do I need to declare my QList so that QML will follow the changes in the model when they happen?

    Cyrille


  • Moderators

    @Cyrille-de-Brebisson

    Q_PROPERTY(QList<MyObj*> liste READ getliste NOTIFY listeChanged)
    

    now emit the listeChanged signal everytime you change the list

    If you would implement a QAbstractListModel subclass you would have more control of rearrangements etc.


Log in to reply