fix: Fix size of Slider label to not ocupy to much space (#304)

* fix: Fix size of Slider label to not ocupy to much space

* Update src/superqt/sliders/_labeled.py

---------

Co-authored-by: Talley Lambert <talley.lambert@gmail.com>
This commit is contained in:
Grzegorz Bokota
2025-08-12 18:56:44 +02:00
committed by GitHub
parent 246feb5a89
commit 0d3f9319fc

View File

@@ -14,7 +14,7 @@ from qtpy.QtWidgets import (
QLineEdit,
QSlider,
QStyle,
QStyleOptionSpinBox,
QStyleOption,
QVBoxLayout,
QWidget,
)
@@ -809,7 +809,7 @@ class SliderLabel(QLineEdit):
with signals_blocked(self):
self.clearFocus()
def _update_size(self, *_: Any) -> None:
def _get_size(self):
# fontmetrics to measure the width of text
fm = QFontMetrics(self.font())
h = self.sizeHint().height()
@@ -828,12 +828,14 @@ class SliderLabel(QLineEdit):
w += 3 # cursor blinking space
# get the final size hint
opt = QStyleOptionSpinBox()
opt = QStyleOption()
# self.initStyleOption(opt)
size = self.style().sizeFromContents(
QStyle.ContentsType.CT_SpinBox, opt, QSize(w, h), self
return self.style().sizeFromContents(
QStyle.ContentsType.CT_LineEdit, opt, QSize(w, h), self
)
self.setFixedSize(size)
def _update_size(self, *_: Any) -> None:
self.setFixedSize(self._get_size())
def validate(
self, input_: str | None, pos: int
@@ -843,6 +845,12 @@ class SliderLabel(QLineEdit):
return QValidator.State.Invalid, input_, len(input_)
return super().validate(input_, pos)
def showEvent(self, event: Any) -> None:
# need to update size after showing
# to handle the qss font size change
super().showEvent(event)
self._update_size()
def _fm_width(fm: QFontMetrics, text: str) -> int:
if hasattr(fm, "horizontalAdvance"):