已启用 属性 个被附加操作阻止的 QML 小部件
Enabled property of QML widget blocked by attached action
我想在 TextField
有可接受的文本时启用按钮(我正在使用 validator
),并且这段代码工作正常:
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 400
height: 100
id: mainWindow
property int _buttonSize: 30
property int _interval: 10
TextField {
y: _interval
width: parent.width
height: _buttonSize
id: ipInput
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
placeholderText: "IP"
validator: RegExpValidator
{
regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
}
}
Button {
enabled: ipInput.acceptableInput
id: go
anchors.horizontalCenter: parent.horizontalCenter
y: ipInput.y+_buttonSize+_interval
width: parent.width
height: _buttonSize
text: "GO"
}
}
所以,我将 Action
添加到此 Button
:
Button {
enabled: ipInput.acceptableInput
id: go
anchors.horizontalCenter: parent.horizontalCenter
y: ipInput.y+_buttonSize+_interval
width: parent.width
height: _buttonSize
text: "GO"
action: goAction
Action {
id: goAction
shortcut: "Enter"
enabled: go.enabled && go.visible
onTriggered: {
console.log("good")
}
}
}
现在 Button
始终处于禁用状态。我该如何解决?
Action
s 通过 同步 它们所绑定的所有 Item
s 的状态来工作。文档说:
In applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.
An action can be bound to a menu item and a toolbar button, and it will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.
从这个意义上说,Action
属性 支配 它所绑定的 Item
的属性,反之亦然。当 Action
启用时,所有 它所附加的 Item
也会启用。因此,启用条件应移至 Action
.
这是您的代码的重新访问版本。我添加了另一个 Button
来强调 Action
功能。这里两个 Button
都会在满足条件时自动启用,因为关联的 Action
会启用。
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
id: mainWindow
Column {
spacing: 10
TextField {
width: 400
height: 40
id: ipInput
horizontalAlignment: TextInput.AlignHCenter
placeholderText: "IP"
validator: RegExpValidator {
regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
}
}
Action {
id: goAction
shortcut: "Enter"
enabled: ipInput.acceptableInput
onTriggered: {
console.log("good")
}
}
Button {
id: go
width: 400
height: 40
text: "GO"
action: goAction
}
Button {
id: go2
width: 400
height: 40
text: "GO2"
action: goAction
}
}
}
我想在 TextField
有可接受的文本时启用按钮(我正在使用 validator
),并且这段代码工作正常:
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 400
height: 100
id: mainWindow
property int _buttonSize: 30
property int _interval: 10
TextField {
y: _interval
width: parent.width
height: _buttonSize
id: ipInput
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
placeholderText: "IP"
validator: RegExpValidator
{
regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
}
}
Button {
enabled: ipInput.acceptableInput
id: go
anchors.horizontalCenter: parent.horizontalCenter
y: ipInput.y+_buttonSize+_interval
width: parent.width
height: _buttonSize
text: "GO"
}
}
所以,我将 Action
添加到此 Button
:
Button {
enabled: ipInput.acceptableInput
id: go
anchors.horizontalCenter: parent.horizontalCenter
y: ipInput.y+_buttonSize+_interval
width: parent.width
height: _buttonSize
text: "GO"
action: goAction
Action {
id: goAction
shortcut: "Enter"
enabled: go.enabled && go.visible
onTriggered: {
console.log("good")
}
}
}
现在 Button
始终处于禁用状态。我该如何解决?
Action
s 通过 同步 它们所绑定的所有 Item
s 的状态来工作。文档说:
In applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.
An action can be bound to a menu item and a toolbar button, and it will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.
从这个意义上说,Action
属性 支配 它所绑定的 Item
的属性,反之亦然。当 Action
启用时,所有 它所附加的 Item
也会启用。因此,启用条件应移至 Action
.
这是您的代码的重新访问版本。我添加了另一个 Button
来强调 Action
功能。这里两个 Button
都会在满足条件时自动启用,因为关联的 Action
会启用。
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
id: mainWindow
Column {
spacing: 10
TextField {
width: 400
height: 40
id: ipInput
horizontalAlignment: TextInput.AlignHCenter
placeholderText: "IP"
validator: RegExpValidator {
regExp:/^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/
}
}
Action {
id: goAction
shortcut: "Enter"
enabled: ipInput.acceptableInput
onTriggered: {
console.log("good")
}
}
Button {
id: go
width: 400
height: 40
text: "GO"
action: goAction
}
Button {
id: go2
width: 400
height: 40
text: "GO2"
action: goAction
}
}
}