[Solved] QTreeView, disable deselection ability



  • Qt version = 5.2.1

    Hello.
    I have QTreeView instance with few rows of data which cannot be deleted because "read-only" mode is turned on. When program starts it automatically selects 1st row using QItemSelectionModel::setCurrentIndex. Now I need to disable ability to select "nothing" by clicking on empty area of table or by another method. In other words my QTreeView instance must always have one of the items as current item. In other words it's selectionModel()->currentIndex() must always return valid index.
    I hope this is clear description of my goal. So how can I do it?



  • I think you can create your own class from QTreeView and implement its mousePressEvent(QMouseEvent *mouseEvent).

    Inside your mousePressEvent method you can get the coordinate of the mousepress event and use QTreeView::indexAt(const QPoint & point).

    If index is invalid simply do not allow mouseEvent to propagated by setting: mouseEvent->accept(); And if it is on a valid index simply pass the event through with QListView::mousePressEvent(mouseEvent)

    The downside here is that mouse clicks on scrollbar areas might be caught as well, but I am sure you can get around it though



  • You just need to override selectionChanged() and skip it, if the new selection is empty.



  • Thanks for answers. Overrideing selectionChanged is very easy to implement and it works for me.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.