Originally Posted by
user
Should I save the QModelIndexList from before or is there a better way?
e.g what is the right way to select items 2-5,6 in my list?
QModelIndex is a temporary object. It shouldn't be stored because it might get invalid as soon as the model changes. I'd suggest identifying the items in another way.
// set id
int id = 123;
item->setData(Qt::UserRole, id);
// set id
int id = 123;
item->setData(Qt::UserRole, id);
To copy to clipboard, switch view to plain text mode
// get selected ids
QList<int> ids;
ids += item->data(Qt::UserRole).toInt();
// get selected ids
QList<int> ids;
foreach (QListWidgetItem* item, listWidget->selectedItems())
ids += item->data(Qt::UserRole).toInt();
To copy to clipboard, switch view to plain text mode
// set selected ids (inefficient if there are lots of items)
for (int i = 0; i < listWidget->count(); ++i)
{
int id = item->data(Qt::UserRole).toInt();
item->setSelected(ids.contains(id));
}
// set selected ids (inefficient if there are lots of items)
for (int i = 0; i < listWidget->count(); ++i)
{
QListWidgetItem* item = listWidget->item(i);
int id = item->data(Qt::UserRole).toInt();
item->setSelected(ids.contains(id));
}
To copy to clipboard, switch view to plain text mode
Bookmarks