Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to flatten a TreeView to show only the leaf node items?

How to flatten a TreeView to show only the leaf node items?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qtreeviewqt5
4 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ralphtink
    wrote on 15 Jun 2021, 15:10 last edited by
    #1

    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:

    A
       -B
            -C
    -D
       -E
    -F
    -G
        -H
            -I
                -K
    

    Should become

    -C
    -E
    -F
    -K
    

    I tried setting the treeView indentation to 0 when the QCheckbox is toggled and the setting indentation back to 20 when unchecked.

    This sort of gives the desired results but still shows the parent nodes.

    I have a class overriding QSortFilterProxyModel that is used to filter the elements in the QTreeview based on a search text. I tried to play around with it to hide the parents and cannot seem to figure it how to do it.

    SearchFilter.h: QSortFilterProxyModel

    bool FileFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
    {
        QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
        QModelIndex index1 = sourceModel()->index(source_row, 1, source_parent);
    
        if (!index0.isValid())
            return false;
    
        if (sourceModel()->data(index0).toString().contains(this->filterRegularExpression())
                || sourceModel()->data(index1).toString().contains(this->filterRegularExpression()))
            return true;
    
        int rows = sourceModel()->rowCount(index0);
        for (int row = 0; row < rows; row++) {
            if (filterAcceptsRow(row, index0))
                return true;
        }
    
        return false;
    }
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 15 Jun 2021, 15:21 last edited by VRonin
      #2

      I had a similar question, see https://forum.qt.io/topic/71553/smarter-way-to-get-a-list-of-children-in-tree-model
      The 3-proxy-models solution works (you'll just need to change LevelDataProxy::data to match your use case and I'm still using it in my project without performance taking a big hit (even if the model gets quite big)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      R 1 Reply Last reply 15 Jun 2021, 19:18
      2
      • V VRonin
        15 Jun 2021, 15:21

        I had a similar question, see https://forum.qt.io/topic/71553/smarter-way-to-get-a-list-of-children-in-tree-model
        The 3-proxy-models solution works (you'll just need to change LevelDataProxy::data to match your use case and I'm still using it in my project without performance taking a big hit (even if the model gets quite big)

        R Offline
        R Offline
        ralphtink
        wrote on 15 Jun 2021, 19:18 last edited by
        #3

        @VRonin thanks, I saw your post earlier but KDescendantsProxyModel is apart of the KDE framework and I don't want to import a whole library just for this. Did you manage to acomplish this using only Qt5 APIs?

        V 1 Reply Last reply 15 Jun 2021, 19:40
        0
        • R ralphtink
          15 Jun 2021, 19:18

          @VRonin thanks, I saw your post earlier but KDescendantsProxyModel is apart of the KDE framework and I don't want to import a whole library just for this. Did you manage to acomplish this using only Qt5 APIs?

          V Offline
          V Offline
          VRonin
          wrote on 15 Jun 2021, 19:40 last edited by
          #4

          @ralphtink said in How to flatten a TreeView to show only the leaf node items?:

          I don't want to import a whole library just for this

          I would strongly suggest you do but even if you want to go against my advice you can just:

          • take kbihash_p.h, kdescendantsproxymodel.h and kdescendantsproxymodel.cpp
          • add the files to your project
          • replace #include "kitemmodels_export.h" with #define KITEMMODELS_EXPORT
          • satisfy LGPL 2 requirement on the imported code

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0

          4/4

          15 Jun 2021, 19:40

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved