2

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?

6
  • did you try \n\r Commented Mar 17, 2018 at 14:57
  • @MohammadKanan did not help Commented Mar 17, 2018 at 14:59
  • 1
    How you add the item? where is your code? Commented Mar 17, 2018 at 15:11
  • 1
    I added an item to the QListWidget in the following way: some_list_widget.addItem(QString("Line1\nLine2")); and I get what you want: imgur.com/a/TFv5a , You could give more detail. Commented Mar 17, 2018 at 15:14
  • @eyllanesc Wow. Same result. Why if add the item using QtDesigner I don't get the same result? Commented Mar 17, 2018 at 19:44

2 Answers 2

1

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); 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.