diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-09-06 08:46:50 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-09-08 12:48:22 +0000 |
commit | 72a0caa3fe9dfe9bd377786fddaab0d44bd6b254 (patch) | |
tree | 8faa2206998a011a14bb15eb4608a75504c5aef3 | |
parent | a9d649d02585e5954e11459a2d472d33df8d6489 (diff) |
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 <allan.jensen@qt.io>
(cherry picked from commit 98bae072c090ee19e2a5a1e85eed712a060d12fc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
10 files changed, 54 insertions, 30 deletions
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 <QtGui/QGuiApplication> +#include <QtWebEngineQuick/qtwebenginequickglobal.h> + #include <QtQml/QQmlApplicationEngine> #include <QtQml/QQmlContext> -#include <QtWebEngineQuick/qtwebenginequickglobal.h> -static QUrl startupUrl() +#include <QtGui/QGuiApplication> + +#include <QtCore/QCommandLineParser> +#include <QtCore/QCommandLineOption> + +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 <QtQml/qqml.h> + #include <QtCore/QFileInfo> #include <QtCore/QUrl> -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()) |