QFileSystemModel: Add role for fileinfo

Instead of having users traverse a ProxyModel hierarchy to
get to a QFileSystemModel to use the fileInfo(QModelIndex)
function, just let it be available with the roles.

Change-Id: I285347d1d85b4c6253fcb893737aa629a56e27fd
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Sune Vuorela 2024-03-21 12:05:27 +01:00
parent c6a2f7a70e
commit 19258608e9
3 changed files with 8 additions and 1 deletions

View File

@ -31,6 +31,7 @@ using namespace Qt::StringLiterals;
\value FilePathRole
\value FileNameRole
\value FilePermissions
\value FileInfoRole The QFileInfo object for the index
*/
/*!
@ -730,6 +731,8 @@ QVariant QFileSystemModel::data(const QModelIndex &index, int role) const
return filePath(index);
case FileNameRole:
return d->name(index);
case FileInfoRole:
return QVariant::fromValue(fileInfo(index));
case Qt::DecorationRole:
if (index.column() == QFileSystemModelPrivate::NameColumn) {
QIcon icon = d->icon(index);
@ -1255,6 +1258,7 @@ QHash<int, QByteArray> QFileSystemModel::roleNames() const
ret.insert(QFileSystemModel::FilePathRole, QByteArrayLiteral("filePath"));
ret.insert(QFileSystemModel::FileNameRole, QByteArrayLiteral("fileName"));
ret.insert(QFileSystemModel::FilePermissions, QByteArrayLiteral("filePermissions"));
ret.insert(QFileSystemModel::FileInfoRole, QByteArrayLiteral("fileInfo"));
return ret;
}

View File

@ -32,11 +32,13 @@ Q_SIGNALS:
void directoryLoaded(const QString &path);
public:
// ### Qt 7: renumber these values to be before Qt::UserRole comment.
enum Roles {
FileIconRole = Qt::DecorationRole,
FileInfoRole = Qt::UserRole - 1,
FilePathRole = Qt::UserRole + 1,
FileNameRole = Qt::UserRole + 2,
FilePermissions = Qt::UserRole + 3
FilePermissions = Qt::UserRole + 3,
};
enum Option

View File

@ -811,6 +811,7 @@ void tst_QFileSystemModel::setData()
QCOMPARE(spy.size(), 1);
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName);
QCOMPARE(model->data(idx, QFileSystemModel::FileInfoRole).value<QFileInfo>().fileName(), newFileName);
QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName);
QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp));
QCOMPARE(arguments.at(1).toString(), oldFileName);