For example, we have a QLabel
with MaximumWidth set to 400.
When we try to display some text with pixel width more than 400, it's shown cut off.
Is there any way to make QLabel display this string in multiple lines without using QFontMetrics or the like?
Asked
Active
Viewed 6.9k times
65
3 Answers
121
If I understood your question correctly, you should use the setWordWrap
function for your label, with true
as its parameter.
QLabel lbl("long long string");
lbl.setWordWrap(true);

Alex Feinman
- 5,393
- 1
- 30
- 48

s4eed
- 7,173
- 9
- 67
- 104
-
5With .setWordWrap(true) you can also use new line (\n) to break line if needed. With expanding vertical policy it is for me the best way to create plain text info area (non-selectable and non-editable) – Miro Feb 26 '20 at 03:07
13
In order to show multiple lines in QLabel, right click on QLabel and select 'change rich text'. This brings up dialog where you can type the text as you want to see including enter key. Setting the word wrap is not required for this.
If you set the word wrap as well (in QLabel properties) than it will wrap each individual line in the Qlabel if it was longer than the real estate.

zar
- 11,361
- 14
- 96
- 178
1
As another option to wrap text using Qt Designer, you can check the box under Property Editor for a QLabel:

Momen
- 107
- 3
- 10