我对验证器的问题/要求很小,我目前无法实现。
我有一个简单的TextField(QtQuick.Control 2),它根据状态用不同的颜色装饰。它还应该使用不同的RegExpValidator(使用另一个正则表达式),具体取决于当前状态。
有没有人知道如何在运行时切换/更改/修改RegExpValidator? (例如,当按下PushButton或触发onEditingFinished-Event时)
我当前的qml代码是:
import QtQuick 2.3
import QtQuick.Controls 2.2 as Quick
import QtQuick.Layouts 1.3
RowLayout {
id: layout
property color modeColor: "whitesmoke"
property color modeTextColor: "gray"
spacing: 0
Rectangle {
id: rect
Layout.fillWidth: true
Layout.minimumWidth: 100
Layout.preferredWidth: 100
Layout.maximumWidth: 100
Layout.preferredHeight: layout.implicitHeight
color: modeColor
border.width: 1
border.color: modeColor
Text {
id: recttext
anchors.centerIn: parent
text: "Enter key"
color: modeTextColor
}
}
Quick.TextField {
id: input
Layout.fillWidth: true
placeholderText: "Text"
background: Rectangle {
color: "whitesmoke"
border.width: 1
border.color: modeColor
}
validator: RegExpValidator { regExp: /.*:$/ }
onEditingFinished: {
recttext.text = input.text
if (layout.state == "keyinput") {
layout.state = "valinput"
// should change to another regExp validator
} else {
layout.state = "keyinput"
// should change to another regExp validator
}
input.clear()
}
Keys.onPressed: {
if (event.key == Qt.Key_Escape) {
layout.state = "keyinput"
recttext.text = "Enter key"
input.clear()
}
}
}
states: [
State {
name: "keyinput"
PropertyChanges { target: layout; modeColor: "whitesmoke"; modeTextColor: "gray" }
},
State {
name: "valinput"
PropertyChanges { target: layout; modeColor: "red"; modeTextColor: "white" }
}
]
state: "keyinput"
}
最诚挚的问候,克里斯
编辑: endgoal是输入键值对的输入行(我可以使用特定的RegExpValidator作为特定键。)
此处应使用RegExpValidator输入密钥,例如“作者:”+按Enter键(键输入模式) #
切换到valueinput-mode后,它应该使用另一个RegExpValidator:
顺便说一句。这是一个桌面应用程序:)
答案 0 :(得分:0)
我希望这可以帮到你!
property var valid1 : IntValidator { bottom:0; top: 2000}
property var valid2 : IntValidator { bottom:2000; top: 4000}
...
validator: if(condition) { valid1 }
else { valid2 }