QT中设置QLineEdit只允许输入IP格式的数据,我们可以使用正则表达式,不过在QT5和QT6中有一点点不一样(QT5中是QRegExp
和QRegExpValidator
,QT6中是QRegularExpression
和QRegularExpressionValidator
):
QT5
#include <QRegExp>
#include <QRegExpValidator>
ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("^((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]?)$")));
QT6
#include <QRegularExpression>
#include <QRegularExpressionValidator>
ui->lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^((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]?)$")));
标签:QT6,25,01,QT,IP,QT5,QLineEdit,include
From: https://www.cnblogs.com/chaichengxun/p/17722125.html