diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-09-08 09:06:36 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-09-08 23:55:50 +0000 |
commit | 7557a055ea383e1ef578b309cf915b5423f45233 (patch) | |
tree | 0b4a63d719bbfbbc441491be54188995f7808b75 | |
parent | 72a0caa3fe9dfe9bd377786fddaab0d44bd6b254 (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
Change-Id: Idaad82be4d7cf862de96240af031ed317a401266
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit c11b0bab0172c166b66dc4ee961b1236e1ebd4ab)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | examples/pdf/pdfviewer/main.cpp | 27 | ||||
-rw-r--r-- | examples/pdf/pdfviewer/viewer.qml | 2 |
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 { |