PetukhovLive
PetukhovLive

Reputation: 43

Adding text to existing item of QListWidget

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?

Upvotes: 2

Views: 2062

Answers (2)

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

Upvotes: 0

jonspaceharper
jonspaceharper

Reputation: 4367

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);

Upvotes: 3

Related Questions