QAbstractSpinBox Class

The QAbstractSpinBox class provides a spinbox and a line edit to display values. More...

Header: #include <QAbstractSpinBox>
qmake: QT += widgets
Inherits: QWidget
Inherited By:

QDateTimeEdit, QDoubleSpinBox, and QSpinBox

Public Types

enum ButtonSymbols { UpDownArrows, PlusMinus, NoButtons }
enum CorrectionMode { CorrectToPreviousValue, CorrectToNearestValue }
enum StepEnabledFlag { StepNone, StepUpEnabled, StepDownEnabled }
enum StepType { DefaultStepType, AdaptiveDecimalStepType }

Properties

Public Functions

Qt::Alignment alignment() const
QAbstractSpinBox::ButtonSymbols buttonSymbols() const
QAbstractSpinBox::CorrectionMode correctionMode() const
bool hasAcceptableInput() const
bool hasFrame() const
bool isAccelerated() const
bool isGroupSeparatorShown() const
bool isReadOnly() const
bool keyboardTracking() const
void setAccelerated(bool on)
void setAlignment(Qt::Alignment flag)
void setButtonSymbols(QAbstractSpinBox::ButtonSymbols bs)
void setCorrectionMode(QAbstractSpinBox::CorrectionMode cm)
void setFrame(bool)
void setGroupSeparatorShown(bool shown)
void setKeyboardTracking(bool kt)
void setReadOnly(bool r)
void setSpecialValueText(const QString &txt)
void setWrapping(bool w)
QString specialValueText() const
QString text() const
bool wrapping() const

Detailed Description

The class is designed as a common super class for widgets like QSpinBox, QDoubleSpinBox and QDateTimeEdit

Here are the main properties of the class:

  1. text: The text that is displayed in the QAbstractSpinBox.
  2. alignment: The alignment of the text in the QAbstractSpinBox.
  3. wrapping: Whether the QAbstractSpinBox wraps from the minimum value to the maximum value and vice versa.

QAbstractSpinBox provides a virtual stepBy() function that is called whenever the user triggers a step. This function takes an integer value to signify how many steps were taken. E.g. Pressing Qt::Key_Down will trigger a call to stepBy(-1).

When the user triggers a step whilst holding the Qt::ControlModifier, QAbstractSpinBox steps by 10 instead of making a single step. This step modifier affects wheel events, key events and interaction with the spinbox buttons. Note that on macOS, Control corresponds to the Command key.

Since Qt 5.12, QStyle::SH_SpinBox_StepModifier can be used to select which Qt::KeyboardModifier increases the step rate. Qt::NoModifier disables this feature.

QAbstractSpinBox also provide a virtual function stepEnabled() to determine whether stepping up/down is allowed at any point. This function returns a bitset of StepEnabled.

See also QAbstractSlider, QSpinBox, QDoubleSpinBox, QDateTimeEdit, and Spin Boxes Example.

Member Type Documentation

enum QAbstractSpinBox::ButtonSymbols

This enum type describes the symbols that can be displayed on the buttons in a spin box.

ConstantValueDescription
QAbstractSpinBox::UpDownArrows0Little arrows in the classic style.
QAbstractSpinBox::PlusMinus1+ and - symbols.
QAbstractSpinBox::NoButtons2Don't display buttons.

See also QAbstractSpinBox::buttonSymbols.

enum QAbstractSpinBox::CorrectionMode

This enum type describes the mode the spinbox will use to correct an Intermediate value if editing finishes.

ConstantValueDescription
QAbstractSpinBox::CorrectToPreviousValue0The spinbox will revert to the last valid value.
QAbstractSpinBox::CorrectToNearestValue1The spinbox will revert to the nearest valid value.

See also correctionMode.

enum QAbstractSpinBox::StepEnabledFlag

ConstantValue
QAbstractSpinBox::StepNone0x00
QAbstractSpinBox::StepUpEnabled0x01
QAbstractSpinBox::StepDownEnabled0x02

enum QAbstractSpinBox::StepType

ConstantValue
QAbstractSpinBox::DefaultStepType0
QAbstractSpinBox::AdaptiveDecimalStepType1

Property Documentation

accelerated : bool

This property holds whether the spin box will accelerate the frequency of the steps when pressing the step Up/Down buttons.

If enabled the spin box will increase/decrease the value faster the longer you hold the button down.

This property was introduced in Qt 4.2.

Access functions:

bool isAccelerated() const
void setAccelerated(bool on)

acceptableInput : const bool

This property holds whether the input satisfies the current validation

This property was introduced in Qt 4.2.

Access functions:

bool hasAcceptableInput() const

See also validate(), fixup(), and correctionMode.

alignment : Qt::Alignment

This property holds the alignment of the spin box

Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.

By default, the alignment is Qt::AlignLeft

Attempting to set the alignment to an illegal flag combination does nothing.

Access functions:

Qt::Alignment alignment() const
void setAlignment(Qt::Alignment flag)

See also Qt::Alignment.

buttonSymbols : ButtonSymbols

This property holds the current button symbol mode

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

Note that some styles might render PlusMinus and UpDownArrows identically.

Access functions:

QAbstractSpinBox::ButtonSymbols buttonSymbols() const
void setButtonSymbols(QAbstractSpinBox::ButtonSymbols bs)

See also ButtonSymbols.

correctionMode : CorrectionMode

This property holds the mode to correct an Intermediate value if editing finishes

The default mode is QAbstractSpinBox::CorrectToPreviousValue.

This property was introduced in Qt 4.2.

Access functions:

QAbstractSpinBox::CorrectionMode correctionMode() const
void setCorrectionMode(QAbstractSpinBox::CorrectionMode cm)

See also acceptableInput, validate(), and fixup().

frame : bool

This property holds whether the spin box draws itself with a frame

If enabled (the default) the spin box draws itself inside a frame, otherwise the spin box draws itself without any frame.

Access functions:

bool hasFrame() const
void setFrame(bool)

keyboardTracking : bool

This property holds whether keyboard tracking is enabled for the spinbox.

If keyboard tracking is enabled (the default), the spinbox emits the valueChanged() and textChanged() signals while the new value is being entered from the keyboard.

E.g. when the user enters the value 600 by typing 6, 0, and 0, the spinbox emits 3 signals with the values 6, 60, and 600 respectively.

If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() and textChanged() signals while typing. It emits the signals later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.

This property was introduced in Qt 4.3.

Access functions:

bool keyboardTracking() const
void setKeyboardTracking(bool kt)

readOnly : bool

This property holds whether the spin box is read only.

In read-only mode, the user can still copy the text to the clipboard, or drag and drop the text; but cannot edit it.

The QLineEdit in the QAbstractSpinBox does not show a cursor in read-only mode.

Access functions:

bool isReadOnly() const
void setReadOnly(bool r)

See also QLineEdit::readOnly.

showGroupSeparator : bool

This property holds whether a thousands separator is enabled. By default this property is false.

This property was introduced in Qt 5.3.

Access functions:

bool isGroupSeparatorShown() const
void setGroupSeparatorShown(bool shown)

specialValueText : QString

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 minimum(). Typical use is to indicate that this choice has a special (default) meaning.

For example, if your spin box allows the user to choose a scale factor (or zoom level) for displaying an image, and your application is able to automatically choose one that will enable the image to fit completely within the display window, you can set up the spin box like this:

     QSpinBox *zoomSpinBox = new QSpinBox;
     zoomSpinBox->setRange(0, 1000);
     zoomSpinBox->setSingleStep(10);
     zoomSpinBox->setSuffix("%");
     zoomSpinBox->setSpecialValueText(tr("Automatic"));
     zoomSpinBox->setValue(100);

The user will then be able to choose a scale from 1% to 1000% or select "Auto" to leave it up to the application to choose. Your code must then interpret the spin box value of 0 as a request from the user to scale the image to fit inside the window.

All values are displayed with the prefix and suffix (if set), except for the special value, which only shows the special value text. This special text is passed in the QSpinBox::textChanged() signal that passes a QString.

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

If no special-value text is set, specialValueText() returns an empty string.

Access functions:

QString specialValueText() const
void setSpecialValueText(const QString &txt)

text : const QString

This property holds the spin box's text, including any prefix and suffix

There is no default text.

Access functions:

QString text() const

wrapping : bool

This property holds whether the spin box is circular.

If wrapping is true stepping up from maximum() value will take you to the minimum() value and vice versa. Wrapping only make sense if you have minimum() and maximum() values set.

 QSpinBox *spinBox = new QSpinBox(this);
 spinBox->setRange(0, 100);
 spinBox->setWrapping(true);
 spinBox->setValue(100);
 spinBox->stepBy(1);
 // value is 0

Access functions:

bool wrapping() const
void setWrapping(bool w)

See also QSpinBox::minimum() and QSpinBox::maximum().