QIntValidator 類

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

頭: #include <QIntValidator>
實例化: IntValidator
繼承: QValidator

特性

公共函數

QIntValidator (QObject * parent = 0)
QIntValidator (int minimum , int maximum , QObject * parent = 0)
~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

額外繼承成員

詳細描述

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 returns Intermediate. Values consisting of a number of digits equal to or less than the max value are considered intermediate. This is intended because the digit that prevents a number to be in range is not necessarily the last digit typed. This also means that an intermediate number can have leading zeros.

可以設置最小和最大值采用一次調用 setRange (),或單獨采用 setBottom () 和 setTop ().

QIntValidator uses its locale () to interpret the number. For example, in Arabic locales, QIntValidator will accept Arabic digits. In addition, QIntValidator is always guaranteed to accept a number formatted according to the "C" locale.

另請參閱 QDoubleValidator , QRegExpValidator ,和 行編輯範例 .

特性文檔編製

bottom : int

This property holds the validator's lowest acceptable value.

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

This property holds the validator's highest acceptable value.

默認情況下,此特性值派生自最高可用有符號整數 (通常為 2147483647)。

訪問函數:

int top () const
void setTop (int)

另請參閱 setRange ().

成員函數文檔編製

QIntValidator:: QIntValidator ( QObject * parent = 0)

構造驗證器采用 parent 對象接受所有整數。

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

構造驗證器采用 parent ,接受整數從 minimum to maximum 包括在內。

QIntValidator:: ~QIntValidator ()

銷毀驗證器。

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

重實現自 QValidator::fixup ().

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

將驗證器範圍設為僅接受整數介於 bottom and top 包括在內。

[虛擬] 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 參數並未用於此驗證器。