简体   繁体   English

如何使用QModelIndex从QStandardItemModel获取路径?

[英]How to get path from a QStandardItemModel using a QModelIndex?

I want to get the path of a directory from QStandardItemModel using a QModelIndex. 我想使用QModelIndex从QStandardItemModel获取目录的路径。 Is it possible? 可能吗? I saw that I can use QString QFileSystemModel::filePath ( const QModelIndex & index ) const . 我看到可以使用QString QFileSystemModel::filePath ( const QModelIndex & index ) const But it crashes when I run this code: 但是当我运行以下代码时,它崩溃了:

QString _path = qobject_cast<QFileSystemModel*>(model)->filePath(modelIndex);

Also it crashes if I create an instance of a QFileSystemModel and pass a QModelIndex : 如果我创建QFileSystemModel的实例并传递QModelIndex

QFileSystemModel* fileSystem = new QFileSystemModel();
QString myPath = fileSystem->filePath(modelIndex);

As stated in the documentation 如文档中所述

QFileSystemModel will not fetch any files or directories until setRootPath() is called. 在调用setRootPath()之前, QFileSystemModel将不会获取任何文件或目录。

I strongly recommend doing so. 我强烈建议您这样做。

If more questions arise make sure to check out the documentation . 如果还有其他问题,请确保查看文档

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

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