When using qlistwdiget inser for an item, the following code has a small problem.
listWidget->insertItem(row, new QListWidgetItem(listWidget))
This Code makes the insert Item always at the end of the listwidget. this is a very simple and frequently-used API. It is too simple to be simple, but unexpected problems have occurred. see the document "inserts the item at the position in the list given by row. "No nutrition. read the source code of qlistwidget and find the producer. The insert code snippet is as follows:
void QListModel::insert(int row, QListWidgetItem *item){ if (!item) return; item->view = qobject_cast<QListWidget*>(QObject::parent()); if (item->view && item->view->isSortingEnabled()) { // sorted insertion QList<QListWidgetItem*>::iterator it; it = sortedInsertionIterator(items.begin(), items.end(), item->view->sortOrder(), item); row = qMax(it - items.begin(), 0); } else { if (row < 0) row = 0; else if (row > items.count()) row = items.count(); } beginInsertRows(QModelIndex(), row, row); items.insert(row, item); item->d->theid = row; endInsertRows();}The original problem is very simple. During the insert operation, the system determines whether the item specifies the parent. if the parent is specified, the row is set to the maximum value. if no parent exists, the specified row is used. in my code, parent is specified during the new qlistwidgetitem operation, which causes this problem. modify the code
listWidget->insertItem(row, new QListWidgetItem())
Solve the problem!
Qlistwdiget insertitem sequence Problem