2

So I wanted to add an additional text to my QListWidget list with code like this:

for (int i = 0; i < ui->history->count(); i++ )
{
    ui->history->item(i)->text().append(QTime::currentTime().toString());
}

This does not worked.

I've qDebugged all list items with this code:

qDebug() << "item(" << i << ")->text() : " << ui->history->item(i)->text();

After that i received this output:

item( 0 )->text() :  "http://www.google.ru/?gfe_rd=cr&ei=cT6wV9PDKI-8zAXjlaCIDw"
item( 1 )->text() :  "https://news.google.ru/nwshp?hl=ru&tab=wn"  
item( 2 )->text() :  "https://news.google.ru/news?pz=1&hl=ru&tab=nn"
item( 3 )->text() :  "https://news.google.ru/news?pz=1&hl=ru&tab=nn"

Obviously this function outputs all text of item, so why cannot I append any other string there?

skypjack
  • 49,335
  • 19
  • 95
  • 187

2 Answers2

3

Implicit sharing ensures the text is not directly changed. You have to explicitly set the text value:

QString txt = ui->history->item(i)->text().append(QTime::currentTime().toString());
ui->history->item(i)->setText (txt);
jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
0

text() returns the text by value, not by reference. You need to use setText to modify the text.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313