QIntValidator 类提供确保字符串包含指定范围内有效整数的验证器。 更多...
头: | #include <QIntValidator> |
CMake: |
find_package(Qt6 COMPONENTS Gui REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Gui) |
qmake: | QT += gui |
继承: | QValidator |
QIntValidator (int minimum , int maximum , QObject * parent = nullptr) | |
QIntValidator (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);
下文呈现一些验证器范例。在实践中,它们通常关联上文范例中的 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 使用其 locale () 解释数字。例如,在阿拉伯区域设置,QIntValidator 将接受阿拉伯数字。
注意: QLocale::NumberOptions 设置在 locale () 还会影响数字的解释方式。例如,由于 QLocale::RejectGroupSeparator 默认未设置,验证器将接受组分隔符。因此推荐使用 QLocale::toInt () 获得数值。
另请参阅 QDoubleValidator , QRegularExpressionValidator , QLocale::toInt (),和 行编辑范例 .
此特性保持验证器的最低可接受值
By default, this property's value is derived from the lowest signed integer available (typically -2147483647).
访问函数:
int | bottom () const |
void | setBottom (int) |
通知程序信号:
void | bottomChanged (int bottom ) |
另请参阅 setRange ().
此特性保持验证器的最高可接受值
默认情况下,此特性值派生自最高可用有符号整数 (通常为 2147483647)。
访问函数:
int | top () const |
void | setTop (int) |
通知程序信号:
void | topChanged (int top ) |
另请参阅 setRange ().
构造验证器采用 parent ,接受整数从 minimum to maximum 包括在内。
构造验证器采用 parent 对象接受所有整数。
[虚拟]
QIntValidator::
~QIntValidator
()
销毁验证器。
[override virtual]
void
QIntValidator::
fixup
(
QString
&
input
) const
重实现: QValidator::fixup (QString &input) const.
将验证器范围设为仅接受整数介于 bottom and top 包括在内。
[override virtual]
QValidator::State
QIntValidator::
validate
(
QString
&
input
,
int
&
pos
) const
重实现: QValidator::validate (QString &input, int &pos) const.
返回 Acceptable 若 input 是在有效范围内的整数。若 input has at most as many digits as the top of the range, or is a prefix of an integer in the valid range, returns 中间体 。否则,返回 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).
Similarly, if the valid range is between 46 and 53, then 41 and 59 will be evaluated as 中间体 , as otherwise the user wouldn't be able to change a value from 49 to 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 参数并未用于此验证器。