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 。正则表达式既可以在创建 QRegularExpressionValidator 时提供,也可以在后续时间提供。
如果正则表达式与字符串部分匹配,则结果被视为Intermediate 。例如,对于正则表达式[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);下面我们列举了一些验证器的示例。实际应用中,它们通常会像上例那样与某个控件相关联。
// 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) |
通知信号:
| 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 (单词字符, 数字, 数字),则“A57”是Acceptable ,“E5”是Intermediate ,“+9”是Invalid 。
另请参阅 QRegularExpression::match()。
© 2026 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.