Fix flags in QFileSystemModel

In 63eea5e5c69434871eaef9d9dc7184d7e54d7276, the
Qt::ItemNeverHasChildren flag was introduced. QFileSystemModel
was modified to use this flag for items which are not directories,
but only if the QFileSystemModel is not read-only and
the directory is writable.
This patch modifies QFileSystemModel to use the ItemNeverHasChildren
flags also if the model is read-only and if the item
is read-only.

Amends 63eea5e5c69434871eaef9d9dc7184d7e54d7276

Change-Id: Ie7f7d58ecf7baade93f9f03d120da84d3c005d42
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
This commit is contained in:
Andreas Buhr 2022-01-31 15:52:30 +01:00
parent e2b98627a8
commit 63937ffe6e
2 changed files with 7 additions and 2 deletions

View File

@ -987,14 +987,15 @@ Qt::ItemFlags QFileSystemModel::flags(const QModelIndex &index) const
} }
flags |= Qt::ItemIsDragEnabled; flags |= Qt::ItemIsDragEnabled;
if (!indexNode->isDir())
flags |= Qt::ItemNeverHasChildren;
if (d->readOnly) if (d->readOnly)
return flags; return flags;
if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) { if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) {
flags |= Qt::ItemIsEditable; flags |= Qt::ItemIsEditable;
if (indexNode->isDir()) if (indexNode->isDir())
flags |= Qt::ItemIsDropEnabled; flags |= Qt::ItemIsDropEnabled;
else
flags |= Qt::ItemNeverHasChildren;
} }
return flags; return flags;
} }

View File

@ -261,10 +261,14 @@ void tst_QFileSystemModel::readOnly()
QModelIndex root = model->setRootPath(flatDirTestPath); QModelIndex root = model->setRootPath(flatDirTestPath);
QTRY_VERIFY(model->rowCount(root) > 0); QTRY_VERIFY(model->rowCount(root) > 0);
// ItemIsEditable should change, ItemNeverHasChildren should not change
QVERIFY(!(model->flags(model->index(fileName)) & Qt::ItemIsEditable)); QVERIFY(!(model->flags(model->index(fileName)) & Qt::ItemIsEditable));
QVERIFY(model->flags(model->index(fileName)) & Qt::ItemNeverHasChildren);
model->setReadOnly(false); model->setReadOnly(false);
QCOMPARE(model->isReadOnly(), false); QCOMPARE(model->isReadOnly(), false);
QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable); QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable);
QVERIFY(model->flags(model->index(fileName)) & Qt::ItemNeverHasChildren);
} }
class CustomFileIconProvider : public QFileIconProvider class CustomFileIconProvider : public QFileIconProvider