fix: minimum size hint for QElidingLabel (#260)

This commit is contained in:
Gabriel Selzer
2024-11-26 15:53:12 -06:00
committed by GitHub
parent 952ac336bf
commit e7a87897f5
2 changed files with 18 additions and 0 deletions

View File

@@ -73,3 +73,10 @@ class QElidingLabel(_GenericEliding, QLabel):
flags = int(self.alignment() | Qt.TextFlag.TextWordWrap)
r = fm.boundingRect(QRect(QPoint(0, 0), self.size()), flags, self._text)
return QSize(self.width(), r.height())
def minimumSizeHint(self) -> QSize:
# The smallest that self._elidedText can be is just the ellipsis.
fm = QFontMetrics(self.font())
flags = int(self.alignment() | Qt.TextFlag.TextWordWrap)
r = fm.boundingRect(QRect(QPoint(0, 0), self.size()), flags, "...")
return QSize(r.width(), r.height())

View File

@@ -69,3 +69,14 @@ def test_wrap_text():
assert isinstance(wrap, list)
assert all(isinstance(x, str) for x in wrap)
assert 9 <= len(wrap) <= 13
def test_minimum_size_hint():
# The hint should always just be the space needed for "..."
wdg = QElidingLabel()
size_hint = wdg.minimumSizeHint()
# Regardless of what text is contained
wdg.setText(TEXT)
new_hint = wdg.minimumSizeHint()
assert size_hint.width() == new_hint.width()
assert size_hint.height() == new_hint.height()