Results 1 to 4 of 4

Thread: How to select a row in QTreeView

  1. #1
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question How to select a row in QTreeView

    Hi,

    I have a QTreeView derived from QAbstractItemModel.
    I want to make the selection to the topmost node(root node) of the tree when the UI launches.

    Below is my code, but no selection happening.

    QModelIndex rootIndex = TreeView->rootIndex();
    QModelIndex rootChildIndex = TreeModel->index(0, 0, rootIndex);

    if (rootChildIndex.isValid())
    {
    TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Select);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to select a row in QTreeView

    Add the QItemSelectionModel::Rows flag to the second argument if you want the whole row (all columns) selected.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to select a row in QTreeView

    Not working!

    TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Rows);

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to select a row in QTreeView

    Hi, you need to add QItemSelectionModel::Select, otherwise nothing will be changed.
    Qt Code:
    1. TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Rows | QItemSelectionModel::Select);
    To copy to clipboard, switch view to plain text mode 

    Ginsengelf

  5. The following user says thank you to Ginsengelf for this useful post:

    chithara (4th June 2019)

Similar Threads

  1. Select a row from QTreeView
    By vieraci in forum Qt Programming
    Replies: 5
    Last Post: 7th December 2015, 20:44
  2. Replies: 2
    Last Post: 30th July 2013, 01:48
  3. Replies: 1
    Last Post: 6th July 2011, 07:03
  4. Select all items in QTreeView. Why very slow ?
    By MaxBooster in forum Qt Programming
    Replies: 0
    Last Post: 25th August 2010, 07:12
  5. QTreeView and multi-select of rows
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2008, 03:32

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.