Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get previous property value before it changed? (in QML)

Tags:

properties

qml

I want to understand the following question:

How can I store the previous value of a property in the declarative QML language?

The task is to memorize property value to another property before it change. The problem is that the existing signal mechanism onPropertyNameChanged(). This mechanism emits a signal about the property change after its modification. And in this handler it is impossible to get the previous value of the property for memorize it.

It is desirable to see code examples.

like image 957
Xintrea Avatar asked Jan 25 '18 12:01

Xintrea


2 Answers

Interesting question. The only way I see is a bit stupid:

Item {
    id: item
    property int prev: 0
    property int temp: value
    property int value: 0
    onValueChanged: {
        prev = temp;
        temp = value;
        console.log("prev=" + prev);
        console.log("value=" + value)
        console.log("---------------");
    }

    Timer {
        interval: 1000
        repeat: true
        running: true
        onTriggered: {
            item.value = Math.round(Math.random() * 1000);
        }
    }
}
like image 180
folibis Avatar answered Oct 07 '22 01:10

folibis


why not create the buffer property yourself? eg)

property int value: 0
property int prevValue: 

onValueChanged: {
    ... logic to deal with value change
    ... if you are trying to diff with prevValue, make sure check if prevValue is not undefined (first time Value changes)
    prevValue = value;
}

if you are dealing with custom type "var" then you can try:

onValueChanged: {
    prevValue = JSON.parse(JSON.stringify(value));
}
prevValue = JSON.parse(JSON.stringify(value));
like image 44
hoistyler Avatar answered Oct 07 '22 01:10

hoistyler