Results 1 to 4 of 4

Thread: QTreeView how to abstract only second level hierarchy elements to populate the view

  1. #1
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QTreeView how to abstract only second level hierarchy elements to populate the view

    Hello,
    I have been working on a task where I should make a drop-down menu like a combo box. I want to set a QMenu to QPushbutton, which should contain a qtreeview. Let's assume we have the following QAbstractItemModel:
    X
    -->Leaf_B1
    .....-->Leaf_B1.1
    .....-->Leaf_B1.2
    ......... ------>Leaf_A1.1.1
    -->Leaf_B2
    .....-->Leaf_B2.1
    ......... ------->Leaf_B2.1.1
    -->Leaf_B3
    .....-->Leaf_B3.1
    .....-->Leaf_B3.2
    ......... ------->Leaf_B2.1.1

    I want to customize a QTreeView to look like QListView and to populate only second hierarchy elements in the view (Leaf_B1, Leaf_B2, Leaf_B3).

    I have made a search about it on the web and I have stumble across the following article:
    https://stackoverflow.com/questions/...display-childr
    http://lynxline.com/jongling-qt-models/

    I have tried to use QTreeView::setRootIndex() but without success.

    I cannot understand how exactly i can abstract only the second level hierarchy elements.

    Does anyone have an idea how can I abstract only these elements and populate them in the view?

    Any suggestions are highly appreciated!

    Thanks in advance

  2. #2
    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: QTreeView how to abstract only second level hierarchy elements to populate the vi

    Hi, why don't you simply use a QListView if it does what you want?

    Ginsengelf

  3. #3
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView how to abstract only second level hierarchy elements to populate the vi

    Because I want to filter out only the second hierarchy elements and this is not possible with QListView as far as I know.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTreeView how to abstract only second level hierarchy elements to populate the vi

    Because I want to filter out only the second hierarchy elements
    You basically want a QSortFilterProxyModel that implements the QSortFilterProxyModel::filterAcceptsRow() method. In this method, you will examine the QModelIndex that is passed in, determine whether it is a second-level item or not, and return the proper true or false value. You set the proxy model as the model to be used in the QListView, and set your tree model as source for the proxy model.

    You must be careful though, because for tree-structured models traversal down into the tree stops with the first index where the filterAcceptsRow() method returns false. So if you answer "false" for your "X" node, that ends the traversal of the tree. See "Filtering" in the QSortFilterProxyModel documentation.

    If this doesn't work for you, then you might be forced to implement a separate model based on your underlying data that contains only the second-level items and use that instead of the tree model as the source model for your list view.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Custom Delegate in multi hierarchy QTreeView
    By btbt9527 in forum Qt Programming
    Replies: 0
    Last Post: 5th January 2016, 19:28
  2. Populate QTreeView from database
    By yagabey in forum Qt Programming
    Replies: 15
    Last Post: 26th March 2015, 03:10
  3. Replies: 2
    Last Post: 26th March 2015, 02:56
  4. Need advice regarding hierarchy view
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 9th July 2008, 10:42
  5. Graphics View - Scene & Item hierarchy
    By mooreaa in forum Qt Programming
    Replies: 0
    Last Post: 30th June 2008, 00:49

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.