Skip to content

Commit

Permalink
fix: minimum size hint for QElidingLabel (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer authored Nov 26, 2024
1 parent 952ac33 commit e7a8789
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/superqt/elidable/_eliding_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ def sizeHint(self) -> QSize:
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())
11 changes: 11 additions & 0 deletions tests/test_eliding_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e7a8789

Please sign in to comment.