EDIT: Updated with working example and example screenshots of current and desired behavior.
Description
I'm trying to get a multiline QLabel to have these behaviors:
- It should expand to fill available width.
- If its text is longer than it can show, it should show all it can (expanding to its max height) and elide the last line.
- If its text is not the longest it can show, it should show all text, but shrink its height to fit the text.
The label is in a layout. Maybe I'm thinking about this wrong, like maybe instead of a label it should be a text area wrapped in something else?
Anyway, I've found solutions that cover one or two of these behaviors, but not all three. I've tried tweaking Qt's ElidedLabel, but I can't quite figure out how to tweak it to achieve all three.
I've considered overriding sizeHint()
, but I'm not Qt-savvy enough to know if that's the right way to do this. I feel like maybe the right combination of layoutSizeConstraint
on the layout and size policies on the ElidedLabel might make this possible, but I'm not sure.
I've slightly modified Qt's ElidedLabel example code so that it doesn't take its content in its constructor.
Example code
https://gist.github.com/jahabrewer/bd0c79ac2255953eeadf87c9767ce693
Screenshots
Current behavior
Left column has text short enough that the ElidedLabel should reduce its height and cede that vertical space to the QLabel underneath it. Right column has text long enough that it's elided, which is correct/desired.
Desired behavior
(to be clear, I want a single configuration that will produce behavior like the left column when text is short and like the right column when text is long)
QLabel
with the propertyWordWrap
to true isn't enough? – Dimitry ErnotQLabel
withWordWrap
doesn't elide. – Jay