简体   繁体   中英

How to get path from a QStandardItemModel using a QModelIndex?

I want to get the path of a directory from QStandardItemModel using a QModelIndex. Is it possible? I saw that I can use 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* 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.

I strongly recommend doing so.

If more questions arise make sure to check out the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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