The QIntValidator class provides a validator that ensures a string contains a valid integer within a specified range. 更多...
| 頭: | #include <QIntValidator> |
| qmake: | QT += gui |
| 繼承: | QValidator |
| QIntValidator (QObject * parent = nullptr) | |
| QIntValidator (int minimum , int maximum , QObject * parent = nullptr) | |
| virtual | ~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 override |
| virtual QValidator::State | validate (QString & input , int & pos ) const override |
| const QMetaObject | staticMetaObject |
The 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.
注意: The QLocale::NumberOptions 設置在 locale () 還會影響數字的解釋方式。例如,由於 QLocale::RejectGroupSeparator 默認未設置,驗證器將接受組分隔符。因此推薦使用 QLocale::toInt () 獲得數值。
另請參閱 QDoubleValidator , QRegExpValidator , QLocale::toInt (),和 行編輯範例 .
此特性保持驗證器的最低可接受值
By default, this property's value is derived from the lowest signed integer available (typically -2147483647).
訪問函數:
| int | bottom () const |
| void | setBottom ( int ) |
另請參閱 setRange ().
此特性保持驗證器的最高可接受值
默認情況下,此特性值派生自最高可用有符號整數 (通常為 2147483647)。
訪問函數:
| int | top () const |
| void | setTop ( int ) |
另請參閱 setRange ().
構造驗證器采用 parent 對象接受所有整數。
構造驗證器采用 parent ,接受整數從 minimum to maximum 包括在內。
[虛擬]
QIntValidator::
~QIntValidator
()
銷毀驗證器。
[override virtual]
void
QIntValidator::
fixup
(
QString
&
input
) const
重實現自 QValidator::fixup ().
[虛擬]
void
QIntValidator::
setRange
(
int
bottom
,
int
top
)
將驗證器範圍設為僅接受整數介於 bottom and top 包括在內。
[override 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 參數並未用於此驗證器。