Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/superqt/sliders/_generic_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def _to_qinteger_space(self, val, _max=None):
_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:
Expand Down
36 changes: 36 additions & 0 deletions tests/zz_test_sliders/test_range_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions tests/zz_test_sliders/test_single_value_sliders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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