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. [SOLVED] Hide leafs in QTreeView
QtWS25 Last Chance

[SOLVED] Hide leafs in QTreeView

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.1k 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.
  • francescmmF Offline
    francescmmF Offline
    francescmm
    wrote on last edited by
    #1

    Hi all,

    I'm working in QTreeView data representation and I would like to show a TreeModel (inherited from QStandardItemModel) that only show the nodes but not the leafs of them. An example could be:

    • ROOT
      ** NODE 1
      *** LEAF
      *** NODE 1.1
      ** NODE 2
      *** NODE 2.1
      *** NODE 2.2
      *** LEAF

    And represented with the custom TreeModel like:

    • ROOT
      ** NODE 1
      *** NODE 1.1
      ** NODE 2
      *** NODE 2.1
      *** NODE 2.2

    I have tried overloading rowCount() of the model, overloading the return of the childCount() or rowCount() of each item but I have not accomplished what I want. I know how many childs are leafs so I can return the total value decreasing the total of leafs.

    One problem is that in the TreeModel::data() I need some way to show the following node in case that the current index is a leaf (due to the childCount() reports only the total of nodes with child).

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Have you checked out the QSortFilterProxy class? It might suit your needs. Then the data function is still the same, but the sort filter proxy is placed between the view and the model to customize the way it is shown in every different view.
      Greetz

      Greetz, Jeroen

      1 Reply Last reply
      0
      • francescmmF Offline
        francescmmF Offline
        francescmm
        wrote on last edited by
        #3

        Thanks! I will take a look and I will post the results!

        1 Reply Last reply
        0
        • francescmmF Offline
          francescmmF Offline
          francescmm
          wrote on last edited by
          #4

          Hi again!

          Here I post how to hide the leafs of a TreeModel. My model is based on QStandardItemModel, so I need to cast from it to evaluate if the node is a leaf or not:

          @bool TreeFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &index) const
          {
          if (index.isValid())
          {
          TreeModel model = dynamic_cast<TreeModel>(sourceModel());

              if (model)
              {
                  TreeItem *item = model->itemFromIndex(index);
          
                  if (item and item->child(source_row)->hasChildren())
                      return true;
              }
          }
          else if (source_row == 0)
              return true;
          
          return false;
          

          }@

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            you could simply avoid the casting when you hold a pointer to your source model as member.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0

            • Login

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