Qt4.7 QML TextInput空状态检测
我有以下Qt4.7 QML TextInput空状态检测,qml,qt-quick,qtdeclarative,Qml,Qt Quick,Qtdeclarative,我有以下TextInput元素: TextInput { id: textInput text: m_init anchors.centerIn: parent font.family : "Helvetica" font.pixelSize: 14 color: "black" maximumLength: 2 smooth: true inputMask: "HH" states : [ Sta
TextInput
元素:
TextInput {
id: textInput
text: m_init
anchors.centerIn: parent
font.family : "Helvetica"
font.pixelSize: 14
color: "black"
maximumLength: 2
smooth: true
inputMask: "HH"
states : [
State {
name: "EmptyInputLeft"
when: !text.length
PropertyChanges {
target: textInput
text : "00"
}
}
]
}
当所有内容都被退格删除时,我想显示00
。为此,我编写了一个状态
,但它没有按预期工作。我做错了什么?您在上面的代码中检测到“QML TextInput:绑定循环”属性“text”错误。原因是当您将文本设置为“00”时,长度会发生变化,这会再次触发“when”子句(并再次设置),并导致循环错误
下面是一个使用验证器的解决方案:
TextInput {
id: textInput
text: m_init
anchors.centerIn: parent
font.family : "Helvetica"
font.pixelSize: 14
color: "black"
maximumLength: 2
smooth: true
inputMask: "00"
//validator: RegExpValidator { regExp: /^([0-9]|0[0-9]|1[0-9]|2[0-3])/ } //max 23 hours
validator: RegExpValidator {
regExp: /^([0-9]|[0-9]/ } //any two digits
}
或者将文本绑定到函数:
text: myFunction()
结合ContextChanged事件,可以产生更好的结果:
onTextChanged: {
//some code
myFunction()
}
function myFunction(){
//some validation
return "00"
}
对于TextField,有这样的属性-如下所示使用它:
TextField {
id: textInput
text: m_init
placeholderText: "00"
...
}