QLabel Fixed count of Rows
-
I want to have a Qt Label with a fixed size which can display
n
lines of text.My hacky solution for this was this (with
n = 4
):QFontMetrics fm{ mAnnouncementLabel->font() }; mAnnouncementLabel->setFixedHeight(fm.horizontalAdvance("Test") * 4);
So what is the correct way to limit a QLabel to n lines of text?
-
@sandro4912 Depending on what you want to do you could use one QLabel for each line :-)
-
@sandro4912 said in QLabel Fixed count of Rows:
horizontalAdvance
QFontMetrics::horizontalAdvance()
looks like it returns some horizontal distance? Presumably you rather want something which measures the font vertically, and (I don't know) includes the inter-line gap, if applicable? I'm thinkingQFontMetrics::lineSpacing() * 4
? -
@JonB That did the job now the label height looks correct for 4 lines.
@jsulm I thought several Labels is a bit overkill. For now i build a string together with
\n
at the end depending on how many messages show up (Maximum can be 4). I guess I could also pack theQLabel
in aQVector
and display / fill that.