Item on Repeater create and destroy when model changed



  • Hi,
    I have a repeater with model given by C++ part. There are several field on this model : "name", "value", ... The field "value" change a lot.
    I have notice all item are destroy and recreate each time model change. Is that serious ? Is an other way to do this ?

    Repeater {
        id:repeater
        model: myApp.data
        Rectangle {
            width: 300
            height: 300
    	border.color:"black"
            Row {
               Text {text:modelData.name}
               Text {text:modelData.value}
    	} 
    	Component.onCompleted: console.log("complete", index)
            Component.onDestruction: console.log("destruction", index)              
        }
    }
    


  • What is the type of myApp.data? If you make it a proper model, Repeater is able to listen to fine-grained notifiers and act correspondingly. If it's some sort of JS-array/object, Repeater gets a notification that the JS-array changes, but doesn't know what exactly changed. It has to rebuild the entire representation, because it basically corresponds to changing the entire model.



  • It's a QJsonArray :

        Q_PROPERTY(QJsonArray data READ data WRITE setData NOTIFY dataChanged)
    

    Do you mean it's better to have a QQmlListProperty<MyObject> with MyObject :

    class MyObject : public QObject
    {
    	Q_OBJECT
    	Q_PROPERTY(QString name READ name NOTIFY nameChanged)
    	Q_PROPERTY(int value READ value NOTIFY valueChanged)
    public :
    	...
    };
    

    Is there an influence over performance of application ?



  • @helenebro It's better to use either QAbstractItemModel (or any descendant) in C++, or ListModel (or other ready-made models) in QML. Since you have a QJsonArray, you could implement a simple QAbstractListModel around it. Just make sure to emit the appropriate signals by calling beginInsertRows() + endInsertRows() when you insert rows, for instance.



  • Ok. Thank you for your answer.
    I never use QAbstractListModel. Do you have some keys to start ?

    What is difference between using QAbstractListModel and QQmlListProperty ?
    In which case is it better to use them ?


Log in to reply
 

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