From 8bfc247bd4616fa7472ea1ae65ee72650d40c12f Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Mon, 20 Mar 2017 17:26:47 +0300 Subject: weatherinfo example: use lambdas instead of QSignalMapper QSignalMapper is deprecated since Qt 5.10. Besides code looks clearer with lambdas. Change-Id: I7e4681e0d6b354cd8da9a4506dad917799688dc9 Reviewed-by: Alex Blasche --- examples/positioning/weatherinfo/appmodel.cpp | 36 +++++++-------------------- examples/positioning/weatherinfo/appmodel.h | 7 +++--- 2 files changed, 12 insertions(+), 31 deletions(-) (limited to 'examples/positioning/weatherinfo') diff --git a/examples/positioning/weatherinfo/appmodel.cpp b/examples/positioning/weatherinfo/appmodel.cpp index 4d8806b8..f17d8141 100644 --- a/examples/positioning/weatherinfo/appmodel.cpp +++ b/examples/positioning/weatherinfo/appmodel.cpp @@ -47,7 +47,6 @@ #include #include -#include #include #include #include @@ -145,8 +144,6 @@ public: WeatherData now; QList forecast; QQmlListProperty *fcProp; - QSignalMapper *geoReplyMapper; - QSignalMapper *weatherReplyMapper, *forecastReplyMapper; bool ready; bool useGps; QElapsedTimer throttle; @@ -210,16 +207,6 @@ AppModel::AppModel(QObject *parent) : forecastAt, forecastClear); - d->geoReplyMapper = new QSignalMapper(this); - d->weatherReplyMapper = new QSignalMapper(this); - d->forecastReplyMapper = new QSignalMapper(this); - - connect(d->geoReplyMapper, SIGNAL(mapped(QObject*)), - this, SLOT(handleGeoNetworkData(QObject*))); - connect(d->weatherReplyMapper, SIGNAL(mapped(QObject*)), - this, SLOT(handleWeatherNetworkData(QObject*))); - connect(d->forecastReplyMapper, SIGNAL(mapped(QObject*)), - this, SLOT(handleForecastNetworkData(QObject*))); connect(&d->delayedCityRequestTimer, SIGNAL(timeout()), this, SLOT(queryCity())); connect(&d->requestNewWeatherTimer, SIGNAL(timeout()), @@ -312,9 +299,8 @@ void AppModel::queryCity() QNetworkReply *rep = d->nam->get(QNetworkRequest(url)); // connect up the signal right away - d->geoReplyMapper->setMapping(rep, rep); - connect(rep, SIGNAL(finished()), - d->geoReplyMapper, SLOT(map())); + connect(rep, &QNetworkReply::finished, + this, [this, rep]() { handleGeoNetworkData(rep); }); } void AppModel::positionError(QGeoPositionInfoSource::Error e) @@ -344,9 +330,8 @@ void AppModel::hadError(bool tryAgain) d->delayedCityRequestTimer.start(); } -void AppModel::handleGeoNetworkData(QObject *replyObj) +void AppModel::handleGeoNetworkData(QNetworkReply *networkReply) { - QNetworkReply *networkReply = qobject_cast(replyObj); if (!networkReply) { hadError(false); // should retry? return; @@ -393,9 +378,8 @@ void AppModel::refreshWeather() QNetworkReply *rep = d->nam->get(QNetworkRequest(url)); // connect up the signal right away - d->weatherReplyMapper->setMapping(rep, rep); - connect(rep, SIGNAL(finished()), - d->weatherReplyMapper, SLOT(map())); + connect(rep, &QNetworkReply::finished, + this, [this, rep]() { handleWeatherNetworkData(rep); }); } static QString niceTemperatureString(double t) @@ -403,10 +387,9 @@ static QString niceTemperatureString(double t) return QString::number(qRound(t-ZERO_KELVIN)) + QChar(0xB0); } -void AppModel::handleWeatherNetworkData(QObject *replyObj) +void AppModel::handleWeatherNetworkData(QNetworkReply *networkReply) { qCDebug(requestsLog) << "got weather network data"; - QNetworkReply *networkReply = qobject_cast(replyObj); if (!networkReply) return; @@ -452,14 +435,13 @@ void AppModel::handleWeatherNetworkData(QObject *replyObj) QNetworkReply *rep = d->nam->get(QNetworkRequest(url)); // connect up the signal right away - d->forecastReplyMapper->setMapping(rep, rep); - connect(rep, SIGNAL(finished()), d->forecastReplyMapper, SLOT(map())); + connect(rep, &QNetworkReply::finished, + this, [this, rep]() { handleForecastNetworkData(rep); }); } -void AppModel::handleForecastNetworkData(QObject *replyObj) +void AppModel::handleForecastNetworkData(QNetworkReply *networkReply) { qCDebug(requestsLog) << "got forecast"; - QNetworkReply *networkReply = qobject_cast(replyObj); if (!networkReply) return; diff --git a/examples/positioning/weatherinfo/appmodel.h b/examples/positioning/weatherinfo/appmodel.h index 024f314f..9809284b 100644 --- a/examples/positioning/weatherinfo/appmodel.h +++ b/examples/positioning/weatherinfo/appmodel.h @@ -149,10 +149,9 @@ private slots: void networkSessionOpened(); void positionUpdated(QGeoPositionInfo gpsPos); void positionError(QGeoPositionInfoSource::Error e); - // these would have QNetworkReply* params but for the signalmapper - void handleGeoNetworkData(QObject *networkReply); - void handleWeatherNetworkData(QObject *networkReply); - void handleForecastNetworkData(QObject *networkReply); + void handleGeoNetworkData(QNetworkReply *networkReply); + void handleWeatherNetworkData(QNetworkReply *networkReply); + void handleForecastNetworkData(QNetworkReply *networkReply); //! [3] signals: -- cgit v1.2.3