在QML中使用InputMask
我有一个小的显示窗口,应该可以容纳输入掩码,如“0000.00”。单击0到9之间的数字按钮时,显示屏应显示相应的数字,例如:0345.89 这是我的密码:在QML中使用InputMask,qml,Qml,我有一个小的显示窗口,应该可以容纳输入掩码,如“0000.00”。单击0到9之间的数字按钮时,显示屏应显示相应的数字,例如:0345.89 这是我的密码: TextInput { id: displayNumbers6Text cursorVisible: true focus: true validator: RegExpValidator { regExp: /[0-9]+/ } inputMask: "0000.00"
TextInput {
id: displayNumbers6Text
cursorVisible: true
focus: true
validator: RegExpValidator {
regExp: /[0-9]+/
}
inputMask: "0000.00"
maximumLength: 7
}
对于按钮:
Button {
width: parent.width*0.3
height: parent.height*0.15
buttonColor: "#000000"
MouseArea {
anchors.fill: parent
onClicked: {
displayNumbers6Text.text = "1"
}
}
}
但它不起作用。我做错了什么?当您指定
text
属性时,您将该值重置为“1”。
如果您尝试在单击按钮时插入值,则可以尝试以下操作:
import QtQuick 2.0
Rectangle {
width: 360
height: 200
TextInput {
id: displayNumbers6Text
width: parent.width
height: parent.height * 0.5
cursorVisible: true
focus: true
validator: DoubleValidator {
}
inputMask: "0000.00"
maximumLength: 7
function appendNum(num) {
var dotPos = text.indexOf(".");
if (dotPos == 4)
text += num;
else
text = text.substr(0, dotPos) + num + text.substr(dotPos);
}
}
Rectangle {
width: parent.width*0.3
height: parent.height*0.15
anchors.top: displayNumbers6Text.bottom
color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
console.log(displayNumbers6Text.text);
displayNumbers6Text.appendNum("1");
}
}
}
}
- 您可以使用DoubleValidator而不是一般的RegexValidator,它将更具可读性
- 我在
中添加了一个appendNum函数,以便在文本字符串的末尾插入一个值。请注意,由于设置了TextInput
,因此点字符始终位于字符串中inputMask