Skip to content

Commit

Permalink
make it a combo
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Oct 15, 2024
1 parent 6d5814e commit 17d6a99
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/superqt/sliders/_labeled.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
QBoxLayout,
QDoubleSpinBox,
QHBoxLayout,
QLabel,
QComboBox,
QSlider,
QSpinBox,
QStyle,
Expand Down Expand Up @@ -131,7 +131,7 @@ def __getattr__(self, name: Any) -> Any:
return getattr(self._slider, name)


class TextSliderLabel(QLabel):
class TextSliderLabel(QComboBox):
editingFinished = Signal()

def __init__(
Expand All @@ -143,10 +143,15 @@ def __init__(
) -> None:
super().__init__(parent=parent)
self._slider = slider
self.setAlignment(alignment)
if connect is not None:
self.currentIndexChanged.connect(lambda: connect(self.currentIndex()))

def setValue(self, val: str) -> None:
self.setText(str(val))
self.setCurrentText(str(val))

def setAlignment(self, alignment: Qt.AlignmentFlag) -> None:
# self.lineEdit().setAlignment(alignment)
...


class SliderLabel(QDoubleSpinBox):
Expand Down Expand Up @@ -442,23 +447,13 @@ def __init__(
self._on_categories_changed(self._slider.categories())

def _on_slider_value_changed(self, v: Any) -> None:
category = self._slider.category()
self._label.setValue(str(category))
self.categoryChanged.emit(category)
self._label.setCurrentIndex(v)
self.categoryChanged.emit(self._slider.category())
self.valueChanged.emit(v)

def _setValue(self, value: Any) -> None:
"""Convert the value from float to int before setting the slider value."""
self._slider.setCategory(value)

def _on_categories_changed(self, categories: Iterable) -> None:
strings = [str(c) for c in categories]
if not strings:
self._label.setFixedWidth(0)
return
fm = QFontMetrics(self._label.font())
w = max(fm.horizontalAdvance(x) for x in strings) + 5
self._label.setFixedWidth(w)
self._label.clear()
self._label.addItems([str(c) for c in categories])
self._label.setValue(str(self._slider.category()))


Expand Down

0 comments on commit 17d6a99

Please sign in to comment.