From 63937ffe6e2c0d62c419c981b210e38892bb566d Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Mon, 31 Jan 2022 15:52:30 +0100 Subject: [PATCH] Fix flags in QFileSystemModel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/gui/itemmodels/qfilesystemmodel.cpp | 5 +++-- .../gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/gui/itemmodels/qfilesystemmodel.cpp b/src/gui/itemmodels/qfilesystemmodel.cpp index 0ee97d06be..d29a1c0114 100644 --- a/src/gui/itemmodels/qfilesystemmodel.cpp +++ b/src/gui/itemmodels/qfilesystemmodel.cpp @@ -987,14 +987,15 @@ Qt::ItemFlags QFileSystemModel::flags(const QModelIndex &index) const } flags |= Qt::ItemIsDragEnabled; + + if (!indexNode->isDir()) + flags |= Qt::ItemNeverHasChildren; if (d->readOnly) return flags; if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) { flags |= Qt::ItemIsEditable; if (indexNode->isDir()) flags |= Qt::ItemIsDropEnabled; - else - flags |= Qt::ItemNeverHasChildren; } return flags; } diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp index b6f6328acd..edd7316d89 100644 --- a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -261,10 +261,14 @@ void tst_QFileSystemModel::readOnly() QModelIndex root = model->setRootPath(flatDirTestPath); 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::ItemNeverHasChildren); model->setReadOnly(false); QCOMPARE(model->isReadOnly(), false); QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable); + QVERIFY(model->flags(model->index(fileName)) & Qt::ItemNeverHasChildren); } class CustomFileIconProvider : public QFileIconProvider