SoFunction
Updated on 2025-04-14

Detailed explanation of the usage example of PyQt QDoubleSpinBox control

QDoubleSpinBoxYes The control used in PyQt to enter floating point numbers, supports keyboard input and up and down arrows to adjust the value. Unlike QtSpinBox, QtSpinBox is a control used to enter integers.

Key properties and methods

QDoubleSpinBoxThe key properties and methods of   are shown in the following table:

Methods/Properties illustrate
setRange(min, max) Set the value range
setSingleStep(step) Set the step size (the amount of change that the arrow clicks once)
setDecimals(n) Set the number of decimal places
setValue(value) Set the current value
value() Get the current value
valueChanged The signal triggered when the value changes
setPrefix(text) Add prefix text
setSuffix(text) Add suffix text

Basic settings method

  • setRange(min, max)
  • Set the numerical range (minimum value min, maximum value max).
  • setMinimum(min) / setMaximum(max)
  • Set the minimum or maximum value separately.
  • setSingleStep(step)
  • Set the step size (the amount of change of the value when clicking the arrow).
  • setDecimals(prec)
  • Sets the number of decimal places displayed (such as prec=2 retains two decimal places).
  • setValue(value)
  • Set the current value directly (must make sure the value is within the range).
  • setPrefix(text) / setSuffix(text)
  • Add prefix or suffix text (such as unit symbols).

Value acquisition method

  • value()
  • Returns the current value (floating point number type).
  • textFromValue(value) / valueFromText(text)
  • Customize the conversion logic of numeric values ​​to text (for input validation or formatting).

Behavior control method

  • setKeyboardTracking(enabled)
  • Controls whether the value is updated in real time when input is entered (default True, update in real time; set to False, update after input is completed).
  • stepBy(steps)
  • Adjust the value by step increment (positive number increases, negative number decreases).
  • stepUp() / stepDown()
  • Triggers a step increase or decrease operation.

Enter verification method

  • setCorrectionMode(mode)
  • Set the input correction mode (such as automatic correction of illegal values).
  • validate(text, pos) / fixup(text)
  • Custom input verification logic (rewrite method required).

Signal

  • valueChanged(double)
  • The signal triggered when the value changes (the parameter is the current value).
  • editingFinished()
  • Triggered when the user finishes editing (such as pressing Enter or leaving focus).

Other practical methods

  • clear()
  • Clear the value (reset to minimum or 0.0, depending on range).
  • setAlignment(alignment)
  • Set text alignment (such as ).
  • setReadOnly(enabled)
  • Set to read-only mode (user cannot edit).

Usage example

Control initialization

from  import QApplication, QWidget, QDoubleSpinBox, QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
# Create control and set initial propertiesspinbox = QDoubleSpinBox()
(-100.0, 100.0)   # Set the value range(0.5)        # Set the step size (the amount of change that the arrow clicks once)(2)            # Keep two decimal places(3.14)            # Set initial value(spinbox)
(layout)
()
app.exec_()

Signal and slot connection

When the user changes the value, triggervalueChangedSignal:

def on_value_changed(value):
    print(f"Current value: {value}")
(on_value_changed)

Dynamic adjustment range

Dynamically modify the range or step size according to the conditions:

# When the value exceeds 50, the range will be automatically expandeddef check_range(value):
    if value > 50.0:
        (0.0, 100.0)
    else:
        (0.0, 50.0)
(check_range)

Format display

Add a prefix/suffix or custom format:

("temperature: ")     # Add a prefix("°C")        # Add a suffix# Use custom formats (such as currency)(0)
("$ ")

Enter verification

Restrict users from entering illegal values ​​(if automatically corrected if the range is exceeded):

(False)  # Verify after the input is completed (rather than real-time)def validate_input(value):
    if value < 0:
        return 0.0  # Automatically correct to minimum value    return value
 = lambda text: validate_input(float(text))

Advanced Usage: Custom Step Logic

Dynamically adjust the step size (such as logarithmic scale) according to the current value:

def dynamic_step(value):
    if value < 1.0:
        return 0.1
    elif value < 10.0:
        return 0.5
    else:
        return 1.0
(0.1)  # Initial step lengthdef on_step_up():
    current = ()
    step = dynamic_step(current)
    (current + step)
def on_step_down():
    current = ()
    step = dynamic_step(current)
    (current - step)
# Replace the default up and down arrow behavior(on_step_up)
(on_step_down)

This is the end of this article about the detailed explanation of the usage of PyQt QDoubleSpinBox control. For more related contents of PyQt QDoubleSpinBox control, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!