gpt4 book ai didi

c++ - 获取 QTreeView 的根索引并返回上一级

转载 作者:搜寻专家 更新时间:2023-10-31 01:38:37 25 4
gpt4 key购买 nike

我使用QTreeViewQFileSystemModel 制作了一个非常简单的文件浏览器。现在 QTreeView 可能会混淆它的级联结构:

image description

添加了一个选项,用于将所选目录设置为临时根位置:

image description

在代码中,这是通过给定路径将根节点设置为QTreeView 的方法。目前我不知道如何检查无效路径:

void FileView::setRootPath( const QString&str )
{
// This didn't work
//model->setRootPath(str);
//This works
ui.treeView->setRootIndex(model->index(str));
}

但我也希望能够恢复此操作并返回目录树。我认为我需要的是在以下注释行下获取一些代码:

void FileView::rootUpOneLevel() {
QString rootPath;
// Get the current root index path into the QString
...
// Set the path as new root index, provided it's not out of the original root
setRootPath(rootPath);
}

最佳答案

我想,如果你想重置 View 并显示整个目录树,你只需要做:

ui.treeView->setRootIndex(QModelIndex());

即通过提供无效的模型索引,即根模型索引。

更新

为了更上一层楼,您需要调用相同的 setRootIndex() 函数,但将父模型索引作为参数:

void FileView::up(const QString &str)
{
QModelIndex idx = model->index(str);
ui.treeView->setRootIndex(idx.parent());
}

// Goes one level up from the current
void FileView::up()
{
QModelIndex currentRoot = ui.treeView->rootIndex();
ui.treeView->setRootIndex(currentRoot.parent());
}

关于c++ - 获取 QTreeView 的根索引并返回上一级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32578884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com