Ufx
Ufx

Reputation: 2695

How to make items in QListWidget multiline?

There is QListWidget. If I add an item with the text like "Line1\nLine2" I get an item containing a one string.
How can I make this item containing two strings?

Upvotes: 2

Views: 2582

Answers (2)

eyllanesc
eyllanesc

Reputation: 243897

It seems that the editor adds one more backslash as the following image shows:

enter image description here

The solution is simple, you must edit the text in Object Inspector and add it there directly as I show below:

enter image description here

Upvotes: 1

Mohammad Kanan
Mohammad Kanan

Reputation: 4582

You could add two lines to QListWidget in two way:

First:

 listWidget->addItem(tr("Line1\nLine2")); 

or

new QListWidgetItem(tr("Line4\nLine5"), listWidget);

and that should work as per common testing.

Second, and the better way is to add multiple items using addItems() with QStringList

QStringList items;
items << "Line1";
items << "Line2";
listWidget->addItems(items);

Upvotes: 1

Related Questions