The QValidator class provides validation of input text. 更多...
| 頭: | #include <QValidator> |
| qmake: | QT += gui |
| 繼承: | QObject |
| 繼承者: | QDoubleValidator , QIntValidator , QRegExpValidator ,和 QRegularExpressionValidator |
| enum | State { Invalid, Intermediate, Acceptable } |
| QValidator (QObject * parent = nullptr) | |
| virtual | ~QValidator () |
| virtual void | fixup (QString & input ) const |
| QLocale | locale () const |
| void | setLocale (const QLocale & locale ) |
| virtual QValidator::State | validate (QString & input , int & pos ) const = 0 |
| void | changed () |
| const QMetaObject | staticMetaObject |
The QValidator class provides validation of input text.
類本身是抽象的。2 個子類 QIntValidator and QDoubleValidator ,提供基本的數值範圍校驗,和 QRegExpValidator 使用自定義正則錶達式提供一般校驗。
If the built-in validators aren't sufficient, you can subclass QValidator . The class has two virtual functions: validate () 和 fixup ().
validate () 必須由每個子類來實現。它返迴 Invalid , 中間體 or Acceptable 取決於其自變量是否有效 (對於有效的子類定義而言)。
這 3 種狀態需要一些解釋。 Invalid 字符串是 clearly 無效的。 中間體 is less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited). QValidator 定義 中間體 作為字符串特性,作為最終結果這既不明顯無效也不可接受。 Acceptable 意味著字符串可以作為最終結果被接受。有人可能會說,任何字符串都是閤理中間狀態,在輸入期間 Acceptable 字符串是 中間體 .
這裏是一些範例:
fixup () 是為可以修理某些用戶錯誤的驗證器而提供的。默認實現什麼都不做。 QLineEdit ,例如,會調用 fixup () 若用戶按下 Enter 鍵 (或 Return 鍵) 且內容目前無效。這允許 fixup () 函數有機會履行一些魔法操作以使 Invalid string Acceptable .
驗證器擁有區域設置,設置采用 setLocale ()。通常使用它來剖析本地化數據。例如, QIntValidator and QDoubleValidator 使用它來剖析整數和雙精度數的本地化錶示。
QValidator is typically used with QLineEdit , QSpinBox and QComboBox .
另請參閱 QIntValidator , QDoubleValidator , QRegExpValidator ,和 行編輯範例 .
此枚舉類型定義可以存在經過驗證的字符串狀態。
| 常量 | 值 | 描述 |
|---|---|---|
QValidator::Invalid
|
0
|
字符串是 clearly 無效的。 |
QValidator::Intermediate
|
1
|
字符串是閤理的中間體值。 |
QValidator::Acceptable
|
2
|
字符串是可接受的最終結果;即:它是有效的。 |
設置驗證器。 parent 參數被傳遞給 QObject 構造函數。
[虛擬]
QValidator::
~QValidator
()
銷毀驗證器,釋放使用的任何存儲和其它資源。
[signal]
void
QValidator::
changed
()
此信號發射,當可能影響字符串有效性的任何特性改變時。
[虛擬]
void
QValidator::
fixup
(
QString
&
input
) const
此函數試圖改變 input 為有效根據此驗證器的規則。它不需要産生有效字符串:此函數的調用者之後必須重新測試;默認什麼都不做。
此函數的重實現可以改變 input 即使沒有産生有效字符串。例如,ISBN 驗證器可能想要刪除除數字和 - 外的每個字符,即使結果仍是無效 ISBN;姓氏驗證器可能想要從字符串開頭和結尾移除空格,即使結果字符串不在接受姓氏列錶中。
返迴用於驗證器的區域設置。默認情況下,初始區域設置如同 QLocale()。
另請參閱 setLocale () 和 QLocale::QLocale ().
設置 locale 將用於驗證器。除非已調用 setLocale,否則驗證器將使用默認區域設置,設置采用 QLocale::setDefault ()。若默認區域設置尚未設置,則它是操作係統的區域設置。
另請參閱 locale () 和 QLocale::setDefault ().
[pure virtual]
QValidator::State
QValidator::
validate
(
QString
&
input
,
int
&
pos
) const
此虛函數返迴 Invalid if input 是無效的根據此驗證器規則, 中間體 若可能的話,稍微多做一些編輯將使輸入可接受 (如:用戶在接受 10 至 99 的整數的 Widget 中鍵入 4),及 Acceptable 若輸入有效。
函數可以改變 input and pos (光標位置) 若有要求。