TG-PlatformPlus/qml/common/QxRegTextField.qml

82 lines
2.1 KiB
QML
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import QtQuick
import QtQuick.Controls
//import QtQuick.VirtualKeyboard 2.15
TextField {
id: control
placeholderText:qsTr("XX XX XX")
// font.pointSize: 13
font.family: "微软雅黑"
placeholderTextColor: "#A5A5A5"
color: "#1D2129"
focus: true
selectByMouse: true
// validator: RegExpValidator { // 使用正则表达式验证器
// regExp: /[0-9A-F]+/ // 只能输入0~9的整数和AF的大写字母
// }
background: Rectangle {
implicitWidth: 330
implicitHeight: 30
color: "#F2F3F5"
border.color: "#0068B4"
}
property bool isDelete: false
property bool isBack: false
property bool isPaste: false
// maximumLength: 29//200
Keys.onPressed: {
isBack = (event.key === Qt.Key_Backspace)
isDelete = (event.key === Qt.Key_Delete)
isPaste = (event.modifiers === Qt.ControlModifier && event.key === Qt.Key_V)
event.accepted = (event.text===" ") //过滤键盘输入空格
}
onTextChanged: {
if(isPaste){
control.text = checkFormat(control.text)
return
}
let temp = control.text
if(!isBack&&!isDelete){
var position = cursorPosition
if(length === 2)
{
temp += " "
}
else if(length > 3 && (length - 2) % 3 === 0){
temp += " "
}
else
{
position += 1
}
temp = checkFormat(temp)
control.text = temp
select(position,position)
}
isBack = false
isDelete = false
isPaste = false
}
function checkFormat(text){
text = text.replace(/[^0-9a-f]/gi, '');
let upertext = text.toUpperCase();
let newText = upertext.split(" ").join("")
var index = 0
while(index < newText.length){
if((index+1) %3 === 0){
newText = newText.slice(0, index) + " " + newText.slice(index)
index++
}
index++
}
return newText
}
}