PetukhovLive PetukhovLive - 1 year ago 108
C++ Question

Adding text to existing item of QListWidget

So I wanted to add an additional text to my

list with code like this:

for (int i = 0; i < ui->history->count(); i++ )

This does not worked.

all list items with this code:

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

After that i received this output:

item( 0 )->text() : ""
item( 1 )->text() : ""
item( 2 )->text() : ""
item( 3 )->text() : ""

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

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download