8

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().

But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.

Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)

Voo
  • 26,852
  • 9
  • 70
  • 145

1 Answers1

11

Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

Arnold Spence
  • 20,892
  • 7
  • 69
  • 66
  • Strange I tried Ignored and while it allowed me to shrink the widget correctly, it lead to a strange graphical appearance because the widget no longer filled the complete horizontal space (which I put totally had forgotten by the time I posted it, I'm sorry for that!). I just tried it again and.. it works - in german we would say "Vorführeffekt" to that ;) +1 and accepted – Voo Jul 17 '11 at 02:12
  • 1
    I enjoyed looking that word up :) – Arnold Spence Jul 16 '11 at 21:13