QIntValidator 类

QIntValidator class provides a validator that ensures a string contains a valid integer within a specified range. 更多...

头: #include <QIntValidator>
qmake: QT += gui
实例化: IntValidator
继承: QValidator

特性

公共函数

QIntValidator (QObject * parent = Q_NULLPTR)
QIntValidator (int minimum , int maximum , QObject * parent = Q_NULLPTR)
~QIntValidator ()
int bottom () const
void setBottom ( int )
virtual void setRange (int bottom , int top )
void setTop ( int )
int top () const

重实现公共函数

virtual void fixup (QString & input ) const
virtual QValidator::State validate (QString & input , int & pos ) const

额外继承成员

详细描述

QIntValidator class provides a validator that ensures a string contains a valid integer within a specified range.

用法范例:

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);
					

下文呈现一些验证器范例。在实践中,它们通常关联上文范例中的 Widget。

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 返回中间数。由 <= 最大值的数字组成的值,被认为是中间数。这是因为旨在阻止来自范围内数的数字,不一定是最后键入的数字。这还意味着:中间数可以拥有前导 0。

可以设置最小和最大值采用一次调用 setRange (),或单独采用 setBottom () 和 setTop ().

QIntValidator uses its locale () to interpret the number. For example, in Arabic locales, QIntValidator will accept Arabic digits.

注意: QLocale::NumberOptions 设置在 locale () 还会影响数字的解释方式。例如,由于 QLocale::RejectGroupSeparator 默认未设置,验证器将接受组分隔符。因此推荐使用 QLocale::toInt () 获得数值。

另请参阅 QDoubleValidator , QRegExpValidator , QLocale::toInt (),和 行编辑范例 .

特性文档编制

bottom : int

此特性保持验证器的最低可接受值

By default, this property's value is derived from the lowest signed integer available (typically -2147483647).

访问函数:

int bottom () const
void setBottom ( int )

另请参阅 setRange ().

top : int

此特性保持验证器的最高可接受值

默认情况下,此特性值派生自最高可用有符号整数 (通常为 2147483647)。

访问函数:

int top () const
void setTop ( int )

另请参阅 setRange ().

成员函数文档编制

QIntValidator:: QIntValidator ( QObject * parent = Q_NULLPTR)

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

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

构造验证器采用 parent ,接受整数从 minimum to maximum 包含在内。

QIntValidator:: ~QIntValidator ()

销毁验证器。

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

重实现自 QValidator::fixup ().

[virtual] void QIntValidator:: setRange ( int bottom , int top )

将验证器范围设为仅接受整数介于 bottom and top 包含在内。

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

重实现自 QValidator::validate ().

返回 Acceptable input is an integer within the valid range, 中间体 input is a prefix of an integer in the valid range, and Invalid 否则。

If the valid range consists of just positive integers (e.g., 32 to 100) and input is a negative integer, then Invalid is returned. (On the other hand, if the range consists of negative integers (e.g., -100 to -32) and input is a positive integer, then Intermediate is returned, because the user might be just about to type the minus (especially for right-to-left languages).

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 参数并未用于此验证器。