QTreeView 仅显示父目录,而不显示父目录及其所有兄弟目录
我试图让 QTreeView (使用底层 QFileSystemModel)显示目录树。如果我将 RootPath 设置为父目录,那么我会看到所有子目录,但看不到父目录。如果我将 RootPath 设置为父目录的父目录,那么我会看到父目录及其所有同级目录。有没有办法让它显示没有兄弟姐妹的父母以及所有孩子?
谢谢
I'm trying to get QTreeView (using an underlying QFileSystemModel) to show a directory tree. If I set the RootPath to the parent directory, then I see all the children, but not the parent. If I set the RootPath to be the parent's parent, then I see the parent directory with all its siblings. Is there a way to get it to show the parent without its siblings, and all the children?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我在 Linux 上有效。我并不是说这是最好的实现,并且我不确定使用反斜杠分隔符是否适用于 Windows。我知道 Qt 将它们转换为本机分隔符,但我不知道它是否是来自模型的
data
方法的本机分隔符。This works for me on Linux. I'm not claiming it's the best implementation and I'm not sure if using backslash separators will work on Windows. I know Qt translates them to the native separator but I don't know if it's native separators that come out of the model's
data
method.QTreeView
描述中的 示例 讨论了调用QFileSystemModel::setRootPath
但描述QFileSystemModel
讨论使用QTreeView::setRootIndex
。该文件说:这是一个对我有用的示例。在此示例中,当我没有调用
tree->setRootIndex(model->index((dir)))
时,它用于向我显示所有目录的列表,而不是c :/样本
。希望这有帮助。The example in description of
QTreeView
talks about callingQFileSystemModel::setRootPath
but description ofQFileSystemModel
talks of usingQTreeView::setRootIndex
. The document says:Here is an example which worked for me. In this example when I wasn't calling
tree->setRootIndex(model->index((dir)))
, it used to show me list of all the directories and notc:/sample
. Hope this helps.