diff options
Diffstat (limited to 'examples/webenginequick/lifecycle')
-rw-r--r-- | examples/webenginequick/lifecycle/CMakeLists.txt | 4 | ||||
-rw-r--r-- | examples/webenginequick/lifecycle/WebTab.qml | 2 | ||||
-rw-r--r-- | examples/webenginequick/lifecycle/doc/src/lifecycle.qdoc | 4 | ||||
-rw-r--r-- | examples/webenginequick/lifecycle/lifecycle.pro | 1 | ||||
-rw-r--r-- | examples/webenginequick/lifecycle/main.cpp | 6 | ||||
-rw-r--r-- | examples/webenginequick/lifecycle/utils.h | 25 |
6 files changed, 40 insertions, 2 deletions
diff --git a/examples/webenginequick/lifecycle/CMakeLists.txt b/examples/webenginequick/lifecycle/CMakeLists.txt index 556b50706..46478b622 100644 --- a/examples/webenginequick/lifecycle/CMakeLists.txt +++ b/examples/webenginequick/lifecycle/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + cmake_minimum_required(VERSION 3.16) project(lifecycle LANGUAGES CXX) @@ -13,6 +16,7 @@ find_package(Qt6 REQUIRED COMPONENTS Core Gui QuickControls2 WebEngineQuick) qt_add_executable(lifecycle main.cpp + utils.h ) set_target_properties(lifecycle PROPERTIES diff --git a/examples/webenginequick/lifecycle/WebTab.qml b/examples/webenginequick/lifecycle/WebTab.qml index d805628ad..6fb6cb386 100644 --- a/examples/webenginequick/lifecycle/WebTab.qml +++ b/examples/webenginequick/lifecycle/WebTab.qml @@ -132,7 +132,7 @@ ColumnLayout { text: view.url == "about:blank" ? "" : view.url selectByMouse: true - onAccepted: { view.url = text } + onAccepted: { view.url = utils.fromUserInput(text) } } WebToolButton { text: "⋮" diff --git a/examples/webenginequick/lifecycle/doc/src/lifecycle.qdoc b/examples/webenginequick/lifecycle/doc/src/lifecycle.qdoc index b28e3e272..1580da26d 100644 --- a/examples/webenginequick/lifecycle/doc/src/lifecycle.qdoc +++ b/examples/webenginequick/lifecycle/doc/src/lifecycle.qdoc @@ -6,6 +6,7 @@ \title WebEngine Lifecycle Example \ingroup webengine-examples \brief Freezes and discards background tabs to reduce CPU and memory usage. + \examplecategory {Web Technologies} \image lifecycle.png @@ -31,6 +32,9 @@ window also has a \l {Drawer} for changing settings. The drawer can be opened by clicking the "⋮" button on the tool bar. + \note Note that \c {WebTab.qml} uses \l {QUrl::} + {fromUserInput} to handle incomplete URLs. + \section1 Lifecycle States in the Example The example implements two ways of changing the lifecycle state: manual and diff --git a/examples/webenginequick/lifecycle/lifecycle.pro b/examples/webenginequick/lifecycle/lifecycle.pro index ccbe801f3..044d025d7 100644 --- a/examples/webenginequick/lifecycle/lifecycle.pro +++ b/examples/webenginequick/lifecycle/lifecycle.pro @@ -2,6 +2,7 @@ TEMPLATE = app QT += quickcontrols2 webenginequick +HEADERS += utils.h SOURCES += main.cpp RESOURCES += resources.qrc diff --git a/examples/webenginequick/lifecycle/main.cpp b/examples/webenginequick/lifecycle/main.cpp index 3601bdf5c..1f45ad0ee 100644 --- a/examples/webenginequick/lifecycle/main.cpp +++ b/examples/webenginequick/lifecycle/main.cpp @@ -1,6 +1,8 @@ -// Copyright (C) 2019 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 <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> @@ -12,6 +14,8 @@ int main(int argc, char *argv[]) QtWebEngineQuick::initialize(); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; + Utils utils; + engine.rootContext()->setContextProperty("utils", &utils); engine.load(QUrl(QStringLiteral("qrc:/WebBrowser.qml"))); return app.exec(); } diff --git a/examples/webenginequick/lifecycle/utils.h b/examples/webenginequick/lifecycle/utils.h new file mode 100644 index 000000000..d9a803907 --- /dev/null +++ b/examples/webenginequick/lifecycle/utils.h @@ -0,0 +1,25 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef UTILS_H +#define UTILS_H + +#include <QtCore/QFileInfo> +#include <QtCore/QUrl> + +class Utils : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE static QUrl fromUserInput(const QString &userInput); +}; + +inline QUrl Utils::fromUserInput(const QString &userInput) +{ + QFileInfo fileInfo(userInput); + if (fileInfo.exists()) + return QUrl::fromLocalFile(fileInfo.absoluteFilePath()); + return QUrl::fromUserInput(userInput); +} + +#endif // UTILS_H |