QRegularExpressionValidator Class
QRegularExpressionValidator 类用于根据正则表达式检查字符串。更多
头文件: | #include <QRegularExpressionValidator> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
继承: | QValidator |
属性
- regularExpression : QRegularExpression
公共函数
QRegularExpressionValidator(QObject *parent = nullptr) | |
QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr) | |
virtual | ~QRegularExpressionValidator() |
QRegularExpression | regularExpression() const |
重新实现的公共函数
virtual QValidator::State | validate(QString &input, int &pos) const override |
公共插槽
void | setRegularExpression(const QRegularExpression &re) |
信号
void | regularExpressionChanged(const QRegularExpression &re) |
详细说明
QRegularExpressionValidator 使用正则表达式(regexp)确定输入字符串是Acceptable 、Intermediate 还是Invalid 。regexp 可以在构建 QRegularExpressionValidator 时提供,也可以稍后提供。
如果 regexp 与字符串部分匹配,结果将被视为Intermediate 。例如,对于 regexp[A-Z][0-9],""和 "A"是Intermediate (而"_"则是Invalid )。
QRegularExpressionValidator 会自动将正则表达式封装在\\A
和\\z
锚点中;换句话说,它会始终尝试进行精确匹配。
使用示例
// regexp: optional '-' followed by between 1 and 3 digits QRegularExpression rx("-?\\d{1,3}"); QValidator *validator = new QRegularExpressionValidator(rx, this); QLineEdit *edit = new QLineEdit(this); edit->setValidator(validator);
下面我们将介绍一些验证器示例。在实践中,它们通常会与上面示例中的 widget 关联。
// integers 1 to 9999 QRegularExpression re("[1-9]\\d{0,3}"); // the validator treats the regexp as "^[1-9]\\d{0,3}$" QRegularExpressionValidator v(re, 0); QString s; int pos = 0; s = "0"; v.validate(s, pos); // returns Invalid s = "12345"; v.validate(s, pos); // returns Invalid s = "1"; v.validate(s, pos); // returns Acceptable re.setPattern("\\S+"); // one or more non-whitespace characters v.setRegularExpression(re); s = "myfile.txt"; v.validate(s, pos); // Returns Acceptable s = "my file.txt"; v.validate(s, pos); // Returns Invalid // A, B or C followed by exactly five digits followed by W, X, Y or Z re.setPattern("[A-C]\\d{5}[W-Z]"); v.setRegularExpression(re); s = "a12345Z"; v.validate(s, pos); // Returns Invalid s = "A12345Z"; v.validate(s, pos); // Returns Acceptable s = "B12"; v.validate(s, pos); // Returns Intermediate // match most 'readme' files re.setPattern("read\\S?me(\\.(txt|asc|1st))?"); re.setPatternOptions(QRegularExpression::CaseInsensitiveOption); v.setRegularExpression(re); s = "readme"; v.validate(s, pos); // Returns Acceptable s = "README.1ST"; v.validate(s, pos); // Returns Acceptable s = "read me.txt"; v.validate(s, pos); // Returns Invalid s = "readm"; v.validate(s, pos); // Returns Intermediate
另请参阅 QRegularExpression,QIntValidator, 和QDoubleValidator 。
属性文档
regularExpression : QRegularExpression
该属性包含用于验证的正则表达式
默认情况下,该属性包含一个空模式的正则表达式(因此可匹配任何字符串)。
访问功能:
QRegularExpression | regularExpression() const |
void | setRegularExpression(const QRegularExpression &re) |
Notifier 信号:
void | regularExpressionChanged(const QRegularExpression &re) |
成员函数文档
[explicit]
QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent = nullptr)
用parent 对象构造验证器,该对象接受任何字符串(包括空字符串)为有效字符串。
[explicit]
QRegularExpressionValidator::QRegularExpressionValidator(const QRegularExpression &re, QObject *parent = nullptr)
使用parent 对象构造验证器,该验证器接受与正则表达式re 匹配的所有字符串。
[virtual noexcept]
QRegularExpressionValidator::~QRegularExpressionValidator()
销毁验证器。
[override virtual]
QValidator::State QRegularExpressionValidator::validate(QString &input, int &pos) const
重实现:QValidator::validate(QString &input, int &pos) const.
如果input 与此验证器的正则表达式匹配,则返回Acceptable ;如果部分匹配(即如果添加其他有效字符,则可能是有效匹配),则返回Intermediate ;如果input 未匹配,则返回Invalid 。
如果input 未匹配,则pos 参数会被设置为input 参数的长度;否则,它不会被修改。
例如,如果正则表达式是\w\d\d(word-character,digit,digit),那么 "A57 "就是Acceptable ,"E5 "就是Intermediate ,"+9 "就是Invalid 。
另请参见 QRegularExpression::match() 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.