Do arrays not save with a simple settings call in QML

Viewed 230

I was working on a simple project and needed to save values of an array run by run. If i try to use settings with a integer, it saves fine between runs. If the following code is run twice, I get 5, 15, 15, 25, as expected.

Window {
    visible: true
    width: 500
    height: 500
    id: root
    title: qsTr("Hello World")
    
    property int mNum: 5
    
    Rectangle {
        id: bob
        
        Component.onCompleted: {
            console.log(mNum)
            mNum+=10;
        }
        
        Component.onDestruction: {
            console.log(mNum)
        }
    }
    
    Settings {
        category: "justSomeSettings1"
        property alias mNumber: root.mNum
    }
}

However if I try to do the same thing with an array, I get unexpected results. For the following code, I print [peach,cherry,orange], [peach,cherry,orange,apple], [peach,cherry,orange], and [peach,cherry,orange, apple] if I run it 2 times. I am wondering why I do not get [peach,cherry,orange], [peach,cherry,orange,apple], [peach,cherry,orange,apple], and [peach,cherry,orange,apple,apple].

Window {
    visible: true
    width: 500
    height: 500
    id: root
    title: qsTr("Hello World")
    
    property var mArr: ["peach", "cherry", "orange"]
    
    Rectangle {
        id: joe
        
        Component.onCompleted: {
            console.log(mArr);
            mArr.push("apple");
        }
        
        Component.onDestruction: {
            console.log(mArr);
        }
    }
    
    Settings {
        category: "justSomeSettings2"
        property alias mArray: root.mArr
    }
}

Does anyone know why this happens or if there are any workarounds?

1 Answers

var properties do not signal when they are updated. So your Settings object never finds out that the value changed. You can emit the signal manually though. So you can do this:

mArr.push("apple");
mArrChanged();
Related