Adding item to beginning of QListWidget doesn't work for me [Solved]
Hi all. Hopefully I'm doing something completely wrong here - maybe someone could be friendly enough pointing out where.
I have a QListWidget. I want to prepend items in the list widget. Docs tells me that QListWidget::insertItem is my friend. I insert an item thusly, ie. at list index 0:
@QListWidgetItem* widgetItem = new QListWidgetItem(ui->listWidget);
qDebug() << "Inserting item at index 0 (hopefully): " << widgetItem->text();
qDebug() << "Item at index 0: " << ui->listWidget->item(0)->text();
qDebug() << "Index for inserted widget: " << ui->listWidget->row(widgetItem);@
The debug statements print out the following:
Inserting item at index 0 (presumably): "Retarded Tests"
Item at index 0: "Color Blindness test - Real !"
Index for inserted widget: 44
As you can see, inserting at index 0 does not give me the desired result. It happily inserts at the bottom of the list. I tried inserting at index -1 instead, same result. On a side note, I am setting an item widget for each list entry, but this should have any effect on the way an item is inserted?
Edit: Qt 5.1, tested in Mac OS and Windows
Please remove ui->listWidget from your following statement.
QListWidgetItem* widgetItem = new QListWidgetItem(ui->listWidget);
Read the documentation of QListWidgetItem's contructor and QListWidget::addItem, you can find the answer.
Aha! It works! You're right, the doc for QListWidgetItem's constructor says that the item is added if it has a parent.
May thanks! :-)