Results 1 to 7 of 7

Thread: QTreeView: selection behavior upon selected item removal

  1. #1
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView: selection behavior upon selected item removal

    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

  2. #2
    Join Date
    Jun 2007
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    Hi,

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

  3. #3
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    QTreeView::setCurrentIndex(QModelIndex());

    Could you do this?

  4. #4
    Join Date
    Jul 2006
    Posts
    126
    Thanks
    17
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    Quote Originally Posted by roro View Post
    Hi,

    you can connect QAbstractItemModel::rowsRemoved() to QAbstractItemView::clearSelection() for instance.
    Yes, better solution

  5. #5
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Genk, Belgium
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    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.

    Qt Code:
    1. void TreeView::rowsAboutToBeRemoved ( const QModelIndex & parent, int start, int end )
    2. {
    3. if (selectionMode() == QAbstractItemView::SingleSelection)
    4. {
    5. QModelIndexList selectedRows = selectionModel()->selectedRows();
    6. if (selectedRows.size() == 1 && selectedRows[0].parent() == parent && selectedRows[0].row() >= start && selectedRows[0].row() <= end)
    7. {
    8. // the rows about to be removed contain the single selected item
    9. selectionModel()->clear();
    10. }
    11. }
    12. QTreeView::rowsAboutToBeRemoved (parent, start, end);
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2007
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView: selection behavior upon selected item removal

    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...
    Last edited by roro; 11th July 2007 at 17:05.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.