diff --git a/src/superqt/sliders/_generic_slider.py b/src/superqt/sliders/_generic_slider.py index f5c8ee2..fbc0229 100644 --- a/src/superqt/sliders/_generic_slider.py +++ b/src/superqt/sliders/_generic_slider.py @@ -349,7 +349,7 @@ class _GenericSlider(QSlider): _max = _max or self.MAX_DISPLAY range_ = self._maximum - self._minimum if range_ == 0: - return self._minimum + return 0 return int(min(QOVERFLOW, val / range_ * _max)) def _pick(self, pt: QPoint) -> int: diff --git a/tests/zz_test_sliders/test_range_slider.py b/tests/zz_test_sliders/test_range_slider.py index f2f9ea6..af9e37e 100644 --- a/tests/zz_test_sliders/test_range_slider.py +++ b/tests/zz_test_sliders/test_range_slider.py @@ -254,3 +254,39 @@ def test_rangeslider_signals(cls, orientation, qtbot): sld.setRange(1, 2) mock.assert_called_once_with(1, 2) _assert_types(mock.call_args.args, type_) + + +@pytest.mark.parametrize("cls, orientation", ALL_SLIDER_COMBOS) +def test_range_slider_with_equal_min_max(cls, orientation, qtbot): + """Test that slider works when min == max (issue #307). + + Previously, this would raise a TypeError: 'float' object cannot be + interpreted as an integer when calling show() because _to_qinteger_space + returned a float instead of an int when range was 0. + """ + sld = cls(orientation) + qtbot.addWidget(sld) + + # Test with min=max=99 (the specific case from issue #307) + with qtbot.waitSignal(sld.rangeChanged): + sld.setMinimum(99) + + # This should not raise a TypeError + sld.show() + + # Verify the slider state + assert sld.minimum() == 99 + assert sld.maximum() == 99 + assert sld.value() == (99, 99) + + # Test that we can also set max first + sld2 = cls(orientation) + qtbot.addWidget(sld2) + + with qtbot.waitSignal(sld2.rangeChanged): + sld2.setMaximum(0) + + sld2.show() + assert sld2.minimum() == 0 + assert sld2.maximum() == 0 + assert sld2.value() == (0, 0) diff --git a/tests/zz_test_sliders/test_single_value_sliders.py b/tests/zz_test_sliders/test_single_value_sliders.py index e3738fe..295d350 100644 --- a/tests/zz_test_sliders/test_single_value_sliders.py +++ b/tests/zz_test_sliders/test_single_value_sliders.py @@ -229,3 +229,32 @@ def test_slider_extremes(sld: _GenericSlider, mag, qtbot): for i in _linspace(-_mag, _mag, 10): sld.setValue(i) assert math.isclose(sld.value(), i, rel_tol=1e-8) + + +def test_slider_with_equal_min_max(sld: _GenericSlider, qtbot): + """Test that slider works when min == max (issue #307). + + Previously, this would raise a TypeError: 'float' object cannot be + interpreted as an integer when calling show() because _to_qinteger_space + returned a float instead of an int when range was 0. + """ + # Test with min=max=99 (the specific case from issue #307) + with qtbot.waitSignal(sld.rangeChanged, timeout=400): + sld.setMinimum(99) + + # This should not raise a TypeError + sld.show() + + # Verify the slider state + assert sld.minimum() == 99 + assert sld.maximum() == 99 + assert sld.value() == 99 + + # Test that we can also set max first + with qtbot.waitSignal(sld.rangeChanged, timeout=400): + sld.setMaximum(0) + + sld.show() + assert sld.minimum() == 0 + assert sld.maximum() == 0 + assert sld.value() == 0