PDA

View Full Version : QTreeView: selection behavior upon selected item removal



Pieter from Belgium
10th July 2007, 11:40
Hi,

When the selected item is removed from my tree model, the next item in the view gets selected automatically. I do not want this: I want no item to be selected. I have been searching for an easy way to do this, but did not find any. Is there a solution?

[Of course I can do that programmatically by subclassing the selection model, but I'd expect Qt to provide a built in way.]

Pieter

roro
10th July 2007, 11:58
Hi,

you can connect QAbstractItemModel::rowsRemoved() to QAbstractItemView::clearSelection() for instance.

xgoan
10th July 2007, 11:59
QTreeView::setCurrentIndex(QModelIndex());

Could you do this?

xgoan
10th July 2007, 11:59
Hi,

you can connect QAbstractItemModel::rowsRemoved() to QAbstractItemView::clearSelection() for instance.

Yes, better solution :)

Pieter from Belgium
10th July 2007, 13:42
What I actually want, is not that the selection is always cleared upon removing rows.

I want the selection to be maintained when I remove rows that are not selected. But if I remove the selected row, I want the selection to be cleared.

Sorry for being unclear about that in my original question.

Pieter

Pieter from Belgium
10th July 2007, 16:37
I have solved it this way in my QTreeView subclass, but there must/ought to be a way for doing this by configuring Qt objects.



void TreeView::rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end )
{
if (selectionMode() == QAbstractItemView::SingleSelection)
{
QModelIndexList selectedRows = selectionModel()->selectedRows();
if (selectedRows.size() == 1 && selectedRows[0].parent() == parent && selectedRows[0].row() >= start && selectedRows[0].row() <= end)
{
// the rows about to be removed contain the single selected item
selectionModel()->clear();
}
}
QTreeView::rowsAboutToBeRemoved (parent, start, end);
}

roro
11th July 2007, 17:00
The QAbstractItemModel::rowsAboutToBeRemoved() signal gives you in parameter the parent index and the row of removed object.
So you can connect that to your proper slot outside the treeview and decide to call clearSelection() (it is a public slot) or not following tests on removed rows.

No ? :)

But I'm not afraid about subclassing QTreeView, anyway...