从 QFileSystemModel 中的文件路径和文件名获取 QModelIndex
Get QModelIndex from filepath and filename in QFileSystemModel
我需要从 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)
我需要从 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)