Results 1 to 11 of 11

Thread: QTreeView: hide parents but show children?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QTreeView: hide parents but show children?

    The project that I was needing this for ended up getting postponed indefinitely, so I stopped exploring the solution. It's been a long time since I was looking into this, but if the thread tells the tale correctly, then the concept of proxy models should help you: http://doc.qt.io/qt-5/qsortfilterproxymodel.html , http://doc.qt.io/qt-5/qabstractproxymodel.html . Wish I could be more helpful!

  2. #2
    Join Date
    Dec 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: hide parents but show children?

    Unfortunately so far it was of no particular use.
    I do have a proxyfiltermodel in there, to filter the view and having the parents still displayed when any children matches is working just fine.
    But I can't seem to get behind on how one could possibly make it ignore the parent items, but still display the children that are matching. If the parent is not accepted, then the children never get examined.

    Thought about overriding mapTo/FromSource and somehow keeping a mapping but for some reason, even if there are no filters set for the proxyfiltermodel, it still doesn't map 1:1 to the source model, at least row of the proxy / source indexes don't match and I cannot see a pattern either. This unexpected behavior is throwing me off.
    Plus would have to somehow delete that mapping when the source changes or generate it on the fly somewhere, which I don't know where yet.
    Last edited by WorstNoob; 6th December 2017 at 21:26.

  3. #3
    Join Date
    Apr 2020
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeView: hide parents but show children?

    I've been looking into how to do this and have found quite a nice way so long has your willing to use some private apis.

    The issue is as WorstNoob states that there is no way to show only the children for a tree model using a QSortFilterProxyModel (that I could find), however you can use an internal Qt model called QQmlTreeModelToTableModel (QtQmlModels/private/qqmltreemodeltotablemodel_p_p.h) which as the name suggests proxies a tree model and converts it into a table model. Now you can proxy this table model and filter out parent rows as they are technically no longer parents. This is the model that Qts QML TreeView uses internally for what its worth but it should work whatever your usecase. It also preserves child parent relationships albeit in a different way so you still have that info available should you need it. One gotcha to look out for is that you need to manually expand the rows, otherwise it will only show the first row in the model.

  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: hide parents but show children?

    Several years ago, the KDE project had a FlatProxyModel class. It doesn't seem to be a part of the current project, but you can still find it online as part of some other projects in the kptflatproxymodel.[h, cpp] files.

    There is now a KDescendantsProxyModel in the KDE Frameworks (KItemModels section) which looks like it might be the replacement for the FlatProxyModel class.

    These are both KDE "Tier 1" classes, meaning they have no dependence on other parts of KDE, only on Qt.

    These do essentially the same thing as your private model does - flatten a tree into a table by turning each leaf into a row. Parents are added as new columns at the start of the row. It has the advantage that it isn't going to break your code in the future when it is modified. Private classes are private because the authors of Qt want to be able to change them while keeping the external public API the same.
    <=== 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. Hide parents in QSortFilterProxyModel if they don't match
    By Annihilator in forum Qt Programming
    Replies: 1
    Last Post: 21st October 2014, 14:44
  2. QTreeView display of graph (multiple parents of item)
    By iraytrace in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2014, 19:34
  3. Hide children of QStandardItem in QTreeView
    By Phlucious in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2012, 20:48
  4. Hide Parent and show only children in QTreeView
    By y.s.bisht in forum Qt Programming
    Replies: 8
    Last Post: 19th January 2012, 09:51
  5. Replies: 0
    Last Post: 14th April 2010, 15:03

Tags for this Thread

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.