Solved Restore property binding when changed
-
Hello,
for a settings page, i have a TextField which is connected to a c++ property:
Q_PROPERTY(QString serialNumber READ getSerialNumber WRITE setSerialNumber NOTIFY serialNumberChanged)
QML:
TextField { id: textFieldSerialNumber value: backend.serialNumber }
When the page is loaded, the value is displayed and changed-signals are received.
Now the user should have an option to discard any changes made to the TextField. There is now an extra button:
Button { id: buttonDiscard onClicked: textFieldSerialNumber= backend.serialNumber }
This restores textFieldSerialNumber to the original value of the backend, but now it seems i've lost the binding to the c++ property and i can't receive any changes from the backend anymore.
I see this when using another button for resetting to default values:
Button { id: buttonReset onClicked: backend.resetSerialNumber() }
This changes the c++ property to the default value and emits a changed() signal to qml. But because the user clicked "discard", the property is not updated anymore.
Is there any solution only with properties?
I appreciate any help.
-
@kkettinger said in Restore property binding when changed:
Is there any solution only with properties?
My way to doing this would be:
property var aux: backend.serialNumber TextField { id: textFieldSerialNumber value: backend.serialNumber } Button { id: buttonDiscard onClicked: textFieldSerialNumber.value = backend.serialNumber // Set current value (no more updates) } Button { id: buttonReset onClicked: { textFieldSerialNumber.value = backend.serialNumber // set current value textFieldSerialNumber.value = Qt.binding(function() { return backend.serialNumber} ) //"restore" connection (updated on changes) } }
-
This is one of the classic problems of QML :-) Check Andre's slides: https://www.kdab.com/kdab-qt-world-summit-2019/#component The video went down for some reason :-(
In general, there is no ready-made solution for this in Qt, but there are several ways to address this problem and it can be fixed quite easily.
-
Please check if something like this works:
property var aux: backend.serialNumber TextField { id: textFieldSerialNumber value: aux } Button { id: buttonDiscard onClicked: aux = "some original value" } Button { id: buttonReset onClicked: aux = backend.serialNumber //"restore" connection }
-
@kkettinger said in Restore property binding when changed:
Is there any solution only with properties?
My way to doing this would be:
property var aux: backend.serialNumber TextField { id: textFieldSerialNumber value: backend.serialNumber } Button { id: buttonDiscard onClicked: textFieldSerialNumber.value = backend.serialNumber // Set current value (no more updates) } Button { id: buttonReset onClicked: { textFieldSerialNumber.value = backend.serialNumber // set current value textFieldSerialNumber.value = Qt.binding(function() { return backend.serialNumber} ) //"restore" connection (updated on changes) } }
-
You may want to have a look at the Qt.binding() function to re-activate a broken binding. See https://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html#creating-property-bindings-from-javascript for details.
-
Using Qt.binding() does get the job done, thank you :)