簡體   English   中英

如何使用QModelIndex從QStandardItemModel獲取路徑?

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

我想使用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);

如文檔中所述

在調用setRootPath()之前, QFileSystemModel將不會獲取任何文件或目錄。

我強烈建議您這樣做。

如果還有其他問題,請確保查看文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM