Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex QML中时间输入的文本字段验证_Regex_Qt_Qml_Textfield_Qtquickcontrols2 - Fatal编程技术网

Regex QML中时间输入的文本字段验证

Regex QML中时间输入的文本字段验证,regex,qt,qml,textfield,qtquickcontrols2,Regex,Qt,Qml,Textfield,Qtquickcontrols2,我有一个TextField,允许用户输入时间,我使用了RegValidator进行验证。目前,只要用户单击backspace,我就需要用“0”填充特定位置。代码如下: TextField { id:textField text:"11:11:11" width:200 height:80 font.pointSize: 15 color:"white" inputMask: "99:99:99" validator: RegExpV

我有一个
TextField
,允许用户输入时间,我使用了RegValidator进行验证。目前,只要用户单击backspace,我就需要用“0”填充特定位置。代码如下:

TextField {
    id:textField
    text:"11:11:11"
    width:200
    height:80
    font.pointSize: 15
    color:"white"
    inputMask: "99:99:99"
    validator: RegExpValidator { regExp: /^([0-1\s]?[0-9\s]|2[0-3\s]):([0-5\s][0-9\s]):([0-5\s][0-9\s])$ / }
    horizontalAlignment: Text.AlignHCenter
    verticalAlignment: Text.AlignVCenter
    inputMethodHints: Qt.ImhDigitsOnly
}
当用户单击backspace时

你是说只是按退格键?那么它将是这样的:

TextField {
    ..
    Keys.onBackPressed: text = "00:00:00"
}
编辑

为了只重置光标所在的一个数字,可以执行以下操作。我没有测试它,也许有些指数是错误的,但你明白了

TextField {
    ..
    Keys.onBackPressed: {
        var index = cursorPosition
        var char = text.charAt(index)
        if(char != ":"){
            text = text.substr(0, index) + "0"+ text.substr(index);
        }

    }
}

实际上,您可以使用文本输入的显示文本属性,如下所示:

   TextInput {

      id: textInput

      inputMask: "00:00:00;0"

      onDisplayTextChanged: {

         // when user backspacing or deleting some character,
         // this property become as visible value: "03:01:35"
         // but text property has value: "03:1:35"

         console.log(displayText); 
      }
   }

如果我设置文本“00:00:00”,它会将所有位置替换为“0”。我的问题是,如果光标位于第二个位置,并且单击了BackSpace,我需要在该位置添加一个“0”,以及如何做到这一点?。原因是我不能允许文本字段为“:”:“当所有位置被清除时。