import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
// 时间格式有限制 "2022-01-23 12:45:32"
Rectangle {
width: parent.width
height: parent.height
TextField{
id:textEdit
width: parent.width
height: parent.height
maximumLength: 19
selectByMouse: true
text:"2022-01-23 12:45:32"//
validator: RegExpValidator{regExp:/^\d{4}-\d{2}-\d{2} ([0-1]?[0-9]|2[0-3]):([0-5]?[0-9]?):([0-5]?[0-9]?)$/}
property string currentText: textEdit.text
style:TextFieldStyle{
background: Rectangle{
radius: 2
width: 200
height: 25
}
}
onTextChanged: {
if(calculationTimes(textEdit.text) != 2)
{
textEdit.text = currentText
return
}
currentText = textEdit.text
}
onSelectedTextChanged: { //选中事件
if(textEdit.selectionStart < 11)
textEdit.select(11,12)
else if(textEdit.selectedText.toString().indexOf(":")!= -1)
{
textEdit.select(11,13)
}
}
}
//计算':'出现的次数
function calculationTimes(str){
var index = str.indexOf(':');
var num = 0;
while(index !== -1) {
num++;
index = str.indexOf(':',index + 1);
}
return num
}
}