summaryrefslogtreecommitdiffstats
path: root/examples/pdf
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-08 09:06:36 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-08 14:48:17 +0200
commitc11b0bab0172c166b66dc4ee961b1236e1ebd4ab (patch)
treed46299b0be270925db18eb242d691ffb9d2eb5f7 /examples/pdf
parent98bae072c090ee19e2a5a1e85eed712a060d12fc (diff)
Polish the QML/PdfViewer example
- Use QCommandLineParser - Use QmlEngine::setInitialProperties() instead of context properties and make "source" a required property of type url - Use startup documents from resource Pick-to: 6.4 Change-Id: Idaad82be4d7cf862de96240af031ed317a401266 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'examples/pdf')
-rw-r--r--examples/pdf/pdfviewer/main.cpp27
-rw-r--r--examples/pdf/pdfviewer/viewer.qml2
2 files changed, 21 insertions, 8 deletions
diff --git a/examples/pdf/pdfviewer/main.cpp b/examples/pdf/pdfviewer/main.cpp
index da940dd3f..bed0e5860 100644
--- a/examples/pdf/pdfviewer/main.cpp
+++ b/examples/pdf/pdfviewer/main.cpp
@@ -1,9 +1,13 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QGuiApplication>
#include <QQmlApplicationEngine>
+#include <QGuiApplication>
+
+#include <QCommandLineParser>
+#include <QCommandLineOption>
+
int main(int argc, char* argv[])
{
QCoreApplication::setApplicationName("Qt Quick PDF Viewer Example");
@@ -11,14 +15,23 @@ int main(int argc, char* argv[])
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QGuiApplication app(argc, argv);
+ QCommandLineParser parser;
+ parser.addHelpOption();
+ parser.addVersionOption();
+ parser.addPositionalArgument("file", "The file to open.");
+ parser.process(app);
+
QQmlApplicationEngine engine;
+
+ QUrl toLoad = QUrl("qrc:/pdfviewer/resources/test.pdf");
+ if (!parser.positionalArguments().isEmpty())
+ toLoad = QUrl::fromLocalFile(parser.positionalArguments().constFirst());
+
+ engine.setInitialProperties({{"source", toLoad}});
+
engine.load(QUrl(QStringLiteral("qrc:///pdfviewer/viewer.qml")));
- if (app.arguments().count() > 1) {
- QUrl toLoad = QUrl::fromUserInput(app.arguments().at(1));
- engine.rootObjects().first()->setProperty("source", toLoad);
- } else {
- engine.rootObjects().first()->setProperty("source", QStringLiteral("resources/test.pdf"));
- }
+ if (engine.rootObjects().isEmpty())
+ return -1;
return app.exec();
}
diff --git a/examples/pdf/pdfviewer/viewer.qml b/examples/pdf/pdfviewer/viewer.qml
index 9f78c1404..a62035166 100644
--- a/examples/pdf/pdfviewer/viewer.qml
+++ b/examples/pdf/pdfviewer/viewer.qml
@@ -13,7 +13,7 @@ ApplicationWindow {
color: "lightgrey"
title: document.title
visible: true
- property string source // for main.cpp
+ required property url source // for main.cpp
property real scaleStep: Math.sqrt(2)
header: ToolBar {