The QSpinBox 類提供自鏇框 Widget。 更多...
| 頭: | #include <QSpinBox> |
| 繼承: | QAbstractSpinBox |
|
| QSpinBox (QWidget * parent = 0) | |
| QString | cleanText () const |
| int | maximum () const |
| int | minimum () const |
| QString | prefix () const |
| void | setMaximum (int max ) |
| void | setMinimum (int min ) |
| void | setPrefix (const QString & prefix ) |
| void | setRange (int minimum , int maximum ) |
| void | setSingleStep (int val ) |
| void | setSuffix (const QString & suffix ) |
| int | singleStep () const |
| QString | suffix () const |
| int | value () const |
| void | setValue (int val ) |
| void | valueChanged (int i ) |
| void | valueChanged (const QString & text ) |
| virtual QString | textFromValue (int value ) const |
| virtual int | valueFromText (const QString & text ) const |
| virtual bool | event (QEvent * event ) |
| virtual void | fixup (QString & input ) const |
| virtual QValidator::State | validate (QString & text , int & pos ) const |
The QSpinBox 類提供自鏇框 Widget。
QSpinBox is designed to handle integers and discrete sets of values (e.g., month names); use QDoubleSpinBox 為浮點值。
QSpinBox allows the user to choose a value by clicking the up/down buttons or pressing up/down on the keyboard to increase/decrease the value currently displayed. The user can also type the value in manually. The spin box supports integer values but can be extended to use different strings with validate (), textFromValue () 和 valueFromText ().
Every time the value changes QSpinBox 發射 valueChanged () signals. The current value can be fetched with value () 和設置采用 setValue ().
點擊嚮上/嚮下按鈕 (或使用鍵盤加速鍵的嚮上和嚮下箭頭) 將遞增 (或遞減) 當前值,按步幅大小 singleStep ()。若想要改變此行為,可以重實現虛函數 stepBy ()。最小和最大值及步幅大小的設置可以使用某個構造函數,且稍後可以改變采用 setMinimum (), setMaximum () 和 setSingleStep ().
Most spin boxes are directional, but QSpinBox can also operate as a circular spin box, i.e. if the range is 0-99 and the current value is 99, clicking "up" will give 0 if wrapping () 被設為 true。使用 setWrapping () 若想要循環行為。
可以為顯示值前置和追加任意字符串指示,例如,貨幣 (或度量單位)。見 setPrefix () 和 setSuffix ()。自鏇框文本的檢索是采用 text () (包括任何 prefix () 和 suffix ()),或采用 cleanText () (沒有 prefix (),沒有 suffix () 且沒有前導或結尾空白)。
經常期望對用戶給齣特殊 (常常默認) 選擇,除數值範圍外。見 setSpecialValueText () for how to do this with QSpinBox .
|
A spin box shown in the Windows XP widget style . |
|
A spin box shown in the Plastique widget style . |
|
A spin box shown in the Macintosh widget style . |
若使用 prefix (), suffix (),和 specialValueText () don't provide enough control, you subclass QSpinBox 並重實現 valueFromText () 和 textFromValue ()。例如,這裏是允許用戶錄入圖標大小 (如 32 x 32),用於自定義自鏇框的代碼:
int IconSizeSpinBox::valueFromText(const QString &text) const { QRegExp regExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?")); if (regExp.exactMatch(text)) { return regExp.cap(1).toInt(); } else { return 0; } } QString IconSizeSpinBox::textFromValue(int value) const { return tr("%1 x %1").arg(value); }
見 圖標 範例瞭解完整源代碼。
另請參閱 QDoubleSpinBox , QDateTimeEdit , QSlider ,和 自鏇框範例 .
此特性保持自鏇框的文本,不包括任何前綴、後綴、或前導/結尾空白。
訪問函數:
| QString | cleanText () const |
另請參閱 text , QSpinBox::prefix ,和 QSpinBox::suffix .
This property holds the maximum value of the spin box.
當設置此特性 minimum 有必要調節,以確保範圍仍然有效。
默認最大值為 99。
訪問函數:
| int | maximum () const |
| void | setMaximum (int max ) |
另請參閱 setRange () 和 specialValueText .
This property holds the minimum value of the spin box.
當設置此特性 maximum 有必要調節,以確保範圍仍然有效。
默認最小值為 0。
訪問函數:
| int | minimum () const |
| void | setMinimum (int min ) |
另請參閱 setRange () 和 specialValueText .
This property holds the spin box's prefix.
前綴會前置到顯示值開頭。典型用途是顯示度量單位 (或貨幣符號)。例如:
sb->setPrefix("$");
要關閉前綴顯示,把此特性設為空字符串。默認為無前綴。前綴不顯示,當 value () == minimum () 和 specialValueText () 有設置。
prefix() 返迴空字符串,若未設置前綴。
訪問函數:
| QString | prefix () const |
| void | setPrefix (const QString & prefix ) |
另請參閱 suffix (), setSuffix (), specialValueText (),和 setSpecialValueText ().
This property holds the step value.
當用戶使用箭頭改變自鏇框值時,值將按 singleStep 數量遞增/遞減。默認值為 1。將 singleStep 值設為小於 0,什麼都不做。
訪問函數:
| int | singleStep () const |
| void | setSingleStep (int val ) |
This property holds the suffix of the spin box.
後綴會被追加到顯示值末尾。典型用法是顯示度量單位 (或貨幣符號)。例如:
sb->setSuffix(" km");
要關閉後綴顯示,將此特性設為空字符串。默認無後綴。後綴不顯示對於 minimum () 若 specialValueText () 有設置。
若未設置後綴,suffix() 返迴空字符串。
訪問函數:
| QString | suffix () const |
| void | setSuffix (const QString & suffix ) |
另請參閱 prefix (), setPrefix (), specialValueText (),和 setSpecialValueText ().
This property holds the value of the spin box.
setValue() will emit valueChanged() if the new value is different from the old one.
訪問函數:
| int | value () const |
| void | setValue (int val ) |
通知程序信號:
| void | valueChanged (int i ) |
| void | valueChanged (const QString & text ) |
構造自鏇框采用 0 作為最小值和 99 作為最大值,步幅值 1。初始值被設為 0。父級為 parent .
另請參閱 setMinimum (), setMaximum (),和 setSingleStep ().
[virtual protected]
bool
QSpinBox::
event
(
QEvent
*
event
)
重實現自 QObject::event ().
[virtual protected]
void
QSpinBox::
fixup
(
QString
&
input
) const
重實現自 QAbstractSpinBox::fixup ().
方便函數能設置 minimum ,和 maximum 值采用單函數調用。
setRange(minimum, maximum);
相當於:
setMinimum(minimum); setMaximum(maximum);
[virtual protected]
QString
QSpinBox::
textFromValue
(
int
value
) const
此虛函數用於自鏇框,每當它需要顯示給定 value 。默認實現返迴的字符串包含 value 會以標準方式打印使用 QWidget::locale ().toString(), but with the thousand separator removed. Reimplementations may return anything. (See the example in the detailed description.)
注意: QSpinBox 不會調用此函數對於 specialValueText () 且 prefix () 或 suffix () 應包括在返迴值中。
若重實現這,還可能需要重實現 valueFromText () 和 validate ()
另請參閱 valueFromText (), validate (),和 QLocale::groupSeparator ().
[virtual protected]
QValidator::State
QSpinBox::
validate
(
QString
&
text
,
int
&
pos
) const
重實現自 QAbstractSpinBox::validate ().
[virtual protected]
int
QSpinBox::
valueFromText
(const
QString
&
text
) const
此虛函數用於自鏇框,每當它需要解釋 text 由用戶作為值鍵入。
需要以非數字方式顯示自鏇框值的子類,需要重實現此函數。
注意: QSpinBox 處理 specialValueText () 單獨;此函數隻關心其它值。
另請參閱 textFromValue () 和 validate ().