diff options
author | Friedemann Kleint <[email protected]> | 2024-08-02 15:54:16 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <[email protected]> | 2024-08-07 17:31:56 +0000 |
commit | 3cbbf09d189db4dd1d9421f86446e3b03831a2a0 (patch) | |
tree | abbfc0845f438479eb9a137d2865d4691d44fa6c | |
parent | b3f985b749734972cafdc1d7631b2bf2bca9b5ce (diff) |
Polish the minibrowser example
- Use string literals
- Streamline the code
- Use initial properties for the window position
Pick-to: 6.7
Task-number: PYSIDE-2206
Change-Id: I15d9d745e52625281560ab42acda563559a9f164
Reviewed-by: Shyamnath Premnadh <[email protected]>
Reviewed-by: Fabian Kosmale <[email protected]>
(cherry picked from commit 44ec15a4e65afff79a749b57f36d15a6dd42b5a1)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
-rw-r--r-- | examples/webview/minibrowser/main.cpp | 45 | ||||
-rw-r--r-- | examples/webview/minibrowser/main.qml | 4 |
2 files changed, 25 insertions, 24 deletions
diff --git a/examples/webview/minibrowser/main.cpp b/examples/webview/minibrowser/main.cpp index c05855b..0027834 100644 --- a/examples/webview/minibrowser/main.cpp +++ b/examples/webview/minibrowser/main.cpp @@ -11,20 +11,25 @@ #include <QtQml/QQmlContext> #include <QtWebView/QtWebView> +using namespace Qt::StringLiterals; + // Workaround: As of Qt 5.4 QtQuick does not expose QUrl::fromUserInput. -class Utils : public QObject { +class Utils : public QObject +{ Q_OBJECT public: - Utils(QObject *parent = nullptr) : QObject(parent) { } - Q_INVOKABLE static QUrl fromUserInput(const QString& userInput); + using QObject::QObject; + + Q_INVOKABLE static QUrl fromUserInput(const QString &userInput); }; -QUrl Utils::fromUserInput(const QString& userInput) +QUrl Utils::fromUserInput(const QString &userInput) { - if (userInput.isEmpty()) - return QUrl::fromUserInput("about:blank"); - const QUrl result = QUrl::fromUserInput(userInput); - return result.isValid() ? result : QUrl::fromUserInput("about:blank"); + if (!userInput.isEmpty()) { + if (const QUrl result = QUrl::fromUserInput(userInput); result.isValid()) + return result; + } + return QUrl::fromUserInput("about:blank"_L1); } #include "main.moc" @@ -42,17 +47,16 @@ int main(int argc, char *argv[]) parser.setApplicationDescription(QGuiApplication::applicationDisplayName()); parser.addHelpOption(); parser.addVersionOption(); - parser.addPositionalArgument("url", "The initial URL to open."); - QStringList arguments = app.arguments(); - parser.process(arguments); - const QString initialUrl = parser.positionalArguments().isEmpty() ? - QStringLiteral("https://www.qt.io") : parser.positionalArguments().first(); + parser.addPositionalArgument("url"_L1, "The initial URL to open."_L1); + parser.process(QCoreApplication::arguments()); + const QString initialUrl = parser.positionalArguments().value(0, "https://www.qt.io"_L1); QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); - context->setContextProperty(QStringLiteral("utils"), new Utils(&engine)); - context->setContextProperty(QStringLiteral("initialUrl"), + context->setContextProperty("utils"_L1, new Utils(&engine)); + context->setContextProperty("initialUrl"_L1, Utils::fromUserInput(initialUrl)); + QRect geometry = QGuiApplication::primaryScreen()->availableGeometry(); if (!QGuiApplication::styleHints()->showIsFullScreen()) { const QSize size = geometry.size() * 4 / 5; @@ -60,12 +64,13 @@ int main(int argc, char *argv[]) const QPoint pos = geometry.topLeft() + QPoint(offset.width(), offset.height()); geometry = QRect(pos, size); } - context->setContextProperty(QStringLiteral("initialX"), geometry.x()); - context->setContextProperty(QStringLiteral("initialY"), geometry.y()); - context->setContextProperty(QStringLiteral("initialWidth"), geometry.width()); - context->setContextProperty(QStringLiteral("initialHeight"), geometry.height()); - engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + engine.setInitialProperties(QVariantMap{{"x"_L1, geometry.x()}, + {"y"_L1, geometry.y()}, + {"width"_L1, geometry.width()}, + {"height"_L1, geometry.height()}}); + + engine.load(QUrl("qrc:/main.qml"_L1)); if (engine.rootObjects().isEmpty()) return -1; diff --git a/examples/webview/minibrowser/main.qml b/examples/webview/minibrowser/main.qml index 2ca4277..f647c62 100644 --- a/examples/webview/minibrowser/main.qml +++ b/examples/webview/minibrowser/main.qml @@ -10,10 +10,6 @@ import QtQuick.Layouts ApplicationWindow { id: window visible: true - x: initialX - y: initialY - width: initialWidth - height: initialHeight title: webView.title menuBar: ToolBar { |