QIntValidator Class
QIntValidator 类提供了一个验证器,用于确保字符串中包含指定范围内的有效整数。更多内容...
| 头文件: | #include <QIntValidator> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 继承自: | QValidator |
属性
公共函数
| QIntValidator(QObject *parent = nullptr) | |
| QIntValidator(int minimum, int maximum, QObject *parent = nullptr) | |
| virtual | ~QIntValidator() |
| int | bottom() const |
| void | setBottom(int) |
| void | setRange(int bottom, int top) |
| void | setTop(int) |
| int | top() const |
重新实现的公共函数
| virtual void | fixup(QString &input) const override |
| virtual QValidator::State | validate(QString &input, int &pos) const override |
详细说明
使用示例:
QValidator *validator = new QIntValidator(100, 999, this);
QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);下面我们列举了一些验证器的示例。在实际应用中,它们通常会像上例那样与某个控件相关联。
QString str;
int pos = 0;
QIntValidator v(100, 900, this);
str = "1";
v.validate(str, pos); // returns Intermediate
str = "012";
v.validate(str, pos); // returns Intermediate
str = "123";
v.validate(str, pos); // returns Acceptable
str = "678";
v.validate(str, pos); // returns Acceptable
str = "999";
v.validate(str, pos); // returns Intermediate
str = "1234";
v.validate(str, pos); // returns Invalid
str = "-123";
v.validate(str, pos); // returns Invalid
str = "abc";
v.validate(str, pos); // returns Invalid
str = "12cm";
v.validate(str, pos); // returns Invalid请注意,999 函数返回的是“Intermediate”状态。由位数小于或等于最大值的数字组成的数值将被视为“Intermediate”。这是有意为之,因为导致数字超出范围的位数未必是最后输入的那一位。这也意味着“Intermediate”状态的数字可以带有前导零。
最小值和最大值可以通过一次调用setRange() 来设置,也可以分别通过setBottom() 和setTop() 进行设置。
QIntValidator 使用其locale() 来解释数字。例如,在阿拉伯语区域设置中,QIntValidator 将接受阿拉伯数字。
注意: 在locale()中设置的 QLocale::NumberOptions 也会影响数字的解析方式。例如,由于QLocale::RejectGroupSeparator 默认未设置,验证器将接受分组分隔符。因此建议使用QLocale::toInt() 来获取数值。
另请参阅 QDoubleValidator 、QRegularExpressionValidator 、QLocale::toInt() 以及“行编辑示例”。
属性文档
bottom : int
该属性存储了验证器可接受的最低值
默认情况下,该属性的值源自可用的最小有符号整数(-2147483648)。
访问函数:
| int | bottom() const |
| void | setBottom(int) |
另请参阅 setRange()。
top : int
该属性存储验证器可接受的最高值
默认情况下,该属性的值由可用的最大有符号整数(2147483647)推导而来。
访问函数:
| int | top() const |
| void | setTop(int) |
另请参阅 setRange()。
成员函数文档
[explicit] QIntValidator::QIntValidator(QObject *parent = nullptr)
使用parent 对象构建一个验证器,该验证器接受所有整数。
QIntValidator::QIntValidator(int minimum, int maximum, QObject *parent = nullptr)
创建一个基于parent 的验证器,该验证器接受从minimum 到maximum (含)范围内的整数。
[virtual noexcept] QIntValidator::~QIntValidator()
销毁该验证器。
[override virtual] void QIntValidator::fixup(QString &input) const
重新实现了:QValidator::fixup(QString &input) const。
void QIntValidator::setRange(int bottom, int top)
将验证器的范围设置为仅接受bottom 到top (含)之间的整数。
[override virtual] QValidator::State QIntValidator::validate(QString &input, int &pos) const
重写:QValidator::validate(QString &input, int &pos) const。
若input 是一个处于有效范围内的整数,则返回Acceptable 。若input 的位数不超过该范围的上限,或者它是有效范围内某个整数的前缀,则返回Intermediate 。否则,返回Invalid 。
input 如果有效范围仅由正整数组成(例如 32 到 100),且input 是负整数,则返回 Invalid。 (另一方面,如果范围由负整数组成(例如 -100 到 -32),且 是没有前导正号的正整数,则返回 Intermediate,因为用户可能正准备输入减号(特别是对于从右到左书写的语言)。
同样,如果有效范围在 46 到 53 之间,则 41 和 59 将被评估为Intermediate ,否则用户将无法将值从 49 更改为 51。
int pos = 0;
s = "abc";
v.validate(s, pos); // returns Invalid
s = "5";
v.validate(s, pos); // returns Intermediate
s = "50";
v.validate(s, pos); // returns Acceptable默认情况下,此验证器不使用pos 参数。
© 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.