Solved QLabel adjustSize
-
According to:
https://doc.qt.io/qt-5.9/qwidget.html#adjustSize
Calling adjustSize, "adjusts the size of the widget to fit its contents.", however I have a QLabel, I set the font size in CSS before calling adjustSize, it does not take into account the font size and the geometry is not large enough to accommodate a larger font.
I set the font size with:
font-size:36pt;
-
After revisiting my work and redesigning my class, with the CSS set before the text and geometry, the adjustSize() works correctly and does take into account the font size.
-
@SPlatten
Possible suggestions:-
adjustSize()
cannot take into account QSS stylesheet, only Qt style &sizeHint()
stuff? -
Try the appropriate
QWidget::ensurePolished()
/polish()
. I don't think it's this case, but when you do dynamic stylesheet stuff there are situations where you must do this to get Qt to properly take notice. -
Test whether
adjustSize()
does get it right if you call it (e.g. from aQTimer
) after the label has been allowed to actually render its newly-sized text?
-
-
I did add a 1000 ms timer to call adjustSize and report the geometry in the debugger. The result was that the widget and geometry are now perfectly correct which suggests something else is not set before the initial adjustSize is called.
-
@SPlatten
In some shape or form, Qt does/can not calculate size till a widget is actually shown. Presumably hence the behaviour you see. -
@SPlatten
A text QLabel's size hint is not effective until showEvent. -
-
@Bonnie said in QLabel adjustSize:
A text QLabel's size hint is not effective until showEvent.
But (I read it as --- or maybe this is not what @SPlatten meant?) the
QLabel
is already shown, but a stylesheet is now being set which will change its size. In which case, it won't get a newshowEvent()
to tell it when it's ready toadjustSize()
, will it? -
@JonB
If you set the style sheet after showEvent, then, yes.
I think the best way is reacting to it's parent widget'sQEvent::LayoutRequest
. -
@Bonnie said in QLabel adjustSize:
I think the best way is reacting to it's parent widget's QEvent::LayoutRequest.
Is there anything you don't know? ;-)
-
@JonB
Of course! A lot!
I did spend quite some time looking into the size hint changing thing of QLabel though, it's quite annoying. -
After revisiting my work and redesigning my class, with the CSS set before the text and geometry, the adjustSize() works correctly and does take into account the font size.