Issue
Say I have a treeWidget with 6 rows and 6 columns. I have set the selection behaviour to select individual items as opposed to the entire row. This works as expected and I can select individual cells. My question is how do I query what cells are currently selected?
I've tried using treeWidget.selectedItems(), but this returns a list treeWidgetItems, which from what I can tell, corresponds to the whole row. So if I select several columns in the same row, this command returns a list containing a single treeWidgetItem. And I can find no way to find out which columns are selected within that treeWidgetItem.
Any ideas how to go about this?
Solution
You may need to use the QItemSelectionModel
and call the method selectedIndexes()
Reference: http://doc.qt.io/qt-5/qitemselectionmodel.html#selectedIndexes
QItemSelectionModel* model = myTreeWidget->selectionModel();
QModelIndexList indexList = model->selectedIndexes();
for(int i = 0; i < indexList.size(); ++i) {
QTreeWidgetItem* item = myTreeWidget->itemFromIndex( indexList[i] );
// do something with each item
}
Also the individual indexes in the list will contain .row()
and .column()
information that may be helpful.
Edit
As example of using .column()
from the index:
for(int i = 0; i < indexList.size(); ++i) {
QTreeWidgetItem* item = myTreeWidget->itemFromIndex( indexList[i] );
int c = indexList[i].column();
QVariant d = item.data(c, Qt::DisplayRole);
d = QVariant( d.toString() + "*" )
item->setData(c, Qt::DisplayRole, d);
}
Each time this loop runs, the cells selected should add *
to the end.
edit: fixed issue with the call to item->setData( ... )
Answered By - Naxos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.