
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQSpinBox class provides a spin box widget.More...
| Header: | #include <QSpinBox> |
| Inherits: | 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 |
TheQSpinBox class provides a spin box widget.
QSpinBox is designed to handle integers and discrete sets of values (e.g., month names); useQDoubleSpinBox for floating point values.
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 withvalidate(),textFromValue() andvalueFromText().
Every time the value changesQSpinBox emits thevalueChanged() signals. The current value can be fetched withvalue() and set withsetValue().
Clicking the up/down buttons or using the keyboard accelerator's up and down arrows will increase or decrease the current value in steps of sizesingleStep(). If you want to change this behaviour you can reimplement the virtual functionstepBy(). The minimum and maximum value and the step size can be set using one of the constructors, and can be changed later withsetMinimum(),setMaximum() andsetSingleStep().
Most spin boxes are directional, butQSpinBox 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 ifwrapping() is set to true. UsesetWrapping() if you want circular behavior.
The displayed value can be prepended and appended with arbitrary strings indicating, for example, currency or the unit of measurement. SeesetPrefix() andsetSuffix(). The text in the spin box is retrieved withtext() (which includes anyprefix() andsuffix()), or withcleanText() (which has noprefix(), nosuffix() and no leading or trailing whitespace).
It is often desirable to give the user a special (often default) choice in addition to the range of numeric values. SeesetSpecialValueText() for how to do this withQSpinBox.
![]() | A spin box shown in theWindows XP widget style. |
![]() | A spin box shown in thePlastique widget style. |
![]() | A spin box shown in theMacintosh widget style. |
If usingprefix(),suffix(), andspecialValueText() don't provide enough control, you subclassQSpinBox and reimplementvalueFromText() andtextFromValue(). For example, here's the code for a custom spin box that allows the user to enter icon sizes (e.g., "32 x 32"):
int IconSizeSpinBox::valueFromText(constQString&text)const{QRegExp regExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?"));if (regExp.exactMatch(text)) {return regExp.cap(1).toInt(); }else {return0; }}QString IconSizeSpinBox::textFromValue(int value)const{return tr("%1 x %1").arg(value);}
See theIcons example for the full source code.
See alsoQDoubleSpinBox,QDateTimeEdit,QSlider, andSpin Boxes Example.
This property holds the text of the spin box excluding any prefix, suffix, or leading or trailing whitespace.
Access functions:
| QString | cleanText() const |
See alsotext,QSpinBox::prefix, andQSpinBox::suffix.
This property holds the maximum value of the spin box.
When setting this property theminimum is adjusted if necessary, to ensure that the range remains valid.
The default maximum value is 99.
Access functions:
| int | maximum() const |
| void | setMaximum(int max) |
See alsosetRange() andspecialValueText.
This property holds the minimum value of the spin box.
When setting this property themaximum is adjusted if necessary to ensure that the range remains valid.
The default minimum value is 0.
Access functions:
| int | minimum() const |
| void | setMinimum(int min) |
See alsosetRange() andspecialValueText.
This property holds the spin box's prefix.
The prefix is prepended to the start of the displayed value. Typical use is to display a unit of measurement or a currency symbol. For example:
sb->setPrefix("$");
To turn off the prefix display, set this property to an empty string. The default is no prefix. The prefix is not displayed whenvalue() ==minimum() andspecialValueText() is set.
If no prefix is set, prefix() returns an empty string.
Access functions:
| QString | prefix() const |
| void | setPrefix(const QString & prefix) |
See alsosuffix(),setSuffix(),specialValueText(), andsetSpecialValueText().
This property holds the step value.
When the user uses the arrows to change the spin box's value the value will be incremented/decremented by the amount of the singleStep. The default value is 1. Setting a singleStep value of less than 0 does nothing.
Access functions:
| int | singleStep() const |
| void | setSingleStep(int val) |
This property holds the suffix of the spin box.
The suffix is appended to the end of the displayed value. Typical use is to display a unit of measurement or a currency symbol. For example:
sb->setSuffix(" km");
To turn off the suffix display, set this property to an empty string. The default is no suffix. The suffix is not displayed for theminimum() ifspecialValueText() is set.
If no suffix is set, suffix() returns an empty string.
Access functions:
| QString | suffix() const |
| void | setSuffix(const QString & suffix) |
See alsoprefix(),setPrefix(),specialValueText(), andsetSpecialValueText().
This property holds the value of the spin box.
setValue() will emit valueChanged() if the new value is different from the old one.
Access functions:
| int | value() const |
| void | setValue(int val) |
Notifier signal:
| void | valueChanged(int i) |
| void | valueChanged(const QString & text) |
Constructs a spin box with 0 as minimum value and 99 as maximum value, a step value of 1. The value is initially set to 0. It is parented toparent.
See alsosetMinimum(),setMaximum(), andsetSingleStep().
[virtual protected]bool QSpinBox::event(QEvent * event)Reimplemented fromQObject::event().
[virtual protected]void QSpinBox::fixup(QString & input) constReimplemented fromQAbstractSpinBox::fixup().
Convenience function to set theminimum, andmaximum values with a single function call.
setRange(minimum, maximum);is equivalent to:
setMinimum(minimum);setMaximum(maximum);
[virtual protected]QString QSpinBox::textFromValue(int value) constThis virtual function is used by the spin box whenever it needs to display the givenvalue. The default implementation returns a string containingvalue printed in the standard way usingQWidget::locale().toString(), but with the thousand separator removed. Reimplementations may return anything. (See the example in the detailed description.)
Note:QSpinBox does not call this function forspecialValueText() and that neitherprefix() norsuffix() should be included in the return value.
If you reimplement this, you may also need to reimplementvalueFromText() andvalidate()
See alsovalueFromText(),validate(), andQLocale::groupSeparator().
[virtual protected]QValidator::State QSpinBox::validate(QString & text,int & pos) constReimplemented fromQAbstractSpinBox::validate().
[virtual protected]int QSpinBox::valueFromText(constQString & text) constThis virtual function is used by the spin box whenever it needs to interprettext entered by the user as a value.
Subclasses that need to display spin box values in a non-numeric way need to reimplement this function.
Note:QSpinBox handlesspecialValueText() separately; this function is only concerned with the other values.
See alsotextFromValue() andvalidate().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.