From 72a0caa3fe9dfe9bd377786fddaab0d44bd6b254 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 6 Sep 2022 08:46:50 +0200 Subject: Polish the nano browser example - Use QCommandLineParser - Fix the organization name - Use modern QML type registration (Utils as a singleton) instead of context properties Change-Id: Ibbe93dffd685ddd2e5f2d01852e006c4c47d9713 Reviewed-by: Allan Sandfeld Jensen (cherry picked from commit 98bae072c090ee19e2a5a1e85eed712a060d12fc) Reviewed-by: Qt Cherry-pick Bot --- .../quicknanobrowser/ApplicationRoot.qml | 2 +- .../quicknanobrowser/BrowserDialog.qml | 2 +- .../quicknanobrowser/BrowserWindow.qml | 5 ++- .../webenginequick/quicknanobrowser/CMakeLists.txt | 5 +++ .../quicknanobrowser/DownloadView.qml | 2 +- .../webenginequick/quicknanobrowser/FindBar.qml | 2 +- .../quicknanobrowser/FullScreenNotification.qml | 2 +- examples/webenginequick/quicknanobrowser/main.cpp | 46 +++++++++++++--------- .../quicknanobrowser/quicknanobrowser.pro | 4 ++ examples/webenginequick/quicknanobrowser/utils.h | 14 +++++-- 10 files changed, 54 insertions(+), 30 deletions(-) (limited to 'examples') diff --git a/examples/webenginequick/quicknanobrowser/ApplicationRoot.qml b/examples/webenginequick/quicknanobrowser/ApplicationRoot.qml index 9a40fab15..55c414409 100644 --- a/examples/webenginequick/quicknanobrowser/ApplicationRoot.qml +++ b/examples/webenginequick/quicknanobrowser/ApplicationRoot.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick diff --git a/examples/webenginequick/quicknanobrowser/BrowserDialog.qml b/examples/webenginequick/quicknanobrowser/BrowserDialog.qml index a19ab62bf..c155c3f35 100644 --- a/examples/webenginequick/quicknanobrowser/BrowserDialog.qml +++ b/examples/webenginequick/quicknanobrowser/BrowserDialog.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick diff --git a/examples/webenginequick/quicknanobrowser/BrowserWindow.qml b/examples/webenginequick/quicknanobrowser/BrowserWindow.qml index 3fa481787..7c2a21cd5 100644 --- a/examples/webenginequick/quicknanobrowser/BrowserWindow.qml +++ b/examples/webenginequick/quicknanobrowser/BrowserWindow.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import Qt.labs.settings @@ -8,6 +8,7 @@ import QtQuick.Controls import QtQuick.Layouts import QtQuick.Window import QtWebEngine +import BrowserUtils ApplicationWindow { id: browserWindow @@ -272,7 +273,7 @@ ApplicationWindow { when: currentWebView value: currentWebView.url } - onAccepted: currentWebView.url = utils.fromUserInput(text) + onAccepted: currentWebView.url = Utils.fromUserInput(text) selectByMouse: true } ToolButton { diff --git a/examples/webenginequick/quicknanobrowser/CMakeLists.txt b/examples/webenginequick/quicknanobrowser/CMakeLists.txt index cdccc26be..a12570225 100644 --- a/examples/webenginequick/quicknanobrowser/CMakeLists.txt +++ b/examples/webenginequick/quicknanobrowser/CMakeLists.txt @@ -29,6 +29,11 @@ target_link_libraries(quicknanobrowser PUBLIC Qt::WebEngineQuick ) +qt_add_qml_module(quicknanobrowser + URI BrowserUtils + VERSION 1.0 +) + # Resources: set(resources_resource_files "ApplicationRoot.qml" diff --git a/examples/webenginequick/quicknanobrowser/DownloadView.qml b/examples/webenginequick/quicknanobrowser/DownloadView.qml index 124ae4e9d..e16647cdb 100644 --- a/examples/webenginequick/quicknanobrowser/DownloadView.qml +++ b/examples/webenginequick/quicknanobrowser/DownloadView.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick diff --git a/examples/webenginequick/quicknanobrowser/FindBar.qml b/examples/webenginequick/quicknanobrowser/FindBar.qml index 34713479f..4d130a22b 100644 --- a/examples/webenginequick/quicknanobrowser/FindBar.qml +++ b/examples/webenginequick/quicknanobrowser/FindBar.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2019 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick diff --git a/examples/webenginequick/quicknanobrowser/FullScreenNotification.qml b/examples/webenginequick/quicknanobrowser/FullScreenNotification.qml index e0f3da411..779406432 100644 --- a/examples/webenginequick/quicknanobrowser/FullScreenNotification.qml +++ b/examples/webenginequick/quicknanobrowser/FullScreenNotification.qml @@ -1,4 +1,4 @@ -// Copyright (C) 2015 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import QtQuick diff --git a/examples/webenginequick/quicknanobrowser/main.cpp b/examples/webenginequick/quicknanobrowser/main.cpp index bdb31644f..519cfd089 100644 --- a/examples/webenginequick/quicknanobrowser/main.cpp +++ b/examples/webenginequick/quicknanobrowser/main.cpp @@ -1,43 +1,51 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "utils.h" -#include +#include + #include #include -#include -static QUrl startupUrl() +#include + +#include +#include + +static QUrl startupUrl(const QCommandLineParser &parser) { - QUrl ret; - QStringList args(qApp->arguments()); - args.takeFirst(); - for (const QString &arg : qAsConst(args)) { - if (arg.startsWith(QLatin1Char('-'))) - continue; - ret = Utils::fromUserInput(arg); - if (ret.isValid()) - return ret; + if (!parser.positionalArguments().isEmpty()) { + const QUrl url = Utils::fromUserInput(parser.positionalArguments().constFirst()); + if (url.isValid()) + return url; } return QUrl(QStringLiteral("https://www.qt.io")); } int main(int argc, char **argv) { - QCoreApplication::setOrganizationName("QtExamples"); + QCoreApplication::setApplicationName("Quick Nano Browser"); + QCoreApplication::setOrganizationName("QtProject"); + QtWebEngineQuick::initialize(); QGuiApplication app(argc, argv); + QCommandLineParser parser; + parser.addHelpOption(); + parser.addVersionOption(); + parser.addPositionalArgument("url", "The URL to open."); + parser.process(app); + QQmlApplicationEngine appEngine; - Utils utils; - appEngine.rootContext()->setContextProperty("utils", &utils); appEngine.load(QUrl("qrc:/ApplicationRoot.qml")); - if (!appEngine.rootObjects().isEmpty()) - QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load", Q_ARG(QVariant, startupUrl())); - else + if (appEngine.rootObjects().isEmpty()) qFatal("Failed to load sources"); + const QUrl url = startupUrl(parser); + QMetaObject::invokeMethod(appEngine.rootObjects().constFirst(), + "load", Q_ARG(QVariant, url)); + return app.exec(); } diff --git a/examples/webenginequick/quicknanobrowser/quicknanobrowser.pro b/examples/webenginequick/quicknanobrowser/quicknanobrowser.pro index 3a014fe20..e4c0cdfd1 100644 --- a/examples/webenginequick/quicknanobrowser/quicknanobrowser.pro +++ b/examples/webenginequick/quicknanobrowser/quicknanobrowser.pro @@ -10,6 +10,10 @@ RESOURCES += resources.qrc QT += qml quick webenginequick +CONFIG += qmltypes +QML_IMPORT_NAME = BrowserUtils +QML_IMPORT_MAJOR_VERSION = 1 + qtHaveModule(widgets) { QT += widgets # QApplication is required to get native styling with QtQuickControls } diff --git a/examples/webenginequick/quicknanobrowser/utils.h b/examples/webenginequick/quicknanobrowser/utils.h index 0c57ab19b..6c11e75fb 100644 --- a/examples/webenginequick/quicknanobrowser/utils.h +++ b/examples/webenginequick/quicknanobrowser/utils.h @@ -1,18 +1,24 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + #ifndef UTILS_H #define UTILS_H +#include + #include #include -class Utils : public QObject { +class Utils : public QObject +{ Q_OBJECT + QML_ELEMENT + QML_SINGLETON public: - Q_INVOKABLE static QUrl fromUserInput(const QString& userInput); + Q_INVOKABLE static QUrl fromUserInput(const QString &userInput); }; -inline QUrl Utils::fromUserInput(const QString& userInput) +inline QUrl Utils::fromUserInput(const QString &userInput) { QFileInfo fileInfo(userInput); if (fileInfo.exists()) -- cgit v1.2.3