PyQt - QDoubleSpinBox Widget



The QDoubleSpinBox widget in PyQt is used to input floating point number within a specified range. It is inherited from QAbstractSpinBox which is further inherited from Qwidgets. It supports various functionalities, including keyboard and mouse interaction, validation, and rounding behavior.

Key Features of QDoubleSpinBox

  • Precision Control − Developers can specify the number of decimals to display. This provides control over the precision of the floating-point numbers.
  • Range Specification − Both the minimum and maximum allowed values can be set, defining the ideal range for user input.
  • Keyboard and Mouse Interaction − Users can interact with the QDoubleSpinBox using keyboard arrow keys or the mouse wheel to increment or decrement the value.
  • Signals and Slots − PyQt6 provides a signal (valueChanged) that can be connected to a slot to perform actions whenever the value changes.
  • Localized Number Display − The QDoubleSpinBox uses the locale settings, ensuring that the decimal separator and grouping character are displayed according to the user's locale.

Setting the maximum value

To set the maximum value in a QDoubleSpinBox we can use the setMaximum() method. This method accepts a single argument the maximum value you wish to allow the user to input. By setting the maximumvalue, you restrict the user from entering or selecting a value beyond the specified limit.

Syntax

It's syntax is as follows −

your_double_spin_box.setMaximum(max_value)

Here, your_double_spin_box should be replaced with the actual name of your QDoubleSpinBox object, andmax_value should be the desired maximum value.

Example 1: Basic Implementation of QDoubleSpinBox

In the below example, we create a basic implementation of a PyQt6 application featuring a QDoubleSpinBox widget and a QLabel. The QDoubleSpinBox allows users to select a floating-point value within the range of 0 to 100, and the selected value is displayed in the QLabel.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QDoubleSpinBox, QLabel, QVBoxLayout, QWidget

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()
      self.init_ui()

   def init_ui(self):
      central_widget = QWidget(self)
      self.setCentralWidget(central_widget)

      layout = QVBoxLayout()

      self.double_spin_box = QDoubleSpinBox(self)
      self.double_spin_box.setMinimum(0)
      self.double_spin_box.setMaximum(100)  # Setting the maximum value to 100

      self.label = QLabel(self)
      self.label.setText("Selected value: ")

      layout.addWidget(self.double_spin_box)
      layout.addWidget(self.label)

      central_widget.setLayout(layout)

      self.double_spin_box.valueChanged.connect(self.update_label)

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

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following result −

pyqt qdoublespinbox example 1

Example 2: Customized Maximum Value

In the below example, the maximum value of the QDoubleSpinBox is customized to 50, allowing users to select values within the range of -50 to 50. Similar to Example 1, the selected value is displayed in a QLabel.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QDoubleSpinBox, QLabel, QVBoxLayout, QWidget

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()
      self.init_ui()

   def init_ui(self):
      central_widget = QWidget(self)
      self.setCentralWidget(central_widget)

      layout = QVBoxLayout()

      self.double_spin_box = QDoubleSpinBox(self)
      self.double_spin_box.setMinimum(-50)
      self.double_spin_box.setMaximum(50)  # Setting the maximum value to 50

      self.label = QLabel(self)
      self.label.setText("Selected value: ")

      layout.addWidget(self.double_spin_box)
      layout.addWidget(self.label)

      central_widget.setLayout(layout)

      self.double_spin_box.valueChanged.connect(self.update_label)

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

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

On execution of above code, we get the following result −

pyqt qdoublespinbox example 2

Example 3: Handling Large Ranges

In the below example, we showcase the ability of the QDoubleSpinBox to handle larger ranges by setting the maximum value to 1000. Users can select values within the range of 0 to 1000, and the selected value is updated in real-time in a QLabel.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QDoubleSpinBox, QLabel, QVBoxLayout, QWidget

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

      self.init_ui()

   def init_ui(self):
      central_widget = QWidget(self)
      self.setCentralWidget(central_widget)

      layout = QVBoxLayout()

      self.double_spin_box = QDoubleSpinBox(self)
      self.double_spin_box.setMinimum(0)
      self.double_spin_box.setMaximum(1000)  # Setting the maximum value to 1000

      self.label = QLabel(self)
      self.label.setText("Selected value: ")

      layout.addWidget(self.double_spin_box)
      layout.addWidget(self.label)

      central_widget.setLayout(layout)

      self.double_spin_box.valueChanged.connect(self.update_label)

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

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

On execution of above code, we get the following result −

pyqt qdoublespinbox example 3
Advertisements