PetukhovLive PetukhovLive - 3 months ago 19
C++ Question

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
qDebug
ged
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?

Answer

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