我想使用 QModelIndex 从 QStandardItemModel 获取目录的路径。是否可以?我看到我可以使用 QString QFileSystemModel::filePath (const QModelIndex & index) const
。但是当我运行这段代码时它崩溃了:
QString _path = qobject_cast<QFileSystemModel*>(model)->filePath(modelIndex);
如果我创建一个 QFileSystemModel
的实例并传递一个 QModelIndex
,它也会崩溃:
QFileSystemModel* fileSystem = new QFileSystemModel();
QString myPath = fileSystem->filePath(modelIndex);
最佳答案
如文档中所述
QFileSystemModel
will not fetch any files or directories untilsetRootPath()
is called.
我强烈建议这样做。
如果出现更多问题,请务必查看 the documentation .
关于c++ - 如何使用 QModelIndex 从 QStandardItemModel 获取路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162390/