以递归方式扩展QTreeView中项目的所有子项目

问题描述 投票:3回答:2

我有一个QTreeView,我想扩展最近扩展项目的所有子项。

我尝试使用.expandAll(),但它也会扩展所有其他项。

我很难获取最后扩展的项目的ModelIndex,如果我有它,我可以递归扩展它的子级。

我该怎么做?

qt pyqt pyside qtreeview
2个回答
9
投票

要扩展给定节点之下的所有节点,我将以以下方式(C ++)递归地进行操作:

void expandChildren(const QModelIndex &index, QTreeView *view)
{
    if (!index.isValid()) {
        return;
    }

    int childCount = index.model()->rowCount(index);
    for (int i = 0; i < childCount; i++) {
        const QModelIndex &child = index.child(i, 0);
        // Recursively call the function for each child node.
        expandChildren(child, view);
    }

    if (!view->expanded(index)) {
        view->expand(index);
    }
}

0
投票

从Qt 5.13开始QTreeView :: expand递归https://doc.qt.io/qt-5/qtreeview.html#expandRecursively可用

© www.soinside.com 2019 - 2024. All rights reserved.