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;
@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.
-
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.
-
@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? -
@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? ;-)