Kyl*_*and 5 qt qml property-binding
在QML中(至少在5.6 1版之前),一旦在JavaScript上下文中分配了新值,属性绑定就会中断:
Item {
property bool sourceBool: <some changing value e.g. from C++ code>
property bool boundBool: sourceBool
function reassignBool() {
boundBool = true;
}
}
Run Code Online (Sandbox Code Playgroud)
一旦reassignBool被调用,boundBool将是true不管的状态sourceBool。
我希望能够为不会破坏原始绑定的属性分配一个临时值;临时值将一直存在,直到NOTIFY触发了与原始绑定关联的任何信号为止,此时绑定属性将再次反映绑定计算出的值。
作为一个示例用例,假设我有一个我不希望用户连续按下两次的按钮,并且该按钮根据某些规则启用或禁用:
MyButton {
id: onceOnlyButton
// Suppose the QML type `MyButton` provides a boolean property
// called `enabled` that greys out the button and prevents it from
// being clicked when `false`.
enabled: <condition1> && <scondition2>
onClicked: {
<schedule some asynchronous work>
}
}
Run Code Online (Sandbox Code Playgroud)
假设单击该按钮时,它最终<condition1>将变为,因此该按钮最终将被适当地禁用-但不会立即被禁用。false
因此,我想执行以下操作:
....
onClicked: {
enabled = false
<schedule some asynchronous work>
enabled = Qt.binding(function() {
return <condition1> && <condition2>
}
}
Run Code Online (Sandbox Code Playgroud)
...但当然,当我重新建立绑定,因为condition1还没有尚未成为false,enabled将成为true一次。
我想我可以创建某种Connections对象,该对象将NOTIFY与之关联的信号连接<condition1>到将调用的处理程序Qt.binding,然后...。Connections我想删除该对象。但是,这似乎很复杂且微不足道。
有没有什么办法,以确保enabled被设置为false 立即,然后再重新绑定到NOTIFY的信号<condition1>,并<condition2>尽快任何这些信号的发射?
1我不太确定赋值如何影响5.7中的绑定,但是我知道为属性分配值不会自动中断绑定。因此,这可能会自动在5.7中开箱即用。
使用 QML 类型实现这一点应该相当简单Binding,这对于临时覆盖绑定而不破坏它非常有用(它从 5.6 版本之前就已经存在了!)
您将需要一个可以在when绑定类型的属性中使用的变量或属性。在下面的示例中我使用了,timer.running因为这里安排的异步工作只是一个计时器。
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
visible: true
width: 640
height: 480
property bool condition1: true
property bool condition2: true
Button {
id: onceOnlyButton
enabled: condition1 && condition2
Binding on enabled {
when: timer.running
value: false
}
onClicked: {
timer.start()
}
text: "onceOnlyButton"
}
// stuff below is just for demonstration
Button {
anchors.left: onceOnlyButton.right
text: "toggle Condition1\n(to prove binding still intact)"
onClicked: condition1 = !condition1
}
Timer {
id: timer
interval: 2000
running: false
repeat: false
onTriggered: condition1 = false
}
}
Run Code Online (Sandbox Code Playgroud)
如果您没有可用于确定异步工作是否仍在进行的变量(如我的timer.running),那么您将需要创建一个变量,如下 property forceButtonDisable。这使得它看起来更复杂,所以让我们将它包装在一个新的可重用组件中:
import QtQuick 2.7
import QtQuick.Controls 2.0
Item {
id: control
property alias text: button.text
property bool enabled
property bool forceButtonDisable: false
signal clicked()
onEnabledChanged: forceButtonDisable = false
width: button.implicitWidth
height: button.implicitHeight
Button {
id: button
width: control.width
height: control.height
enabled: control.enabled
Binding on enabled {
when: control.forceButtonDisable
value: false
}
onClicked: {
control.forceButtonDisable = true
control.clicked()
}
}
}
Run Code Online (Sandbox Code Playgroud)
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
visible: true
width: 640
height: 480
property bool condition1: true
property bool condition2: true
// once clicked, the button is temporarily disabled until original binding expression results in a different value
OnceOnlyButton {
id: onceOnlyButton
text: "onceOnlyButton"
enabled: condition1 && condition2
onClicked: timer.start()
}
// stuff below is just for demonstration
Button {
anchors.left: onceOnlyButton.right
text: "toggle Condition1\n(to prove binding still intact)"
onClicked: condition1 = !condition1
}
Timer {
id: timer
interval: 2000
running: false
repeat: false
onTriggered: condition1 = false
}
}
Run Code Online (Sandbox Code Playgroud)