# QSpinBox Class Reference

The QSpinBox class provides a spin box widget, sometimes called up-down widget, little arrows widget or spin button. More...

`#include <qspinbox.h>`

Inherits QFrame and QRangeControl.

List of all member functions.

## Public Members

- QSpinBox ( QWidget * parent = 0, const char * name = 0 )
- QSpinBox ( int minValue, int maxValue, int step = 1, QWidget * parent = 0, const char * name = 0 )
- ~QSpinBox ()
- const char* text () const
- virtual const char* prefix () const
- virtual const char* suffix () const
- virtual QString cleanText () const
- void setSpecialValueText ( const char * text )
- const char* specialValueText () const
- void setWrapping ( bool on )
- bool wrapping () const
- void setValidator ( QValidator * v )
- virtual QSize sizeHint () const

## Public Slots

- virtual void setValue ( int value )
- virtual void setPrefix ( const char * text )
- virtual void setSuffix ( const char * text )
- virtual void stepUp ()
- virtual void stepDown ()

## Signals

- void valueChanged ( int value )
- void valueChanged ( const char * valueText )

## Protected Members

- virtual QString mapValueToText ( int value )
- virtual int mapTextToValue ( bool * ok )
- QString currentValueText ()
- virtual void updateDisplay ()
- virtual void interpretText ()
- QPushButton* upButton () const
- QPushButton* downButton () const
- QLineEdit* editor () const
- virtual void valueChange ()
- virtual void rangeChange ()
- virtual bool eventFilter ( QObject * obj, QEvent * ev )
- virtual void resizeEvent ( QResizeEvent * ev )
- virtual void paletteChange ( const QPalette & )
- virtual void enabledChange ( bool )
- virtual void fontChange ( const QFont & )
- virtual void styleChange ( GUIStyle )

## Protected Slots

- void textChanged ()

## Detailed Description

The QSpinBox class provides a spin box widget, sometimes called up-down widget, little arrows widget or spin button.

QSpinBox allows the user to choose a numeric value, either by clicking the up/down buttons to increase/decrease the value currently displayed, or by typing the value directly into the spin box.

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

The spin box clamps the value within a numeric range, see QRangeControl for details. Clicking the up/down down buttons (or using the keyboard accelerators: Up-arrow and Down-arrow) will increase or decrease the current value in steps of size lineStep().

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. Use setWrapping() to if you want circular behavior.

The displayed value can be appended and/or prepended with an arbitray string indicating for example the unit of measurement. See setPrefix() and setSuffix().

It is often desirable to give the user a special, often default, choice in addition to the range of numeric values. See setSpecialValueText() for how to do this with QSpinBox.

The default focus policy is StrongFocus.

QSpinBox can easily be subclassed to allow the user to input other things than a numeric value, as long as the allowed input can be mapped down to a range of integers. This can be done by overriding the virtual functions mapValueToText() and mapTextToValue() and setting another, suitable validator using setValidator().

See also: QScrollBar, QSlider and GUI Design Handbook: Spin Box

Examples: widgets/widgets.cpp

## Member Function Documentation

### QSpinBox::QSpinBox ( QWidget * parent = 0, const char * name = 0 )

Creates a spin box with the default QRangeControl range and step value.

See also: minValue(), maxValue(), setRange(), lineStep() and setSteps().

### QSpinBox::QSpinBox ( int minValue, int maxValue, int step = 1, QWidget * parent = 0, const char * name = 0 )

Creates a spin box with range from minValue to maxValue inclusive, with step value step. The value is set to minValue.

See also: minValue(), maxValue(), setRange(), lineStep() and setSteps().

### QSpinBox::~QSpinBox ()

Deletes the spin box, freeing all memory and other resoures.

### QString QSpinBox::cleanText () const `[virtual]`

Returns a copy of the current text of the spin box with any prefix and/or suffix and white space at the start and end removed.

See also: text(), setPrefix() and setSuffix().

### QString QSpinBox::currentValueText () `[protected]`

Returns the full text of the current value, i.e. including any prefix, suffix or special-value text.

### QPushButton* QSpinBox::downButton () const `[protected]`

Returns a pointer to the embedded 'down' button.

### QLineEdit* QSpinBox::editor () const `[protected]`

Returns a pointer to the embedded QLineEdit.

### void QSpinBox::enabledChange ( bool ) `[virtual protected]`

Reimplemented for internal purposes.

Reimplemented from QWidget.

### bool QSpinBox::eventFilter ( QObject * obj, QEvent * ev ) `[virtual protected]`

Intercepts and handles those events coming to the embedded QLineEdit which have special meaning for the QSpinBox.

Reimplemented from QObject.

### void QSpinBox::fontChange ( const QFont & ) `[virtual protected]`

Reimplemented for internal purposes.

Reimplemented from QWidget.

### void QSpinBox::interpretText () `[virtual protected]`

Called after the user has manually edited the contents of the spin box. Interprets the text using mapTextToValue(), and calls setValue() if successful.

### int QSpinBox::mapTextToValue ( bool * ok ) `[virtual protected]`

This virtual function is used by the spin box whenever it needs to interpret the text entered by the user as a value. The default implementation tries to interpret it as an integer in the standard way, and returns the integer value.

Override this function in in a subclass if you want a specialized spin box, handling something else than integers. It should call text() (or cleanText() ) and return the value corresponding to that text. If the text does not represent a legal value (uninterpretable), the bool pointed to by ok should be set to FALSE.

This function need not be concerned with special-value text, the QSpinBox handles that automatically.

See also: interpretText() and mapValueToText().

### QString QSpinBox::mapValueToText ( int v ) `[virtual protected]`

This virtual function is used by the spin box whenever it needs to display value v. The default implementation returns a string containing v printed in the standard way.

Override this function in in a subclass if you want a specialized spin box, handling something else than integers. This function need not be concerned with prefix or suffix or special-value text, the QSpinBox handles that automatically.

See also: updateDisplay() and mapTextToValue().

### void QSpinBox::paletteChange ( const QPalette & ) `[virtual protected]`

Reimplemented for internal purposes.

Reimplemented from QWidget.

### const char* QSpinBox::prefix () const `[virtual]`

Returns the currently set prefix, or 0 if no prefix is currently set.

See also: setPrefix(), setSuffix() and suffix().

### void QSpinBox::rangeChange () `[virtual protected]`

This method gets called by QRangeControl whenever the range has changed. It adjusts the default validator and updates the display.

### void QSpinBox::resizeEvent ( QResizeEvent * ) `[virtual protected]`

Handles resize events for the spin box.

Reimplemented from QWidget.

### void QSpinBox::setPrefix ( const char * text ) `[virtual slot]`

Sets the prefix to text. The prefix is prepended to the start of the displayed value. Typical use is to indicate the unit of measurement to the user.

To turn off the prefix display, call this function with 0 or an empty string as parameter. The default is no prefix.

See also: prefix(), setSuffix() and suffix().

### void QSpinBox::setSpecialValueText ( const char * text )

Sets the special-value text to text. If set, the spin box will display this text instead of a numeric value whenever the current value is equal to minVal(). Typically used for indicating that this choice has a special (default) meaning.

For example, if you use a spin box for letting the user choose margin width in a print dialog, and your application is able to automatically choose a good margin width, you can set up the spin box like this:

        QSpinBox marginBox( -1, 20, 1, parent, "marginBox" );
        marginBox->setSuffix( " mm" );
        marginBox->setSpecialValueText( "Auto" );

The user will then be able to choose a margin width from 0-20 millimeters, or select "Auto" to leave it to the application to choose. Your code must then interpret the spin box value of -1 as the user requesting automatic margin width.

Neither prefix nor suffix, if set, are added to the special-value text when displayed.

To turn off the special-value text display, call this function with 0 or an empty string as parameter. The default is no special-value text, i.e. the numeric value is shown as usual.

See also: specialValueText().

Examples: widgets/widgets.cpp

### void QSpinBox::setSuffix ( const char * text ) `[virtual slot]`

Sets the suffix to text. The suffix is appended to the end of the displayed value. Typical use is to indicate the unit of measurement to the user.

To turn off the suffix display, call this function with 0 or an empty string as parameter. The default is no suffix.

See also: suffix(), setPrefix() and prefix().

Examples: widgets/widgets.cpp

### void QSpinBox::setValidator ( QValidator * v )

Sets the validator to v. The default is to use a suitable QIntValidator.

### void QSpinBox::setValue ( int value ) `[virtual slot]`

Sets the current value of the spin box to value. This is QRangeControl::setValue() made available as a slot.

### void QSpinBox::setWrapping ( bool on )

Setting wrapping to TRUE will allow the value to be stepped from the highest value to the lowest, and vice versa. By default, wrapping is turned off.

See also: wrapping(), minValue(), maxValue() and setRange().

### QSize QSpinBox::sizeHint () const `[virtual]`

Returns a good-looking size for the spin box.

Examples: widgets/widgets.cpp

Reimplemented from QWidget.

### const char* QSpinBox::specialValueText () const

Returns the currently special-value text, or 0 if no special-value text is currently set.

See also: setSpecialValueText().

### void QSpinBox::stepDown () `[virtual slot]`

Decreases the current value one step, wrapping as necessary. This is the same as clicking on the pointing-down button, and can be used for e.g. keyboard accelerators.

See also: stepUp(), subtractLine(), lineStep(), setSteps(), setValue() and value().

### void QSpinBox::stepUp () `[virtual slot]`

Increases the current value one step, wrapping as necessary. This is the same as clicking on the pointing-up button, and can be used for e.g. keyboard accelerators.

See also: stepDown(), addLine(), lineStep(), setSteps(), setValue() and value().

### void QSpinBox::styleChange ( GUIStyle ) `[virtual protected]`

Reimplemented for internal purposes.

Reimplemented from QWidget.

### const char* QSpinBox::suffix () const `[virtual]`

Returns the currently set suffix, or 0 if no suffix is currently set.

See also: setSuffix(), setPrefix() and suffix().

### const char * QSpinBox::text () const

Returns the current text of the spin box.

See also: value().

### void QSpinBox::textChanged () `[protected slot]`

This slot gets called whenever the user edits the text of the spin box.

### QPushButton* QSpinBox::upButton () const `[protected]`

Returns a pointer to the embedded 'up' button.

### void QSpinBox::updateDisplay () `[virtual protected]`

Updates the contents of the embedded QLineEdit to reflect current value, using mapValueToText(). Also enables/disables the push buttons accordingly.

See also: mapValueToText().

### void QSpinBox::valueChange () `[virtual protected]`

This method gets called by QRangeControl whenever the value has changed. Updates the display and emits the valueChanged() signals.

### void QSpinBox::valueChanged ( const char * valueText ) `[signal]`

This signal is emitted whenever the valueChanged( int ) signal is emitted, i.e. every time the value of the spin box changes (whatever the cause - by setValue(), by a keyboard accelerator, by mouse clicks etc.).

The string pointed to by valueText is the same string that is displayed in the edit field of the spin box.

See also: value().

### void QSpinBox::valueChanged ( int value ) `[signal]`

This signal is emitted every time the value of the spin box changes (whatever the cause - by setValue(), by a keyboard accelerator, by mouse clicks etc.).

Note that it is emitted every time, not just for the "final" step - if the user clicks 'up' three times, this signal is emitted three times.

See also: value().

### bool QSpinBox::wrapping () const

Returns the current setWrapping() value.

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qspinbox.h: 1998/09/28
- qspinbox.cpp: 1998/09/28