How to get required height for a QLabel without showing the label?
-
Suppose I have a
QLabel
with somealignment
andwordWrap
asTrue
, I have fixed width for it, Is there any way I can get theheight required
for this label without displaying the label?label = QLabel() label.setAlignment(Qt.AlignLeft) label.setWordWrap(True) label.setFixedWidth(60) label.setText('Analysis Reference Range Indicator (ANRIND)')
I need to estimate the height required for a text with wrapping when width is given. I tried
QFontMetrics()
, it is able to give the height, width and the bounding rectangle, however I'm interested on height the text will require for a given width.I tried
label.height()
but it is always480
, I guess it's because it hasn't been shown yet. -
@JonB said in How to get required height for a QLabel without showing the label?:
And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.
QFontMetrix is not limited to a single line of text, here's an excellent example from SO:
-
@JoeCFD It's a local variable, it's sole purpose is to estimate the height, it is neither added to any layout, not is it displayed to the user, so I don't think that it is going to work in the
paintEvent
, actually for this label,paintEvent
isn't even going to call I guess, since it's locally created and deleted without displaying it. -
@ThePyGuy
I believe you are actually saying you want to know what the height will be when it word wraps. So far as I know, you cannot get the height till it is shown. Because it's not calculated till then.resizeEvent
is not called till it's shown. -
@ThePyGuy
And I'm saying I believe there is not. This kind of question has been asked before, and that has always been the answer. And it has been what I have found in my code. Even more so with word wrapping, I can imagine Qt won't even calculate that until it really has to show the item. Unless it has some ability to output to a "hidden" or "null" device in memory where you could measure. But nobody has ever mentioned that Qt has such before. And if you do need to calculate it has always been said you would have to useQFontMetrics
and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.The obvious question is why you need to know this before it is shown? Because I don't think you're going to be able to get it.
-
@JonB said in How to get required height for a QLabel without showing the label?:
And if you do need to calculate it has always been said you would have to use QFontMetrics and do it yourself, which as you say would give you one line's height, but knows nothing about where it will be wrapped to spill across multiple lines.
QFontMetrix is not limited to a single line of text, here's an excellent example from SO:
-
@J-Hilk said in How to get required height for a QLabel without showing the label?:
QFontMetrix is not limited to a single line of text
I knew that, but I did not think it had access to where the Qt code would choose to do the word wrapping. However, from the link you reference I see
The
rect
argument forQFontMetrics::boundingRect
constrains the layout of the input text. You can useQt::TextWordWrap
flag to wrap long lines to multiple rows within the constraint rect.@ThePyGuy
ThatQt::TextWordWrap
flag could be just what you are looking for! Have a look at the accepted solution in that stackoverflow topic. A comment there statesNo problem. If I add a
QLabel label; label.setText(text); label.show(); qDebug() << label.rect();
it prints exactly the same rect as for the font metrics.So that's indicating it should come up with the same answer as it will use when the label actually shown.
-
@JonB I need it because I need to estimate the height required for a given text with wrapping, I'm trying to solve How to make QTableView to adjust the height of it's horizontal header automatically in PyQt? , One possible solution in my mind was to estimate the height for given width for the header text, and then to estimate the height accordingly.