From 054fb061d770205706e80e80b6937edf7c866da1 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 8 Mar 2023 16:38:58 +0100 Subject: [PATCH] Fix warnings from deprecating QFileOpenEvent::openFile, update snippet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Amends 76c63936d3d3c937960108da88a56394a0ac70b5 by adjusting the test case. We still just test that we can open a file based on a filename that we came up with ourselves. Also, update usage documentation and make the snippet a bit more relevant. Change-Id: I5bf00210d74e2a73d5a71a09a5beb1b3f6f8e225 Reviewed-by: Axel Spoerl Reviewed-by: Tor Arne Vestbø --- src/gui/doc/snippets/qfileopenevent/main.cpp | 12 ++++++++++-- src/gui/kernel/qevent.cpp | 9 +++++++-- .../qfileopeneventexternal.cpp | 4 ++-- .../qfileopenevent/test/tst_qfileopenevent.cpp | 15 ++++++++------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/gui/doc/snippets/qfileopenevent/main.cpp b/src/gui/doc/snippets/qfileopenevent/main.cpp index b733bfc320..a94ff58137 100644 --- a/src/gui/doc/snippets/qfileopenevent/main.cpp +++ b/src/gui/doc/snippets/qfileopenevent/main.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Samuel Gaist +// Copyright (C) 2023 Samuel Gaist // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [QApplication subclass] @@ -19,7 +19,15 @@ public: { if (event->type() == QEvent::FileOpen) { QFileOpenEvent *openEvent = static_cast(event); - qDebug() << "Open file" << openEvent->file(); + const QUrl url = openEvent->url(); + if (url.isLocalFile()) { + QFile localFile(url.toLocalFile()); + // read from local file + } else if (url.isValid()) { + // process according to the URL's schema + } else { + // parse openEvent->file() + } } return QApplication::event(event); diff --git a/src/gui/kernel/qevent.cpp b/src/gui/kernel/qevent.cpp index 8e871d7694..8040a3647b 100644 --- a/src/gui/kernel/qevent.cpp +++ b/src/gui/kernel/qevent.cpp @@ -3594,10 +3594,15 @@ Q_IMPL_EVENT_COMMON(QShowEvent) \snippet qfileopenevent/Info.plist Custom Info.plist - The following implementation of a QApplication subclass prints the path to - the file that was, for example, dropped on the Dock icon of the application. + The following implementation of a QApplication subclass shows how to handle + QFileOpenEvent to open the file that was, for example, dropped on the Dock + icon of the application. \snippet qfileopenevent/main.cpp QApplication subclass + + Note how \c{QFileOpenEvent::file()} is not guaranteed to be the name of a + local file that can be opened using QFile. The contents of the string depend + on the source application. */ /*! diff --git a/tests/auto/gui/kernel/qfileopenevent/qfileopeneventexternal/qfileopeneventexternal.cpp b/tests/auto/gui/kernel/qfileopenevent/qfileopeneventexternal/qfileopeneventexternal.cpp index 6e50a96258..0af605ccb6 100644 --- a/tests/auto/gui/kernel/qfileopenevent/qfileopeneventexternal/qfileopeneventexternal.cpp +++ b/tests/auto/gui/kernel/qfileopenevent/qfileopeneventexternal/qfileopeneventexternal.cpp @@ -14,8 +14,8 @@ struct MyApplication : public QGuiApplication { if (event->type() == QEvent::FileOpen) { QFileOpenEvent* ev = static_cast(event); - QFile file; - bool ok = ev->openFile(file, QFile::Append | QFile::Unbuffered); + QFile file(ev->file()); + bool ok = file.open(QFile::Append | QFile::Unbuffered); if (ok) file.write(QByteArray("+external")); return true; diff --git a/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp b/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp index b526240d88..50671e17f2 100644 --- a/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp +++ b/tests/auto/gui/kernel/qfileopenevent/test/tst_qfileopenevent.cpp @@ -78,8 +78,8 @@ void tst_qfileopenevent::constructor() QByteArray tst_qfileopenevent::readFileContent(QFileOpenEvent& event) { - QFile file; - event.openFile(file, QFile::ReadOnly); + QFile file(event.file()); + file.open(QFile::ReadOnly); file.seek(0); QByteArray data = file.readAll(); return data; @@ -87,8 +87,8 @@ QByteArray tst_qfileopenevent::readFileContent(QFileOpenEvent& event) bool tst_qfileopenevent::appendFileContent(QFileOpenEvent& event, const QByteArray& writeContent) { - QFile file; - bool ok = event.openFile(file, QFile::Append | QFile::Unbuffered); + QFile file(event.file()); + bool ok = file.open(QFile::Append | QFile::Unbuffered); if (ok) ok = file.write(writeContent) == writeContent.size(); return ok; @@ -127,8 +127,8 @@ void tst_qfileopenevent::handleLifetime() QScopedPointer event(createFileAndEvent(QLatin1String("testHandleLifetime"), QByteArray("test content"))); // open a QFile after the original RFile is closed - QFile qFile; - QCOMPARE(event->openFile(qFile, QFile::Append | QFile::Unbuffered), true); + QFile qFile(event->file()); + QVERIFY(qFile.open(QFile::Append | QFile::Unbuffered)); event.reset(0); // write to the QFile after the event is closed @@ -152,7 +152,8 @@ void tst_qfileopenevent::multiOpen() QFile files[5]; for (int i=0; i<5; i++) { - QCOMPARE(event->openFile(files[i], QFile::ReadOnly), true); + files[i].setFileName(event->file()); + QVERIFY(files[i].open(QFile::ReadOnly)); } for (int i=0; i<5; i++) files[i].seek(i);