Fix warnings from deprecating QFileOpenEvent::openFile, update snippet

Amends 76c63936d3 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 <axel.spoerl@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-03-08 16:38:58 +01:00
parent a6776de0c7
commit 054fb061d7
4 changed files with 27 additions and 13 deletions

View File

@ -1,4 +1,4 @@
// Copyright (C) 2016 Samuel Gaist <samuel.gaist@edeltech.ch>
// Copyright (C) 2023 Samuel Gaist <samuel.gaist@edeltech.ch>
// 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<QFileOpenEvent *>(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);

View File

@ -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.
*/
/*!

View File

@ -14,8 +14,8 @@ struct MyApplication : public QGuiApplication
{
if (event->type() == QEvent::FileOpen) {
QFileOpenEvent* ev = static_cast<QFileOpenEvent *>(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;

View File

@ -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<QFileOpenEvent> 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);