从 QFileSystemModel 中的文件路径和文件名获取 QModelIndex

Get QModelIndex from filepath and filename in QFileSystemModel

本文关键字:文件名 获取 QModelIndex 路径 文件 QFileSystemModel      更新时间:2023-10-16

我需要从QFileSystemModel中的文件路径和文件名中获取QModelIndex。我看到有一个index函数需要filepath但我不知道列参数应该做什么。

你必须覆盖 QFileSystemModel 的 index(( 方法,以便可以从 QML 访问它:

class DisplayFileSystemModel : public QFileSystemModel {
Q_OBJECT
public:
...
Q_INVOKABLE QModelIndex index(const QString &path, int column = 0) const
{
return QFileSystemModel::index(path, column);    
}
...
};

然后在QML中,您可以按照以下形式使用它:

your_model.index(your_fullpath)