QSpinBox

Inheritance diagram of QSpinBox

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

The PySide.QtGui.QSpinBox class provides a spin box widget.

PySide.QtGui.QSpinBox is designed to handle integers and discrete sets of values (e.g., month names); use PySide.QtGui.QDoubleSpinBox for floating point values.

PySide.QtGui.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 PySide.QtGui.QSpinBox.validate() , PySide.QtGui.QSpinBox.textFromValue() and PySide.QtGui.QSpinBox.valueFromText() .

Every time the value changes PySide.QtGui.QSpinBox emits the PySide.QtGui.QSpinBox.valueChanged() signals. The current value can be fetched with PySide.QtGui.QSpinBox.value() and set with PySide.QtGui.QSpinBox.setValue() .

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 size PySide.QtGui.QSpinBox.singleStep() . If you want to change this behaviour you can reimplement the virtual function PySide.QtGui.QAbstractSpinBox.stepBy() . The minimum and maximum value and the step size can be set using one of the constructors, and can be changed later with PySide.QtGui.QSpinBox.setMinimum() , PySide.QtGui.QSpinBox.setMaximum() and PySide.QtGui.QSpinBox.setSingleStep() .

Most spin boxes are directional, but PySide.QtGui.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 PySide.QtGui.QAbstractSpinBox.wrapping() is set to true. Use PySide.QtGui.QAbstractSpinBox.setWrapping() 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. See PySide.QtGui.QSpinBox.setPrefix() and PySide.QtGui.QSpinBox.setSuffix() . The text in the spin box is retrieved with PySide.QtGui.QAbstractSpinBox.text() (which includes any PySide.QtGui.QSpinBox.prefix() and PySide.QtGui.QSpinBox.suffix() ), or with PySide.QtGui.QSpinBox.cleanText() (which has no PySide.QtGui.QSpinBox.prefix() , no PySide.QtGui.QSpinBox.suffix() 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. See PySide.QtGui.QAbstractSpinBox.setSpecialValueText() for how to do this with PySide.QtGui.QSpinBox .

../../_images/windowsxp-spinbox.png A spin box shown in the Windows XP widget style .
../../_images/plastique-spinbox.png A spin box shown in the Plastique widget style .
../../_images/macintosh-spinbox.png A spin box shown in the Macintosh widget style .

Subclassing QSpinBox

If using PySide.QtGui.QSpinBox.prefix() , PySide.QtGui.QSpinBox.suffix() , and PySide.QtGui.QAbstractSpinBox.specialValueText() don’t provide enough control, you subclass PySide.QtGui.QSpinBox and reimplement PySide.QtGui.QSpinBox.valueFromText() and PySide.QtGui.QSpinBox.textFromValue() . For example, here’s the code for a custom spin box that allows the user to enter icon sizes (e.g., “32 x 32”):

def valueFromText(self, text):
    regExp = QRegExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?"))

    if regExp.exactMatch(text):
        return regExp.cap(1).toInt()
    else:
        return 0

def textFromValue(self, value):
    return self.tr("%1 x %1").arg(value)

See the Icons example for the full source code.

class PySide.QtGui.QSpinBox([parent=None])
Parameters:parentPySide.QtGui.QWidget

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 to parent .

PySide.QtGui.QSpinBox.cleanText()
Return type:unicode

This property holds the text of the spin box excluding any prefix, suffix, or leading or trailing whitespace..

PySide.QtGui.QSpinBox.maximum()
Return type:PySide.QtCore.int

This property holds the maximum value of the spin box.

When setting this property the PySide.QtGui.QSpinBox.minimum() is adjusted if necessary, to ensure that the range remains valid.

The default maximum value is 99.

PySide.QtGui.QSpinBox.minimum()
Return type:PySide.QtCore.int

This property holds the minimum value of the spin box.

When setting this property the PySide.QtGui.QSpinBox.maximum() is adjusted if necessary to ensure that the range remains valid.

The default minimum value is 0.

PySide.QtGui.QSpinBox.prefix()
Return type:unicode

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 when PySide.QtGui.QSpinBox.value() == PySide.QtGui.QSpinBox.minimum() and PySide.QtGui.QAbstractSpinBox.specialValueText() is set.

If no prefix is set, PySide.QtGui.QSpinBox.prefix() returns an empty string.

PySide.QtGui.QSpinBox.setMaximum(max)
Parameters:maxPySide.QtCore.int

This property holds the maximum value of the spin box.

When setting this property the PySide.QtGui.QSpinBox.minimum() is adjusted if necessary, to ensure that the range remains valid.

The default maximum value is 99.

PySide.QtGui.QSpinBox.setMinimum(min)
Parameters:minPySide.QtCore.int

This property holds the minimum value of the spin box.

When setting this property the PySide.QtGui.QSpinBox.maximum() is adjusted if necessary to ensure that the range remains valid.

The default minimum value is 0.

PySide.QtGui.QSpinBox.setPrefix(prefix)
Parameters:prefix – unicode

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 when PySide.QtGui.QSpinBox.value() == PySide.QtGui.QSpinBox.minimum() and PySide.QtGui.QAbstractSpinBox.specialValueText() is set.

If no prefix is set, PySide.QtGui.QSpinBox.prefix() returns an empty string.

PySide.QtGui.QSpinBox.setRange(min, max)
Parameters:
  • minPySide.QtCore.int
  • maxPySide.QtCore.int

Convenience function to set the minimum , and maximum values with a single function call.

setRange(minimum, maximum)

is equivalent to:

setMinimum(minimum)
setMaximum(maximum)
PySide.QtGui.QSpinBox.setSingleStep(val)
Parameters:valPySide.QtCore.int

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 PySide.QtGui.QSpinBox.singleStep() . The default value is 1. Setting a PySide.QtGui.QSpinBox.singleStep() value of less than 0 does nothing.

PySide.QtGui.QSpinBox.setSuffix(suffix)
Parameters:suffix – unicode

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 the PySide.QtGui.QSpinBox.minimum() if PySide.QtGui.QAbstractSpinBox.specialValueText() is set.

If no suffix is set, PySide.QtGui.QSpinBox.suffix() returns an empty string.

PySide.QtGui.QSpinBox.setValue(val)
Parameters:valPySide.QtCore.int

This property holds the value of the spin box.

PySide.QtGui.QSpinBox.setValue() will emit PySide.QtGui.QSpinBox.valueChanged() if the new value is different from the old one.

PySide.QtGui.QSpinBox.singleStep()
Return type:PySide.QtCore.int

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 PySide.QtGui.QSpinBox.singleStep() . The default value is 1. Setting a PySide.QtGui.QSpinBox.singleStep() value of less than 0 does nothing.

PySide.QtGui.QSpinBox.suffix()
Return type:unicode

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 the PySide.QtGui.QSpinBox.minimum() if PySide.QtGui.QAbstractSpinBox.specialValueText() is set.

If no suffix is set, PySide.QtGui.QSpinBox.suffix() returns an empty string.

PySide.QtGui.QSpinBox.textFromValue(val)
Parameters:valPySide.QtCore.int
Return type:unicode

This virtual function is used by the spin box whenever it needs to display the given value . The default implementation returns a string containing value printed in the standard way using QWidget.locale() . toString() , but with the thousand separator removed. Reimplementations may return anything. (See the example in the detailed description.)

Note: PySide.QtGui.QSpinBox does not call this function for PySide.QtGui.QAbstractSpinBox.specialValueText() and that neither PySide.QtGui.QSpinBox.prefix() nor PySide.QtGui.QSpinBox.suffix() should be included in the return value.

If you reimplement this, you may also need to reimplement PySide.QtGui.QSpinBox.valueFromText() and PySide.QtGui.QSpinBox.validate()

See also

PySide.QtGui.QSpinBox.valueFromText() PySide.QtGui.QSpinBox.validate() QLocale.groupSeparator()

PySide.QtGui.QSpinBox.value()
Return type:PySide.QtCore.int

This property holds the value of the spin box.

PySide.QtGui.QSpinBox.setValue() will emit PySide.QtGui.QSpinBox.valueChanged() if the new value is different from the old one.

PySide.QtGui.QSpinBox.valueChanged(arg__1)
Parameters:arg__1PySide.QtCore.int
def callback_int(value_as_int):
    print 'int value changed:', repr(value_as_int)

app = QApplication(sys.argv)
spinbox = QSpinBox()
spinbox.valueChanged[unicode].connect(callback_unicode)
spinbox.show()
sys.exit(app.exec_())
PySide.QtGui.QSpinBox.valueChanged(arg__1)
Parameters:arg__1 – unicode
def callback_unicode(value_as_unicode):
    print 'unicode value changed:', repr(value_as_unicode)

app = QApplication(sys.argv)
spinbox = QSpinBox()
spinbox.valueChanged[unicode].connect(callback_unicode)
spinbox.show()
sys.exit(app.exec_())
PySide.QtGui.QSpinBox.valueFromText(text)
Parameters:text – unicode
Return type:PySide.QtCore.int

This virtual function is used by the spin box whenever it needs to interpret text 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: PySide.QtGui.QSpinBox handles PySide.QtGui.QAbstractSpinBox.specialValueText() separately; this function is only concerned with the other values.

See also

PySide.QtGui.QSpinBox.textFromValue() PySide.QtGui.QSpinBox.validate()