6. Numeric Widgets
Qt offers several widgets designed for numeric input:
QSpinBox- integer valuesQDoubleSpinBox- floating-point (decimal) valuesQSlider- linear selection with a sliding handleQDial- selection with a circular dial
These widgets form two distinct inheritance trees. QDoubleSpinBox and QSpinBox are inherited from QAbstractSpinBox

While QSlider and QDial are inherited from QAbstractSlider:

All of them are bounded (they have configurable minimum and maximum values) and emit the valueChanged() when the user modifies the value.
6.1 QSpinBox
A QSpinBox allows the user select an integer either by clicking the up/down arrows, pressing the keyboard arrows or typing directly.
![]() |
You want to enable the user to select the amount of RAM. |
To do this:
Create the
QSpinBoxinstance and configure it. The default spinbox range is 0-99, but in this example we restrict it to realistic RAM sizes (16-256 GB), set the step size 2 GB, the initial value to 32 GB, and append the unit withsetSuffix(' GB').Define a slot that receives the new value and decorate it with
@Slot(int). The slot name isset_ram()and it sets a label’s text to the spinbox value.Connect the
valueChangedsignal to the slot. The slot receives the spinbox value as its argument.
The application looks like this:

6.2 QDoubleSpinBox
QDoubleSpinBox lets the user select a floating-point (float) value. In this example we use it to control the opacity of another widget (a green label).
![]() |
You need to let the user set a label’s opacity. |
To do this:
Create the spinbox and configure its properties. We limit the range to 0.0-1.0, and choose a single step of 0.05.
Create a slot to react to the
valueChanged()signal. The slotchange_opacity()receives the new value as afloat. We appl it toQGraphicsOpacityEffectattached to the target widget. The effect’ssetOpacity()method accepts values between 0.0 (fully transparent) and 1.0 (fully opaque).Connect the signal to the slot.

6.3 QSlider
QSlider provides a vertical or horizontal slider that lets the user select an integer value by dragging a handle. Its main signals are:
| Signal | Description |
|---|---|
valueChanged(int) |
Emitted when the slider value changes (by user or programmatically) |
sliderPressed() |
Emitted when the user starts dragging the handle |
sliderMoved(int) |
Emitted when the slider is dragged by the user (not on setVBalue()
|
sliderReleased() |
Emitted when the user releases the handle |
![]() |
You are given the task to let the user set a label’s opacity. |
To do this:
Create and configure the slider. Set the range with
setRange(min, max)In this example we use 0-255 because we want to control the alpha channel of a color in a stylesheet (rgba(0, 128, 0, alpha)). We start at 255 (fully opaque) and make the slider horizontal.Create a slot to react to value changes. The slot receives an
int. Here qe dynamically update aQLabels stylesheet, inserting the slider value as the alpha component of anrgba()color.Connect the signal to the slot.

6.4 QDial
The QDial class provides a rounded range control. Unlike the QSlider, which is linear, QDial lets the user “turn” a value by dragging around a circular widget.
![]() |
You need to create a sound volume control in your app. |
To do this:
Create and configure the dial.
Create a slot to respond to the dial value changes.
Connect the signal to the slot.
In the example, turning the QDial updates a QProgressBar in real time.

