From 7e30d3afd35cd24b8888d6f392330c3afa69cb81 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Wed, 23 Oct 2013 12:22:58 +0200 Subject: [PATCH] QFileDialog can delete files too, not just directories After Ic12de12ec51c20de52d040514e90be5e783add43 this functionality was broken. Added an autotest. Task-number: QTBUG-34159 Change-Id: I8f41b7073dc57fea855ab87796f09e8a91520d13 Reviewed-by: David Faure --- src/widgets/dialogs/qfilesystemmodel.cpp | 2 ++ .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 6330d529fb..fa6306005b 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -204,6 +204,8 @@ bool QFileSystemModel::remove(const QModelIndex &aindex) #ifndef QT_NO_FILESYSTEMWATCHER d->fileInfoGatherer.removePath(path); #endif + if (QFileInfo(path).isFile()) + return QFile::remove(path); return QDir(path).removeRecursively(); } diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp index 0b9fb5c168..61a2abc084 100644 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -115,6 +115,7 @@ private slots: void sort(); void mkdir(); + void deleteFile(); void caseSensitivity(); @@ -927,6 +928,25 @@ void tst_QFileSystemModel::mkdir() QCOMPARE(oldRow, idx.row()); } +void tst_QFileSystemModel::deleteFile() +{ + QString newFilePath = QDir::temp().filePath("NewFileDeleteTest"); + QFile newFile(newFilePath); + if (newFile.exists()) { + if (!newFile.remove()) + qWarning() << "unable to remove" << newFilePath; + QTest::qWait(WAITTIME); + } + if (!newFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qWarning() << "unable to create" << newFilePath; + } + newFile.close(); + QModelIndex idx = model->index(newFilePath); + QVERIFY(idx.isValid()); + QVERIFY(model->remove(idx)); + QVERIFY(!newFile.exists()); +} + void tst_QFileSystemModel::caseSensitivity() { QString tmp = flatDirTestPath;