Search code examples
c++qtqt5qt5.5

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?


Solution

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