QIntValidator 類

QIntValidator 類提供確保字符串包含指定範圍內有效整數的驗證器。 更多...

頭: #include <QIntValidator>
qmake: QT += gui
繼承: QValidator

特性

公共函數

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

信號

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 將接受阿拉伯數字。

注意: The QLocale::NumberOptions 設置在 locale () 還會影響數字的解釋方式。例如,由於 QLocale::RejectGroupSeparator 默認未設置,驗證器將接受組分隔符。因此推薦使用 QLocale::toInt () 獲得數值。

另請參閱 QDoubleValidator , QRegExpValidator , QLocale::toInt (),和 行編輯範例 .

特性文檔編製

bottom : int

此特性保持驗證器的最低可接受值

默認情況下,此特性的值派生自可用最低有符號整數 -2147483648。

訪問函數:

int bottom () const
void setBottom ( int )

通知程序信號:

void bottomChanged (int bottom )

另請參閱 setRange ().

top : int

此特性保持驗證器的最高可接受值

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

訪問函數:

int top () const
void setTop ( int )

通知程序信號:

void topChanged (int top )

另請參閱 setRange ().

成員函數文檔編製

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

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

QIntValidator:: QIntValidator ( QObject * parent = nullptr)

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

[虛擬] QIntValidator:: ~QIntValidator ()

銷毀驗證器。

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

重實現: QValidator::fixup (QString &input) const.

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

將驗證器範圍設為僅接受整數介於 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 參數並未用於此驗證器。