简体   繁体   English

如何通过路径在QTreeView中为QFileSystemModel选择文件?

[英]How to select a file in QTreeView for a QFileSystemModel by path?

I have a tree view displaying a file system model. 我有一个显示文件系统模型的树形视图。 I want, upon it starting, make a certain file (specifically, current directory) already selected, so that user will not be forced to go all the way down the file system tree to find it. 我希望在启动时创建一个已选定的文件(特别是当前目录),这样就不会迫使用户一直沿文件系统树查找它。 However, neither QTreeView, nor QFileSystemModel don't have any functions to find the item by role and retrieve its QModelIndex. 但是,QTreeView和QFileSystemModel都没有任何功能来按角色查找项目并检索其QModelIndex。

tree->setCurrentIndex(fsModel->index(QDir::currentPath())); // or any path

Depending on the selection mode the item may or may not be selected. 根据选择模式,可能会选择或可能不会选择该项目。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM