summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-03-15 17:55:36 +0100
committerIvan Solovev <ivan.solovev@qt.io>2021-03-23 10:44:54 +0100
commit70ceffff9c008d467f6ce352b89f13f5cc6f45a6 (patch)
treef375b51c8bc84d2e059ba7e7f99aa0849a79cfd5
parentfcb02f59b29dc5013d4b20ceb44ae41c825c0312 (diff)
downloadqtlocation-70ceffff9c008d467f6ce352b89f13f5cc6f45a6.tar.gz
QtPositioning: update weatherinfo example
Update the logic of the queryCity(): - call it immediately afther GPS state change, if the GPS position is already received (not wait for the next positionUpdate, as it may come only when coordinates actually change, depending on used plugin). - check that we still use GPS, when the reply is received (as we could have already switched to another city). Also update the QML registration to Qt6 way. Task-number: QTBUG-89855 Change-Id: I0bb1aa445763a7c9b125008d71220e1e735c1b6f Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--examples/positioning/weatherinfo/CMakeLists.txt6
-rw-r--r--examples/positioning/weatherinfo/appmodel.cpp10
-rw-r--r--examples/positioning/weatherinfo/appmodel.h5
-rw-r--r--examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc67
-rw-r--r--examples/positioning/weatherinfo/main.cpp10
-rw-r--r--examples/positioning/weatherinfo/weatherinfo.pro4
6 files changed, 71 insertions, 31 deletions
diff --git a/examples/positioning/weatherinfo/CMakeLists.txt b/examples/positioning/weatherinfo/CMakeLists.txt
index 5ed9b48e..d120fb90 100644
--- a/examples/positioning/weatherinfo/CMakeLists.txt
+++ b/examples/positioning/weatherinfo/CMakeLists.txt
@@ -72,3 +72,9 @@ install(TARGETS weatherinfo
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
+
+set_target_properties(weatherinfo PROPERTIES
+ QT_QML_MODULE_VERSION 1.0
+ QT_QML_MODULE_URI WeatherInfo
+)
+qt6_qml_type_registration(weatherinfo)
diff --git a/examples/positioning/weatherinfo/appmodel.cpp b/examples/positioning/weatherinfo/appmodel.cpp
index e498da5b..687ab073 100644
--- a/examples/positioning/weatherinfo/appmodel.cpp
+++ b/examples/positioning/weatherinfo/appmodel.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -340,7 +340,9 @@ void AppModel::handleGeoNetworkData(QNetworkReply *networkReply)
const QString city = jv.toString();
qCDebug(requestsLog) << "got city: " << city;
- if (city != d->city) {
+ // The reply is asynchronous, so it can come also after we switched
+ // to the next city. In this case we should not handle it.
+ if (city != d->city && d->useGps) {
d->city = city;
emit cityChanged();
refreshWeather();
@@ -535,6 +537,10 @@ void AppModel::setUseGps(bool value)
d->throttle.invalidate();
emit cityChanged();
emit weatherChanged();
+ // if we already have a valid GPS position, do not wait until it
+ // updates, but query the city immediately
+ if (d->coord.isValid())
+ queryCity();
}
emit useGpsChanged();
}
diff --git a/examples/positioning/weatherinfo/appmodel.h b/examples/positioning/weatherinfo/appmodel.h
index f6657a59..44c8f9f9 100644
--- a/examples/positioning/weatherinfo/appmodel.h
+++ b/examples/positioning/weatherinfo/appmodel.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -55,6 +55,7 @@
#include <QtCore/QString>
#include <QtNetwork/QNetworkReply>
#include <QtQml/QQmlListProperty>
+#include <QtQml/qqml.h>
#include <QtPositioning/QGeoPositionInfoSource>
@@ -73,6 +74,7 @@ class WeatherData : public QObject {
Q_PROPERTY(QString temperature
READ temperature WRITE setTemperature
NOTIFY dataChanged)
+ QML_ANONYMOUS
public:
explicit WeatherData(QObject *parent = 0);
@@ -131,6 +133,7 @@ class AppModel : public QObject
Q_PROPERTY(QQmlListProperty<WeatherData> forecast
READ forecast
NOTIFY weatherChanged)
+ QML_ELEMENT
public:
explicit AppModel(QObject *parent = 0);
diff --git a/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc b/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc
index 33a95f70..f12d0216 100644
--- a/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc
+++ b/examples/positioning/weatherinfo/doc/src/weatherinfo.qdoc
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt Toolkit.
@@ -45,27 +45,31 @@
\include examples-run.qdocinc
+ \section1 Application Data Models
+
The example uses weather data provided by \l http://www.openweathermap.org.
The key part of this example is the application's data model, contained
- in the WeatherData and AppModel classes. WeatherData represents the weather
- information taken from the HTTP service. It is a simple data class, but we
- give it Q_PROPERTies to expose it nicely to QML, later.
+ in the \c WeatherData and \c AppModel classes. \c WeatherData represents
+ the weather information taken from the HTTP service. It is a simple data
+ class, but we use \l Q_PROPERTY to expose it nicely to QML later.
+ It also uses \l QML_ANONYMOUS macro, which makes it recognized in QML.
\snippet weatherinfo/appmodel.h 0
\snippet weatherinfo/appmodel.h 1
- AppModel models the state of the entire application. At startup, we
+ \c AppModel models the state of the entire application. At startup, we
get the platform's default position source using
- QGeoPositionInfo::createDefaultSource()
+ \l QGeoPositionInfoSource::createDefaultSource()
\snippet weatherinfo/appmodel.cpp 0
\snippet weatherinfo/appmodel.cpp 1
If no default source is available, we take a static position and fetch
weather for that. If, however, we do have a position source, we connect
- its positionUpdated() signal to a slot on the AppModel and call
- startUpdates(), which begins regular updates of device position.
+ its \l {QGeoPositionInfoSource::}{positionUpdated()} signal to a slot on the
+ \c AppModel and call \l {QGeoPositionInfoSource::}{startUpdates()}, which
+ begins regular updates of device position.
When a position update is received, we use the longitude and latitude
of the returned coordinate to retrieve the current "city" name for use
@@ -73,33 +77,58 @@
\snippet weatherinfo/appmodel.cpp 2
- To inform the UI about this process, the cityChanged() signal is emitted
- when a new city is used, and the weatherChanged() signal whenever a
+ To inform the UI about this process, the \c cityChanged() signal is emitted
+ when a new city is used, and the \c weatherChanged() signal whenever a
weather update occurs.
+ The model also uses \l QML_ELEMENT macro, which makes it available in QML.
+
\snippet weatherinfo/appmodel.h 2
\snippet weatherinfo/appmodel.h 3
\snippet weatherinfo/appmodel.h 4
- We use a QQmlListProperty for the weather forecast information,
+ We use a \l QQmlListProperty for the weather forecast information,
which contains the next 4 days of forecast weather. This makes it
easy to access from QML.
- To expose these to the QML UI layer, we use the qmlRegisterType()
- function. We call this once for each type we wish to register, before
- loading the actual QML file.
+ \section1 Expose Custom Models to QML
+
+ To expose the models to the QML UI layer, we use the \l QML_ELEMENT and
+ \l QML_ANONYMOUS macros. See the \l QQmlEngine class description for more
+ details on these macros.
+
+ To make the types available in QML, we need to update our build accordingly.
+
+ \section2 CMake Build
+
+ For a CMake-based build, we need to add the following to the
+ \c {CMakeLists.txt}:
+
+ \quotefromfile weatherinfo/CMakeLists.txt
+ \skipto set_target_properties
+ \printuntil PROPERTIES
+ \skipto QT_QML_MODULE_VERSION 1.0
+ \printuntil qt6_qml_type_registration(weatherinfo)
+
+ \section2 qmake Build
+
+ For a qmake build, we need to modify the \c {weatherinfo.pro} file in the
+ following way:
+
+ \quotefromfile weatherinfo/weatherinfo.pro
+ \skipto CONFIG
+ \printuntil QML_IMPORT_MAJOR_VERSION
- \snippet weatherinfo/main.cpp 0
- \snippet weatherinfo/main.cpp 1
+ \section1 Instantiate the Models in QML
- Finally, in the actual QML, we instantiate the AppModel.
+ Finally, in the actual QML, we instantiate the \c {AppModel}:
\snippet weatherinfo/weatherinfo.qml 0
\snippet weatherinfo/weatherinfo.qml 1
\snippet weatherinfo/weatherinfo.qml 2
- Once instantiated like this, we can use its properties elsewhere in the
- QML document:
+ Once the model is instantiated like this, we can use its properties
+ elsewhere in the QML document:
\snippet weatherinfo/weatherinfo.qml 3
\snippet weatherinfo/weatherinfo.qml 4
diff --git a/examples/positioning/weatherinfo/main.cpp b/examples/positioning/weatherinfo/main.cpp
index 13f532af..a002a244 100644
--- a/examples/positioning/weatherinfo/main.cpp
+++ b/examples/positioning/weatherinfo/main.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -55,7 +55,6 @@
#include <QtQuick/QQuickItem>
#include <QLoggingCategory>
-//! [0]
#include "appmodel.h"
int main(int argc, char *argv[])
@@ -63,12 +62,6 @@ int main(int argc, char *argv[])
QLoggingCategory::setFilterRules("wapp.*.debug=false");
QGuiApplication application(argc, argv);
- qmlRegisterType<WeatherData>("WeatherInfo", 1, 0, "WeatherData");
- qmlRegisterType<AppModel>("WeatherInfo", 1, 0, "AppModel");
-
-//! [0]
- qRegisterMetaType<WeatherData>();
-//! [1]
const QString mainQmlApp = QStringLiteral("qrc:///weatherinfo.qml");
QQuickView view;
view.setSource(QUrl(mainQmlApp));
@@ -79,4 +72,3 @@ int main(int argc, char *argv[])
view.show();
return application.exec();
}
-//! [1]
diff --git a/examples/positioning/weatherinfo/weatherinfo.pro b/examples/positioning/weatherinfo/weatherinfo.pro
index e2b2102d..376bf3f5 100644
--- a/examples/positioning/weatherinfo/weatherinfo.pro
+++ b/examples/positioning/weatherinfo/weatherinfo.pro
@@ -3,6 +3,10 @@ TARGET = weatherinfo
QT += core network positioning qml quick
+CONFIG += qmltypes
+QML_IMPORT_NAME = WeatherInfo
+QML_IMPORT_MAJOR_VERSION = 1
+
SOURCES += main.cpp \
appmodel.cpp