import QtQuick import QtQuick.Controls import "../common" // import "../Style" Rectangle { width: 300 height: 300 border.width: 1 border.color: "lightgray" clip: true property alias _te: control TextEdit { id: control width: parent.width - 10 height: parent.height anchors.horizontalCenter: parent.horizontalCenter // font.pointSize: 13 font.family: "微软雅黑" font.weight: Font.ExtraLight wrapMode: TextEdit.WrapAnywhere color: "#1D2129" property var oldposition: 0 property bool isDelete: false property bool isBack: false property bool isPaste: false focus: true selectByMouse: true Keys.onPressed: { isDelete = (event.key === Qt.Key_Delete) isBack = (event.key === Qt.Key_Backspace) isPaste = (event.modifiers === Qt.ControlModifier && event.key === Qt.Key_V) event.accepted = (event.text===" " || event.text==="\n") //过滤键盘输入' ' } onTextChanged: { if(isPaste){ control.text = checkFormat(control.text) control.cursorPosition = control.text.length return } let temp = control.text if(!isBack&&!isDelete){ var position = control.cursorPosition if(length === 2) { temp += " " } else if(length > 3 && (length - 2) % 3 === 0){ temp += " " } else { position += 1 } temp = checkFormat(temp) var holdPosition = false if(temp.length < control.length) holdPosition = true control.text = temp control.cursorPosition = 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 } }