There are a bunch of Qml Properties questions on StackOverflow, all of them are related to items in the same file, and that's easily fixable by using a Connections or a Binding directly. But what happens if an Qml object loses the connection to the outside because of a missing binding?
I created a "Spinner Slider", that's a Spinner and a Slider that shares the same value:
SpinnerSlider.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.3
RowLayout {
id: root
property alias value: sliderControl.value
property alias from: sliderControl.from
property alias to: sliderControl.to
Slider {
id: sliderControl
Layout.fillWidth: true
stepSize: 1
wheelEnabled: true
}
SpinBox {
id: spinBox
from: root.from
to: root.to
value: root.value
stepSize: sliderControl.stepSize
editable: true
onValueChanged: root.value = value
}
}
And I want to use in the following way, Two sliders, one above the other. Modifying the top one should change the value on the bottom one, changing the value on the botton one should do nothing.
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
Column {
anchors.fill: parent
SpinnerSlider {
id: outerSlider
from: 0
to: 500
value: 50
}
SpinnerSlider {
id: innerSlider
from: 0
to: 500
// Does Not Work
// value: outerSlider.value
// Works
Connections {
target: outerSlider
onValueChanged: innerSlider.value = outerSlider.value
}
// Works
Binding on value {
value: outerSlider.value
}
}
}
}
As per the comments in the code, if I use the "usual" way of connecting things on Qml, value: something.value
, the code will not work as soon as I change the values on the SpinBox because the onValueChange
will trigger and I will remove the Property Binding by effectively setting a constant to it.
What I really want is to be able to use the value: outerSlider.value
way
What I tried:
Connections, Qt.bindings, Bindings on.. in the SpinnerSlider.qml, but nothing seemed to work.
Any hints?
valueModified
signal on the originalSlider
. I think you need to add such a signal to your SpinnerSlider as well and use that to signal a change from either Slider or Spinner. – AmfasisvalueModified
instead ofvalueChanged
(which is a signal there by convention, in your case of property alias, it's even automagic). Actually that is forSpinBox
,Slider
has themoved
signal. – Amfasis