Reputation: 3173
I have a QTreeWidget
and I want certain rows to be non select-able, which can be achieved by QTreeWidgetItem::setFlags(treeWidgetItem->flags() & ~Qt::ItemIsSelectable).
The problem is that I have an existing row that is already selected and later I click on the non select-able row, selectedItems()
returns an empty list. I want the selected row to keep its selection if the user tries to select a non select-able row.
Should I keep track of the selection and handle this scenario in the code, or this can be achieved somehow else. I'd rather not reinvent the wheel.
Thank you.
Upvotes: 3
Views: 2706
Reputation: 8429
Calling QTreeView::mousePressEvent(event)
clears the selection when clicked on a non-selectable item if the selection mode is set to QAbstractItemView::SingleSelection
.
My solution would be to either:
QAbstractItemView::MultiSelection
,or (in case this is not desired):
Note: In either case, use the QItemSelectionModel::selectionChanged
signal to get the list of the selected items.
Here is an example re-implementation of the mouse events in MyTreeWidget preventing the selection of being cleared by clicking a non-selectable item. The top item is expanded/collapsed on a double click:
void MyTreeWidget::mousePressEvent(QMouseEvent *event)
{
if (indexAt(event->pos())->flags() & Qt::ItemIsSelectable)
QTreeWidget::mousePressEvent(event);
}
void MyTreeWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
QTreeWidget::mouseDoubleClickEvent(event);
QTreeWidgetItem *item = itemAt(event->pos());
if (item && item->childCount())
item->setExpanded(!item->isExpanded());
}
The modified in the described manner version of the provided example is available on GitHub.
Special thanks to @eyllanesc for making this example more waterproof by:
item
is not NULL
itemAt
with indexAt
Upvotes: 2