PyQt - QSpinBox Widget



A QSpinBox object presents the user with a textbox which displays an integer with up/down button on its right. The value in the textbox increases/decreases if the up/down button is pressed.

By default, the integer number in the box starts with 0, goes upto 99 and changes by step 1. Use QDoubleSpinBox for float values.

Methods Used in QSpinBox Widget

Important methods of QSpinBox class are listed in the following table −

Sr.No. Methods & Description
1

setMinimum()

Sets the lower bound of counter

2

setMaximum()

Sets the upper bound of counter

3

setRange()

Sets the minimum, maximum and step value

4

setValue()

Sets the value of spin box programmatically

5

Value()

Returns the current value

6

singleStep()

Sets the step value of counter

QSpinBox object emits valueChanged() signal every time when up/own button is pressed. The associated slot function can retrieve current value of the widget by value() method.

Basic Structure of QSpinBox

Before getting into the examples, let's understand the basic structure of a QSpinBox widget.

In this code snippet we're creating a PyQt window featuring a QSpinBox widget. Next we define the upper boundaries, for the spin box by using the setMinimum() and setMaximum() functions. Additionally we initialize the spin box starting value with setValue().

from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox

class MyWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("Basic QSpinBox Example")
      self.setGeometry(200, 200, 300, 200)

      spinbox = QSpinBox(self)
      spinbox.move(100, 50)
      spinbox.setMinimum(0)
      spinbox.setMaximum(100)
      spinbox.setValue(50)

if __name__ == "__main__":
   app = QApplication([])
   window = MyWindow()
   window.show()
   app.exec()

Output

The above code produces the following output −

pyqt qspinBox basic usage

Example 1: Customized QSpinBox

In the below example, we show the customization options for the QSpinBox widget. We set the minimum and maximum values to -100 and 100, respectively. Additionally, we adjust the step size for incrementing or decrementing values using setSingleStep().

from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox

class MyWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("Customized QSpinBox Example")
      self.setGeometry(200, 200, 300, 200)

      spinbox = QSpinBox(self)
      spinbox.move(100, 50)
      spinbox.setMinimum(-100)
      spinbox.setMaximum(100)
      spinbox.setSingleStep(5)
      spinbox.setValue(0)

if __name__ == "__main__":
   app = QApplication([])
   window = MyWindow()
   window.show()
   app.exec()   

Output

The above code produces the following output −

pyqt qspinBox example 1

Example 2: QSpinBox Signal Handling

In this example, we connect the valueChanged signal of the QSpinBox widget to a custom slot on_value_changed. This slot updates a QLabel with the currently selected value whenever the value in the spin box changes.

from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox, QLabel

class MyWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("QSpinBox Signal Handling Example")
      self.setGeometry(200, 200, 300, 200)

      spinbox = QSpinBox(self)
      spinbox.move(100, 50)
      spinbox.setMinimum(0)
      spinbox.setMaximum(100)
      spinbox.valueChanged.connect(self.on_value_changed)

      self.label = QLabel(self)
      self.label.move(100, 100)
      self.label.setText("Selected Value: 0")

   def on_value_changed(self, value):
      self.label.setText(f"Selected Value: {value}")

if __name__ == "__main__":
   app = QApplication([])
   window = MyWindow()
   window.show()
   app.exec()

Output

The above code produces the following output −

pyqt qspinBox example 2
Advertisements