Classes - Annotated - Tree - Functions - Home - Structure

QSpinBox Class Reference

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

#include <qspinbox.h>

Inherits QFrame and QRangeControl.

List of all member functions.

Public Members

Public Slots

Signals

Properties

Protected Members

Protected Slots


Detailed Description

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

QSpinBox allows the user to choose a 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. The value is usually an integer.

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 buttons or using the keyboard accelerator's up and down arrows 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() if you want circular behavior.

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

Normally the spin box displays up and down arrows in the buttons. You can use setButtonSymbols() to change the display to show + and - symbols if this is clearer for your intended purpose. In either case the up and down arrow keys work.

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 QWidget::focusPolicy() is StrongFocus.

QSpinBox can easily be subclassed to allow the user to input other things than an integer 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(). For example, these functions could be changed so that the user provided values from 0.0 to 10.0 while the range of integers used inside the program would be 0 to 100:

    class MySpinBox : public QSpinBox
    {
        Q_OBJECT
    public:
        ...

        QString mapValueToText( int value )
        {
            return QString( "%1.%2" )
                   .arg( value / 10 ).arg( value % 10 );
        }

        int mapTextToValue( bool *ok )
        {
            return (int) ( 10 * text().toFloat() );
        }
    };
  

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


Member Type Documentation

QSpinBox::ButtonSymbols

This enum type determines what the buttons in a spin box show. The currently defined values are:

See also QSpinBox::buttonSymbols.


Member Function Documentation

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

Constructs a spin box with the default QRangeControl range and step values. It has the parent parent and the name name.

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

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

Constructs a spin box that allows values from minValue to maxValue inclusive, with step amount step. The value is initially set to minValue.

parent serves as the parent widget of this spin box with the name name.

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

QSpinBox::~QSpinBox ()

Destroys the spin box, freeing all memory and other resources.

ButtonSymbols QSpinBox::buttonSymbols () const

Returns the current button symbol mode. See the "buttonSymbols" property for details.

QString QSpinBox::cleanText () const [virtual]

Returns the current text of the spin box with any prefix or suffix and with the whitespace at the start and end removed. See the "cleanText" property for details.

QString QSpinBox::currentValueText () [protected]

Returns the full text calculated from the current value, including any prefix, suffix or special-value text.

QRect QSpinBox::downRect () const

Returns the geometry of the "up" button.

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

Returns a pointer to the embedded QLineEdit.

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

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

Reimplemented from QObject.

void QSpinBox::interpretText () [virtual protected]

QSpinBox calls this after the user has manually edited the contents of the spin box (not using the up/down buttons/keys).

The default implementation of this function interprets the new text using mapTextToValue(). If mapTextToValue() is successful, it changes the spin box's value; if not, the value is left unchanged.

int QSpinBox::lineStep () const

Returns the line step. See the "lineStep" property for details.

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

This virtual function is used by the spin box whenever it needs to interpret text entered by the user as a value. The text is available as text() and as cleanText(), and this function must parse it if possible, and set the bool ok to TRUE if successful and to FALSE otherwise.

Subclasses that need to display spin box values in a non-numeric way need to reimplement this function.

Note that Qt handles specialValueText() specially; this function is only concerned with the other values.

The default implementation tries to interpret it as an integer in the standard way and returns the integer value.

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. Reimplementations may return anything.

Note that Qt does not call this function for specialValueText() and that it prepends prefix() and appends suffix() to the return value.

If you reimplement this, you may also need to reimplement mapTextToValue().

See also updateDisplay() and mapTextToValue().

int QSpinBox::maxValue () const

Returns the maximum value of the spin box. See the "maxValue" property for details.

int QSpinBox::minValue () const

Returns the minimum value of the spin box. See the "minValue" property for details.

QString QSpinBox::prefix () const [virtual]

Returns the prefix of the spin box. See the "prefix" property for details.

void QSpinBox::rangeChange () [virtual protected]

This virtual function is called by QRangeControl whenever the range has changed. It adjusts the default validator and updates the display; if you need additional processing, you may reimplement this function.

Reimplemented from QRangeControl.

void QSpinBox::setButtonSymbols ( ButtonSymbols ) [virtual]

Sets the current button symbol mode. See the "buttonSymbols" property for details.

void QSpinBox::setLineStep ( int )

Sets the line step. See the "lineStep" property for details.

void QSpinBox::setMaxValue ( int )

Sets the maximum value of the spin box. See the "maxValue" property for details.

void QSpinBox::setMinValue ( int )

Sets the minimum value of the spin box. See the "minValue" property for details.

void QSpinBox::setPrefix ( const QString & text ) [virtual slot]

Sets the prefix of the spin box to text. See the "prefix" property for details.

void QSpinBox::setSpecialValueText ( const QString & text ) [virtual]

Sets the special-value text to text. See the "specialValueText" property for details.

void QSpinBox::setSuffix ( const QString & text ) [virtual slot]

Sets the suffix of the spin box to text. See the "suffix" property for details.

void QSpinBox::setValidator ( const QValidator * v ) [virtual]

Sets the validator to v. The validator controls what keyboard input is accepted when the user is editing in the value field. The default is to use a suitable QIntValidator.

Use setValidator(0) to turn off input validation (entered input will still be clamped to the range of the spinbox).

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

Sets the current value of the spin box to value. See the "value" property for details.

void QSpinBox::setWrapping ( bool on ) [virtual]

Sets whether it is possible to step the value from the highest value to the lowest value and vice versa to on. See the "wrapping" property for details.

QString QSpinBox::specialValueText () const

Returns the special-value text. See the "specialValueText" property for details.

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 keyboard accelerators, for example.

See also stepUp(), subtractLine(), lineStep, setSteps(), value 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 keyboard accelerators, for example.

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

QString QSpinBox::suffix () const [virtual]

Returns the suffix of the spin box. See the "suffix" property for details.

QString QSpinBox::text () const

Returns the current text of the spin box, including any prefix() and suffix(). See the "text" property for details.

void QSpinBox::textChanged () [protected slot]

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

QRect QSpinBox::upRect () const

Returns the geometry of the "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().

const QValidator * QSpinBox::validator () const

Returns the validator that constrains editing for this spin box if there is any, or else 0.

See also setValidator() and QValidator.

int QSpinBox::value () const

Returns the current value of the spin box. See the "value" property for details.

void QSpinBox::valueChange () [virtual protected]

This virtual function is called by QRangeControl whenever the value has changed. The QSpinBox reimplementation updates the display and emits the valueChanged() signals; if you need additional processing, you may either reimplement this or connect to one of the valueChanged() signals.

Reimplemented from QRangeControl.

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

This signal is emitted every time the value of the spin box changes to value. This is for example caused by setValue(), a keyboard accelerator, mouse clicks, etc.

Note that the valueChanged() signal is emitted every time, not just for the "final" step; i.e. if the user clicks "up" three times, this signal is emitted three times.

See also value.

Examples: listbox/listbox.cpp, qfd/fontdisplayer.cpp, scribble/scribble.cpp and validator/vw.cpp.

void QSpinBox::valueChanged ( const QString & valueText ) [signal]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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 valueText parameter is the same string that is displayed in the edit field of the spin box.

See also value.

bool QSpinBox::wrapping () const

Returns TRUE if it is possible to step the value from the highest value to the lowest value and vice versa; otherwise returns FALSE. See the "wrapping" property for details.


Property Documentation

ButtonSymbols buttonSymbols

This property holds the current button symbol mode.

The possible values can be either UpDownArrows or PlusMinus. The default is UpDownArrows.

Set this property's value with setButtonSymbols() and get this property's value with buttonSymbols().

See also ButtonSymbols.

QString cleanText

This property holds the current text of the spin box with any prefix or suffix and with the whitespace at the start and end removed.

Get this property's value with cleanText().

See also text, prefix and suffix.

int lineStep

This property holds the line step.

The setter function setLineStep() calls the virtual stepChange() function if the new line step is different from the previous setting.

Set this property's value with setLineStep() and get this property's value with lineStep().

See also QRangeControl::setSteps() and setRange().

int maxValue

This property holds the maximum value of the spin box.

When setting this property, the minValue is adjusted so that the range remains valid if necessary.

Set this property's value with setMaxValue() and get this property's value with maxValue().

See also setRange().

int minValue

This property holds the minimum value of the spin box.

When setting this property, the maxValue is adjusted so that the range remains valid if necessary.

Set this property's value with setMinValue() and get this property's value with minValue().

See also setRange().

QString prefix

This property holds the prefix of the spin box.

The prefix is prepended to the start of the displayed value. Typical use is to indicate the unit of measurement to the user. For example:

    sb->setPrefix("$");
  

To turn off the prefix display, set this property to an empty string. The default is no prefix.

If no prefix is set, the getter function prefix() returns a null string.

Set this property's value with setPrefix() and get this property's value with prefix().

See also suffix.

QString specialValueText

This property holds the special-value text.

If set, the spin box will display this text instead of a numeric value whenever the current value is equal to minVal(). Typical use is to indicate that this choice has a special (default) meaning.

For example, if your spin box allows the user to choose the 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, set this property to an empty string as the parameter. The default is no special-value text, i.e., the numeric value is shown as usual.

If no special-value text is currently set, the getter function specialValueText() returns a null string.

Set this property's value with setSpecialValueText() and get this property's value with specialValueText().

QString suffix

This property holds the suffix of the spin box.

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, set this property to an empty string. The default is no suffix.

If no suffix is set, the getter function suffix() returns a null string.

Set this property's value with setSuffix() and get this property's value with suffix().

See also prefix.

QString text

This property holds the current text of the spin box, including any prefix() and suffix().

Get this property's value with text().

See also value.

int value

This property holds the current value of the spin box.

Set this property's value with setValue() and get this property's value with value().

See also QRangeControl::setValue().

bool wrapping

This property holds whether it is possible to step the value from the highest value to the lowest value and vice versa.

By default, wrapping is turned off.

Set this property's value with setWrapping() and get this property's value with wrapping().

See also minValue, maxValue and setRange().


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


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta1-beta1