Skip to content

Add feature to allow editing a slider's value outside of its current range - #353

Open
jules-vanaret wants to merge 1 commit into
pyapp-kit:mainfrom
jules-vanaret:label-extents
Open

Add feature to allow editing a slider's value outside of its current range#353
jules-vanaret wants to merge 1 commit into
pyapp-kit:mainfrom
jules-vanaret:label-extents

Conversation

@jules-vanaret

Copy link
Copy Markdown

PR description

This is a small UX tweak for labeled range sliders.

The problem addressed is that if you edit a slider value label (the text box just above the slider knob) and type a value outside the current slider range, that value gets clamped at the slider bounds. This could be cumbersome for things like contrast-limit controls, where users might expect to be able to type a new endpoint directly instead of first adjusting the min/max edge labels (see napari/napari#9310 ).

Changes

SliderLabel.setValue() normally clamps values to the slider bounds when it refreshes labels. It now accepts a clamp_values parameter, which defaults to True to retain its existing bounds-clamping behavior for normal label updates. SliderLabel._editing_finished() passes clamp_values=False before emitting valueEdited, allowing the owning range slider to receive the raw out-of-range value and decide whether to expand its bounds.

I added an opt-in flag to the range slider itself to trigger the new behaviour:

  • expandRangeOnHandleEdit()
  • setExpandRangeOnHandleEdit(bool)

How to use it in practice

slider = QLabeledRangeSlider() # or QLabeledDoubleRangeSlider()
slider.setExpandRangeOnHandleEdit(True)

With that enabled, typing 110 into the upper handle label will expand the max to 110 and set the handle there instead of being stuck at the old bound.

Reproducer

The small example below illustrates simple- and double-sliders with and without the new range-expansion feature triggered. Try to modify e.g the value of one of the slider above 100 and observe the behavior.

import sys
from qtpy.QtWidgets import QApplication, QVBoxLayout, QWidget, QLabel
from superqt import QLabeledDoubleRangeSlider, QLabeledRangeSlider


class Reproducer(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout(self)

        self.default_slider = QLabeledRangeSlider()
        self.default_slider.setRange(0, 100)
        self.default_slider.setValue((50,))
        self.default_slider.setExpandRangeOnHandleEdit(False)

        self.default_doubleslider = QLabeledDoubleRangeSlider()
        self.default_doubleslider.setRange(0, 100)
        self.default_doubleslider.setValue((20, 80))
        self.default_doubleslider.setExpandRangeOnHandleEdit(False)

        self.expanding_slider = QLabeledRangeSlider()
        self.expanding_slider.setRange(0, 100)
        self.expanding_slider.setValue((50,))

        self.expanding_doubleslider = QLabeledDoubleRangeSlider()
        self.expanding_doubleslider.setRange(0, 100)
        self.expanding_doubleslider.setValue((20, 80))

        layout.addWidget(QLabel("This is a simple slider without the new feature:"))
        layout.addWidget(self.default_slider)
        layout.addWidget(QLabel("This is a double slider without the new feature:"))
        layout.addWidget(self.default_doubleslider)
        layout.addWidget(QLabel("This is a simple slider with the new feature:"))
        layout.addWidget(self.expanding_slider)
        layout.addWidget(QLabel("This is a double slider with the new feature:"))
        layout.addWidget(self.expanding_doubleslider)


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

@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 55.00000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.77%. Comparing base (a175047) to head (e1aa033).

Files with missing lines Patch % Lines
src/superqt/sliders/_labeled.py 55.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #353      +/-   ##
==========================================
- Coverage   84.90%   84.77%   -0.13%     
==========================================
  Files          49       49              
  Lines        3928     3941      +13     
==========================================
+ Hits         3335     3341       +6     
- Misses        593      600       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant