QML Switch.checked not evaluating after manually toggling the switch
-
Hey everyone,
I'm new to Qt and QML and am having an issue with setting the value of a Switch element to the value of a property from a model.
Here is the switch:BoolField { property int test: 0 id: bool4 title.text: model ? model.property : false switch1.checked: model ? model.property : false switch1.onClicked: { model.property = switch1.checked } anchors.top: bool3.bottom }
And the BoolField type:
import QtQuick 2.4 import QtQuick.Layouts 1.0 import QtQuick.Controls 1.3 Item { property alias title: title property alias switch1: switch1 height: 30 width: parent.width Text { id: title height: parent.height verticalAlignment: Text.AlignVCenter } Switch { id: switch1 anchors.right: parent.right height: parent.height } }
Note that model is set in the parent.
My problem is that while the value of "title.text" changes properly when the model changes, the position of "switch1" does not. Adding log statements to the "title.text" and "switch1.checked" statements indicates that once the switch is toggled manually, it no longer re-evaluates that property when model is changed.
I would appreciated any help that you can give me, because I am definitely stumped here! Thanks!
-
For anyone else who gets stuck on this in the future, I fixed my problem by using Qt.binding(function() {return model ? model.property : false}) inside the onClicked property to re-bind the toggle.
http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html