Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED] Hide leafs in QTreeView

    General and Desktop
    3
    5
    3772
    Loading More Posts
    • 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.
    • francescmm
      francescmm last edited by

      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 Reply Quote 0
      • Jeroentjehome
        Jeroentjehome last edited by

        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 Reply Quote 0
        • francescmm
          francescmm last edited by

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

          1 Reply Last reply Reply Quote 0
          • francescmm
            francescmm last edited by

            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 Reply Quote 0
            • raven-worx
              raven-worx Moderators last edited by

              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 Reply Quote 0
              • First post
                Last post