Results 1 to 2 of 2

Thread: How can I display only the children of a QTreeView?

  1. #1
    Join Date
    Jun 2021
    Qt products
    MacOS X Windows

    Question How can I display only the children of a QTreeView?

    I want to add a checkbox toggle to my application where a tree view gets flattened to show only the leafs nodes (child items with no children). When the toggle is unselected it should go back to the original treeview.

    Something like this:


    Should become


    The desired outcome should be similar to doing setIndentation(0) when the check box is toggled and setIndentation(20) when unchecked but only the children should be visible when checked and back to the original treeview when unchecked.

    Other similar questions use KDE's KDescendantsProxyModel but for that you provide the depth level to show but in my case children can be at any depth level. And I am ideally looking for a solution involving only Qt APIs.

    Is there a way I can use QSortProxyFilters to accomplish this?

    Any thoughts/ suggestions?
    Last edited by donnaroma; 16th June 2021 at 21:26.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 864 Times in 851 Posts
    Qt products

    Default Re: How can I display only the children of a QTreeView?

    You can't do this with a QSortFilterProxyModel because as soon as you return false from the filterAcceptsRow() method (which you would for the row containing the parent "A", since you don't want to display it), the proxy model will ignore all children of that row. So there is no way to -not- display a parent row while displaying a subset of the children of that row.

    I think you will have to implement a new tree model derived from QAbstractProxyModel as a base. In non-checked mode, this is simply an identity model which calls the tree model. In checked mode, you will have to do your filtering.

    If you can find it, there used to be a FlatProxyModel in the KDE sources (files kptflatproxymodel.[h, cpp]) that flattened a tree model into a flat table model. This basically added extra columns for the parents to every leaf node, duplicating the parent information for each child row. Every level of the tree must have the same number of columns otherwise the behavior is undefined. You could probably adapt this to do what you want.
    <=== 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. How to set widgets as children items on QTreeView
    By q130s in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2013, 00:40
  2. QTreeView children with multiple coulms
    By maxpower in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2012, 15:10
  3. Hide children of QStandardItem in QTreeView
    By Phlucious in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2012, 21:48
  4. QTreeView adding children to the view
    By Tux-Slack in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2007, 10:28
  5. QTreeView and QStandardModel : add children
    By Valheru in forum Newbie
    Replies: 7
    Last Post: 19th September 2006, 18:24

Tags for this Thread


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.