After clearing selection, the last selected item is still selected



  • I have a tree view.

    I needed some cleverness in what was selected when the user clicked on various parts of it.

    There is a way to click in the treeview that causes the selection model to be cleared. That is, the following code is called:

    selectionModel->clearSelection();

    Immediately after this, the row that was previously highlighted with a blue background no longer has a blue background. However, it still has a dotted outline, and when the selection is fetched

    model->selectedRows()

    that row is still selected. Where my code is currently doing this:
    selectionModel->clearSelection();
    I want to actually clear the selection. I want there to be nothing selected. I'm sure that's what clearSelection is meant to do, so what's with the dotted outline and why is that row still selected?



  • This reply happens a little late, I know, but I stumbled upon the same problematic today and I think I figured it out. I guess other people can also have the same issue, so I allow myself to reply to this very old topic :

    I understood it all after reading this : http://doc.qt.io/qt-5/model-view-programming.html#handling-selections-in-item-views

    To summarize, regarding our issue which is even if we clear selection we still have a dotted outline on the item, it's because "selected" item and "current" item are two differents states in Qt.

    Unless the selection mode on your model/view is NoSelection, when you set an item as current you also set it as selected (by clicking on it or by calling setCurrentIndex() ).
    However, after clearSelection(), you only set the "selected" state to false, but this item is still your "current" item.

    The solution I found for this is to call setCurrentIndex() and pass to it an invalid index.

    Hope this helps for people who had the same problem.

    Regards,

    Laura B.



  • @LauraB said in After clearing selection, the last selected item is still selected:

    when you set an item as current you also set it as selected

    Not necessarily.

    1. select item 1 (selection = {item 1}, currentIndex = item 1)
    2. ctrl+select item 2 (selection = {item 1,item 2}, currentIndex = item 2)
    3. ctrl+deselect item 1 (selection = {item 2}, currentIndex = item 1)

Log in to reply
 

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