参考
https://blog.youkuaiyun.com/u014597198/article/details/52413951
https://www.runoob.com/note/38097
在QML中的TextInput或者TextField下,都有一个validator属性,可以设置正则表达式或者数值验证。该validator不仅有判断的作用,而且还会起到阻止输入的功能。
然后可以通过其acceptableInput属性判断当前输入是否符合需求
TextField {
id: textField_localIp
// inputMethodHints: Qt.ImhDigitsOnly
anchors.margins: defaultMargin
anchors.top: button_read.bottom
anchors.left: textField_line1_plc.left
anchors.leftMargin: 0
validator: RegExpValidator{
regExp: /^(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])$/
}
// validator: IntValidator{bottom: 11; top: 31;}
color: acceptableInput ? "#00ff00" : "#ff0000"
width:300
}