介绍
TextField 是Qt Quick Components 中用于输入和显示单行文本的控件,通过对其属性的设置,可以实现一些特定的功能,如对输入文本的限制,拼写检查等。下文将具体介绍一些特定功能的实现。
特定功能的具体实现TextField实现密码输入
需要设置其echoMode属性和inputMethodHints属性,具体QML代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
placeholderText: "Password field"
echoMode: TextInput.Password
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
}
TextField实现输入字符的限制
需要设置其validator 属性,具体QML代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
placeholderText: "Value between 0 and 100 (mandatory field)"
validator: IntValidator{bottom: 0; top: 100;}
inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText
}
注:输入的字符只能为0到100的整数TextField实现拼写检查
需要设置其errorHighligh属性,具体代码如下:
TextArea
{
anchors {left: parent.left; right: parent.right;}
placeholderText: "Error highlight when more than 5 characters"
errorHighlight: text.length > 5
}
注: 当输入字符长度大于5,并且有拼写错误时,TextArea将显示红框
TextField实现输入无联想记忆
需要设置其inputMethodHints属性,具体代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhNoPredictiveText
}
TextField实现默认输入法为数字
需要设置其inputMethodHints属性,具体代码如下
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText
}
TextField实现默认输入法为大写字母
需要设置其inputMethodHints属性,具体代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhUppercaseOnly
}
TextField实现EMAIL输入
需要设置其inputMethodHints属性,具体代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhEmailCharactersOnly | Qt.ImhNoPredictiveText
}
TextField实现PhoneNumber输入
需要设置其inputMethodHints属性,具体代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhDialableCharactersOnly | Qt.ImhNoPredictiveText
}
TextField实现URL输入
需要设置其inputMethodHints属性,具体代码如下:
TextField{
anchors {left: parent.left; right: parent.right;}
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhUrlCharactersOnly | Qt.ImhNoPredictiveText
}