QLineEdit使用正则表达式限制输入IP
代码如下:
QRegExp rx("\b(?😦?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b");
ui->lineedit_IP->setValidator(new QRegExpValidator(rx, parent));
ui->lineedit_IP->setInputMask(“000.000.000.000”);
注意:
有些文章在setInputMask(“000.000.000.000”)在000.000.000.000后面多增加了一个;0用于占位setInputMask(“000.000.000.000;0”),但是在实际项目中,显示不友好,推荐
setInputMask(“000.000.000.000”);