diff options
-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 { |