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

标签 c++ qt

我想使用 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 until setRootPath() is called.

我强烈建议这样做。

如果出现更多问题,请务必查看 the documentation .

关于c++ - 如何使用 QModelIndex 从 QStandardItemModel 获取路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162390/

相关文章:

C++ (LINUX) 使用 DevIL 设置 X11 窗口背景

c++ - 关于C++函数对象的问题

multithreading - 将 QList<QString>* 传递给来自 QThread 的信号

qt - 如何在 Qt Designer 中安装 QChartView?

qt - 如何在Qt中部署openvino-opencv

c++ - Qt QGraphicsView 或 QLabel

c++ - 静态/全局对象的创建顺序

c++ - 如何使用 Cereal 序列化 boost::ptr_vector?

c++ - 在这种情况下我的压缩率可以低于 6.25% 吗?

c++ - Qt SLOT 宏用作函数参数