QSpinBox 类

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 .

Screenshot of a Windows XP spin box A spin box shown in the Windows XP widget style .
Screenshot of a Plastique spin box A spin box shown in the Plastique widget style .
Screenshot of a Macintosh spin box A spin box shown in the Macintosh widget style .

子类化 QSpinBox

若使用 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 ,和 自旋框范例 .

特性文档编制

cleanText : const QString

此特性保持自旋框的文本,不包括任何前缀、后缀、或前导/结尾空白。

访问函数:

QString cleanText () const

另请参阅 text , QSpinBox::prefix ,和 QSpinBox::suffix .

maximum : int

This property holds the maximum value of the spin box.

当设置此特性 minimum 有必要调节,以确保范围仍然有效。

默认最大值为 99。

访问函数:

int maximum () const
void setMaximum (int max )

另请参阅 setRange () 和 specialValueText .

minimum : int

This property holds the minimum value of the spin box.

当设置此特性 maximum 有必要调节,以确保范围仍然有效。

默认最小值为 0。

访问函数:

int minimum () const
void setMinimum (int min )

另请参阅 setRange () 和 specialValueText .

prefix : QString

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 ().

singleStep : int

This property holds the step value.

当用户使用箭头改变自旋框值时,值将按 singleStep 数量递增/递减。默认值为 1。将 singleStep 值设为小于 0,什么都不做。

访问函数:

int singleStep () const
void setSingleStep (int val )

suffix : QString

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 ().

value : int

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 )

成员函数文档编制

QSpinBox:: QSpinBox ( QWidget * parent = 0)

构造自旋框采用 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 ().

void QSpinBox:: setRange ( int minimum , int maximum )

方便函数能设置 minimum ,和 maximum 值采用单函数调用。

setRange(minimum, maximum);
					

相当于:

setMinimum(minimum);
setMaximum(maximum);
					

另请参阅 minimum and 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 ().