diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-03-07 14:12:42 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-03-07 18:20:26 +0000 |
commit | adc4f4f154e19f685e6a9dab08775bf912003183 (patch) | |
tree | bcc4fdab282d59e6ef20f1a22141f70cfe7bf785 | |
parent | 3a6597e1b6c437f04ba3fd9b8875def8e7298941 (diff) | |
download | qtlocation-adc4f4f154e19f685e6a9dab08775bf912003183.tar.gz |
map viewer/places examples: Brush up the code
- Use modern string literals
- Use the variadic template version of QMetaObject::invokeMethod()
- Use modern connect syntax
- Reorder includes
- Trim newlines in QML
Fix minor bugs:
- Skip empty elements of library path (namely when variable is not
set)
- Fix the check for osm.useragent to operate on the map
- Exit cleanly on QML loading errors
Since places uses similar code, apply the changes there as well.
Change-Id: Ibaeffc6e003c55c458137458c5017584e0c072f1
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 20b0c2fb20f3c37c2549b7aa59f686a7726a2817)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | examples/location/mapviewer/forms/Geocode.qml | 2 | ||||
-rw-r--r-- | examples/location/mapviewer/forms/RouteCoordinate.qml | 1 | ||||
-rw-r--r-- | examples/location/mapviewer/forms/RouteListDelegate.qml | 3 | ||||
-rw-r--r-- | examples/location/mapviewer/main.cpp | 95 | ||||
-rw-r--r-- | examples/location/places/main.cpp | 75 |
5 files changed, 83 insertions, 93 deletions
diff --git a/examples/location/mapviewer/forms/Geocode.qml b/examples/location/mapviewer/forms/Geocode.qml index 448441ec..99accad6 100644 --- a/examples/location/mapviewer/forms/Geocode.qml +++ b/examples/location/mapviewer/forms/Geocode.qml @@ -40,5 +40,3 @@ GeocodeForm { postalCode.text = address.postalCode } } - - diff --git a/examples/location/mapviewer/forms/RouteCoordinate.qml b/examples/location/mapviewer/forms/RouteCoordinate.qml index ca289e93..f2eb771f 100644 --- a/examples/location/mapviewer/forms/RouteCoordinate.qml +++ b/examples/location/mapviewer/forms/RouteCoordinate.qml @@ -39,4 +39,3 @@ RouteCoordinateForm { toLongitude.text = "" + toCoordinate.longitude } } - diff --git a/examples/location/mapviewer/forms/RouteListDelegate.qml b/examples/location/mapviewer/forms/RouteListDelegate.qml index 30e43503..dbefbaa3 100644 --- a/examples/location/mapviewer/forms/RouteListDelegate.qml +++ b/examples/location/mapviewer/forms/RouteListDelegate.qml @@ -40,6 +40,3 @@ Item { color: "#46a2da" } } - - - diff --git a/examples/location/mapviewer/main.cpp b/examples/location/mapviewer/main.cpp index b24bb392..8cb804b7 100644 --- a/examples/location/mapviewer/main.cpp +++ b/examples/location/mapviewer/main.cpp @@ -1,70 +1,69 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtCore/QTextStream> -#include <QtGui/QGuiApplication> #include <QtQml/QQmlApplicationEngine> -#include <QtQuick/QQuickItem> -#if QT_CONFIG(ssl) -#include <QSslSocket> -#endif -#include <QQmlContext> +#include <QtQml/QQmlContext> -static bool parseArgs(QStringList& args, QVariantMap& parameters) -{ +#include <QtGui/QGuiApplication> - while (!args.isEmpty()) { +#if QT_CONFIG(ssl) +#include <QtNetwork/QSslSocket> +#endif - QString param = args.takeFirst(); +#include <iostream> - if (param.startsWith("--help")) { - QTextStream out(stdout); - out << "Usage: " << Qt::endl; - out << "--plugin.<parameter_name> <parameter_value> - Sets parameter = value for plugin" << Qt::endl; - out.flush(); - return true; - } +using namespace Qt::StringLiterals; - if (param.startsWith("--plugin.")) { +static const char *help = R"(Usage: +--plugin.<parameter_name> <parameter_value> - Sets parameter = value for plugin +)"; +static QVariantMap parseArgs(QStringList args) +{ + QVariantMap parameters; + while (!args.isEmpty()) { + QString param = args.takeFirst(); + if (param.startsWith(u"--plugin.")) { param.remove(0, 9); - - if (args.isEmpty() || args.first().startsWith("--")) { - parameters[param] = true; + if (args.isEmpty() || args.constFirst().startsWith(u"--")) { + parameters.insert(param, QVariant(true)); } else { - QString value = args.takeFirst(); - - if (value == "true" || value == "on" || value == "enabled") { - parameters[param] = true; - } else if (value == "false" || value == "off" - || value == "disable") { - parameters[param] = false; + if (value == u"true" || value == u"on" || value == u"enabled") { + parameters.insert(param, QVariant(true)); + } else if (value == u"false" || value == u"off" + || value == u"disable") { + parameters.insert(param, QVariant(false)); } else { - parameters[param] = value; + parameters.insert(param, QVariant(value)); } } } } - return false; + return parameters; } int main(int argc, char *argv[]) { #if QT_CONFIG(library) - const QByteArray additionalLibraryPaths = qgetenv("QTLOCATION_EXTRA_LIBRARY_PATH"); - for (const QByteArray &p : additionalLibraryPaths.split(':')) - QCoreApplication::addLibraryPath(QString(p)); + const QString additionalLibraryPaths = qEnvironmentVariable("QTLOCATION_EXTRA_LIBRARY_PATH"); + for (const auto &p : additionalLibraryPaths.split(u':', Qt::SkipEmptyParts)) + QCoreApplication::addLibraryPath(p); #endif - QGuiApplication application(argc, argv); - QVariantMap parameters; - QStringList args(QCoreApplication::arguments()); + QGuiApplication application(argc, argv); + QCoreApplication::setApplicationName(u"QtLocation Mapviewer example"_s); - if (parseArgs(args, parameters)) + QStringList args = QCoreApplication::arguments(); + args.removeFirst(); + if (args.contains(u"--help")) { + std::cout << qPrintable(QCoreApplication::applicationName()) << "\n\n" << help; return 0; - if (!args.contains(QStringLiteral("osm.useragent"))) - parameters[QStringLiteral("osm.useragent")] = QStringLiteral("QtLocation Mapviewer example"); + } + + QVariantMap parameters = parseArgs(args); + if (!parameters.contains(u"osm.useragent"_s)) + parameters.insert(u"osm.useragent"_s, QCoreApplication::applicationName()); QQmlApplicationEngine engine; #if QT_CONFIG(ssl) @@ -72,16 +71,16 @@ int main(int argc, char *argv[]) #else engine.rootContext()->setContextProperty("supportsSsl", false); #endif - engine.addImportPath(QStringLiteral(":/imports")); - engine.load(QUrl(QStringLiteral("qrc:///mapviewer.qml"))); - QObject::connect(&engine, SIGNAL(quit()), qApp, SLOT(quit())); - - QObject *item = engine.rootObjects().first(); - Q_ASSERT(item); + engine.addImportPath(u":/imports"_s); + engine.load(QUrl(u"qrc:///mapviewer.qml"_s)); + QObject::connect(&engine, &QQmlApplicationEngine::quit, + qApp, QCoreApplication::quit); - QMetaObject::invokeMethod(item, "initializeProviders", - Q_ARG(QVariant, QVariant::fromValue(parameters))); + auto *item = engine.rootObjects().value(0); + if (item == nullptr) + return -1; + QMetaObject::invokeMethod(item, "initializeProviders", QVariant::fromValue(parameters)); return application.exec(); } diff --git a/examples/location/places/main.cpp b/examples/location/places/main.cpp index 6885d231..32d392c5 100644 --- a/examples/location/places/main.cpp +++ b/examples/location/places/main.cpp @@ -1,71 +1,68 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include <QtCore/QTextStream> -#include <QtGui/QGuiApplication> #include <QtQml/QQmlApplicationEngine> #include <QtQml/QQmlContext> -#include <QtQuick/QQuickItem> -static bool parseArgs(QStringList& args, QVariantMap& parameters) -{ +#include <QtGui/QGuiApplication> - while (!args.isEmpty()) { +#include <iostream> - QString param = args.takeFirst(); +using namespace Qt::StringLiterals; - if (param.startsWith("--help")) { - QTextStream out(stdout); - out << "Usage: " << Qt::endl; - out << "--plugin.<parameter_name> <parameter_value> - Sets parameter = value for plugin" << Qt::endl; - out.flush(); - return true; - } - - if (param.startsWith("--plugin.")) { +static const char *help = R"(Usage: +--plugin.<parameter_name> <parameter_value> - Sets parameter = value for plugin +)"; +static QVariantMap parseArgs(QStringList args) +{ + QVariantMap parameters; + while (!args.isEmpty()) { + QString param = args.takeFirst(); + if (param.startsWith(u"--plugin.")) { param.remove(0, 9); - - if (args.isEmpty() || args.first().startsWith("--")) { - parameters[param] = true; + if (args.isEmpty() || args.constFirst().startsWith(u"--")) { + parameters.insert(param, QVariant(true)); } else { - QString value = args.takeFirst(); - - if (value == "true" || value == "on" || value == "enabled") { - parameters[param] = true; - } else if (value == "false" || value == "off" - || value == "disable") { - parameters[param] = false; + if (value == u"true" || value == u"on" || value == u"enabled") { + parameters.insert(param, QVariant(true)); + } else if (value == u"false" || value == u"off" + || value == u"disable") { + parameters.insert(param, QVariant(false)); } else { - parameters[param] = value; + parameters.insert(param, QVariant(value)); } } } } - return false; + return parameters; } int main(int argc, char *argv[]) { QGuiApplication application(argc, argv); - QVariantMap parameters; - QStringList args(QCoreApplication::arguments()); - - if (parseArgs(args, parameters)) + QStringList args = QCoreApplication::arguments(); + args.removeFirst(); + if (args.contains(u"--help")) { + std::cout << qPrintable(QCoreApplication::applicationName()) << "\n\n" << help; return 0; + } + + QVariantMap parameters = parseArgs(args); QQmlApplicationEngine engine; - engine.addImportPath(QStringLiteral(":/imports")); - engine.load(QUrl(QStringLiteral("qrc:///places.qml"))); - QObject::connect(&engine, SIGNAL(quit()), qApp, SLOT(quit())); + engine.addImportPath(u":/imports"_s); + engine.load(QUrl(u"qrc:///places.qml"_s)); + QObject::connect(&engine, &QQmlApplicationEngine::quit, + qApp, QCoreApplication::quit); - QObject *item = engine.rootObjects().first(); - Q_ASSERT(item); + auto *item = engine.rootObjects().value(0); + if (item == nullptr) + return -1; - QMetaObject::invokeMethod(item, "initializeProviders", - Q_ARG(QVariant, QVariant::fromValue(parameters))); + QMetaObject::invokeMethod(item, "initializeProviders", QVariant::fromValue(parameters)); return application.exec(); } |