QIntValidator Class

QIntValidator 类提供了一个验证器,可确保字符串包含指定范围内的有效整数。更多

Header: #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

信号

void bottomChanged(int bottom)
void topChanged(int top)

详细说明

使用示例

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 返回中间值。由等于或小于最大值的数字组成的值被视为中间值。这样做的目的是,导致数字不在范围内的数字不一定是最后输入的数字。这也意味着中间数可以有前导零。

最小值和最大值可通过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)

Notifier 信号:

void bottomChanged(int bottom)

另请参见 setRange().

top : int

此属性保存验证器的最高可接受值

默认情况下,该属性的值取自可用的最高有符号整数(2147483647)。

访问功能:

int top() const
void setTop(int)

Notifier 信号:

void topChanged(int top)

另请参阅 setRange().

成员函数文档

[explicit] QIntValidator::QIntValidator(QObject *parent = nullptr)

使用parent 对象构造验证器,该对象接受所有整数。

QIntValidator::QIntValidator(int minimum, int maximum, QObject *parent = nullptr)

构造一个parent 的验证器,它接受从minimummaximum (含)的整数。

[virtual noexcept] QIntValidator::~QIntValidator()

销毁验证器。

[override virtual] void QIntValidator::fixup(QString &input) const

重实现:QValidator::fixup(QString &input) const.

void QIntValidator::setRange(int bottom, int top)

设置验证器的范围,使其只接受bottomtop (含)之间的整数。

[override virtual] QValidator::State QIntValidator::validate(QString &input, int &pos) const

重实现:QValidator::validate(QString &input, int &pos) const.

如果input 是有效范围内的整数,则返回Acceptable 。如果input 的位数与范围顶端的位数相同,或者是有效范围内整数的前缀,则返回Intermediate 。否则,返回Invalid

如果有效范围内只有正整数(如 32 到 100),而input 是负整数,则返回无效。(另一方面,如果有效范围包括负整数(如 -100 至 -32),而input 是一个不带前导加号的正整数,则返回中间值,因为用户可能正要键入减号(特别是对于从右向左的语言)。

同样,如果有效范围介于 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 参数。

© 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.