Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to count number of visible children in a QTreeWidget



  • Hello,

    I'm trying to determine the number of visible child items in a QTreeWidget (e.g. if the bottom-level children are visible, not collapsed and hidden). So far, I've not had any luck. All elements of the tree are simple top level nodes with children.

    // count a the number of visible items
    QTreeWidget* tree = current_tree();
    int num_visible = 0;
    for (int ii = 0; ii < tree->topLevelItemCount(); ii++)
    {
        QTreeWidgetItem* top_item = tree->topLevelItem(ii);
    
        if (top_item->isExpanded())
        {
            num_visible += top_item->childCount();
        }
    }
    

    Anyone have a simple solution?

    Many thanks,
    BD


  • Lifetime Qt Champion

    Hi,

    The visualRect method comes to mind for that.


Log in to reply