import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 1.15 Column { id: root property alias text: input.text property bool valid: false property bool canEmpty: false property var regularExpression : /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$/ TextField { height: 40 width: 350 id: input selectByMouse: true onTextChanged: { validate() } } Text { id: hint color: "red" font.pixelSize: 12 } function reset() { hint.text = "" } function validate() { if (input.text.length <= 0) { hint.text = root.canEmpty ? "" : "参数不能为空" root.valid = root.canEmpty return root.valid } root.valid = root.regularExpression.test(input.text) if (!root.valid) { hint.text = "参数配置无效" } else { hint.text = "" } return root.valid } }