QTreeView 按照指定深度展开
代码
void expandToDepth(QTreeView *treeView, const QModelIndex &index, int depth) {
if (depth <= 0) {
return;
}
// 展开当前节点
treeView->expand(index);
// 获取当前节点的子节点数量
QAbstractItemModel *model = treeView->model();
int rowCount = model->rowCount(index);
// 递归展开子节点
for (int row = 0; row < rowCount; ++row) {
QModelIndex childIndex = model->index(row, 0, index);
expandToDepth(treeView, childIndex, depth - 1);
}
}