Qt 天气预报

news/2024/11/16 16:55:00/

通过调用天气服务器接口来获取天气信息,随后对获取到的json格式信息进行提取和转换

这里给出一个获取天气服务器的接口:http://wthrcdn.etouch.cn/weather_mini?city=
运行结果如下:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

获取信息功能实现:

weather.cpp:
#include "weather.h"Weather::Weather(QObject *parent) : QObject(parent)
{manager = new QNetworkAccessManager(this);  //新建QNetworkAccessManager对象connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));//关联信号和槽
}Weather::Weather(QString cityName)
{manager = new QNetworkAccessManager(this);  //新建QNetworkAccessManager对象connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));//关联信号和槽this->CityName=cityName;refresh();
}void Weather::refresh()
{this->TemperMax.clear();this->currentTemper.clear();this->Wind.clear();this->WeatherType.clear();this->Fever.clear();this->allInfo.clear();queryWeather();dataMap.clear();//刷新的清空,待获取时在加载
}void Weather::queryWeather()
{char request[256]="http://wthrcdn.etouch.cn/weather_mini?city=";  //"http://wthrcdn.etouch.cn/weather_mini?city="为天气服务器接口QNetworkRequest quest;sprintf(request,"%s%s",request,CityName.toUtf8().data());quest.setUrl(QUrl(request));quest.setHeader(QNetworkRequest::UserAgentHeader,"RT-Thread ART");manager->get(quest);
}//Json文件  例:合肥
//QJsonDocument({"data":{"city":"合肥",
//      "forecast":[{"date":"27日星期四","fengli":"<![CDATA[2级]]>",
//       "fengxiang":"西北风","high":"高温 29℃","low":"低温 21℃","type":"多云"},
//       {"date":"28日星期五","fengli":"<![CDATA[2级]]>","fengxiang":"北风","high":"高温 30℃","low":"低温 21℃","type":"多云"}
//       ,{"date":"29日星期六","fengli":"<![CDATA[2级]]>","fengxiang":"东北风","high":"高温 32℃","low":"低温 24℃","type":"多云"},
//        {"date":"30日星期天","fengli":"<![CDATA[2级]]>","fengxiang":"东北风","high":"高温 31℃","low":"低温 23℃","type":"小雨"},
//        {"date":"31日星期一","fengli":"<![CDATA[2级]]>","fengxiang":"东北风","high":"高温 29℃","low":"低温 22℃","type":"小雨"}],
//         "ganmao":"感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。","wendu":"25","yesterday":{"date":"26日星期三",
//       "fl":"<![CDATA[3级]]>","fx":"西北风","high":"高温 30℃","low":"低温 24℃","type":"中雨"}},"desc":"OK","status":1000})QMap<QString, QMap<QString, QString> > Weather::getDataMap(bool *ok)
{bool Oktemp;if(ok==nullptr){ok=&Oktemp;}if(!this->dataMap.isEmpty()){*ok=true;return this->dataMap;}*ok=false;if(!this->isGetData)   //是否成功获取数据return this->dataMap;QJsonParseError err;QJsonDocument json_recv=QJsonDocument::fromJson(allInfo.toUtf8(),&err);  //解析json对象if(!json_recv.isNull()){QJsonObject object=json_recv.object();if(object.contains("data")){QJsonValue value=object.value("data");  //获取指定key对应的valueif(value.isObject()){QJsonObject object_data=value.toObject();if(object_data.contains("yesterday")&&object_data.contains("forecast"))  //若存在昨日或与预测天气则加载所有数据{QJsonValue value=object_data.value("yesterday");if(value.isObject()){QMap<QString,QString>mapvalue;mapvalue["high"]=value.toObject().value("high").toString();mapvalue["low"]=value.toObject().value("low").toString();mapvalue["fengxiang"]=value.toObject().value("fx").toString();mapvalue["fengli"]=value.toObject().value("fl").toString();mapvalue["type"]=value.toObject().value("type").toString();dataMap[value.toObject().value("date").toString()]=mapvalue;}value=object_data.value("forecast");if(value.isArray()){QJsonArray valueArray=value.toArray();for(int i=0;i<valueArray.count();i++){QJsonObject object=valueArray.at(i).toObject();QMap<QString,QString>mapvalue;mapvalue["high"]=object.value("high").toString();mapvalue["low"]=object.value("low").toString();mapvalue["fengxiang"]=object.value("fengxiang").toString();mapvalue["fengli"]=object.value("fengli").toString();mapvalue["type"]=object.value("type").toString();dataMap[object.value("date").toString()]=mapvalue;}*ok=true;return dataMap;}}}}}return dataMap;
}void Weather::replyFinished(QNetworkReply *reply) //刷新的槽
{this->isGetData=false;allInfo=reply->readAll();QJsonParseError err;QJsonDocument json_recv=QJsonDocument::fromJson(allInfo.toUtf8(),&err);  //解析json对象if(!json_recv.isNull()){QJsonObject object=json_recv.object();if(object.contains("data")){QJsonValue value=object.value("data");  //获取指定key对应的valueif(value.isObject()){QJsonObject object_data=value.toObject();this->CityName=object_data.value("city").toString();this->currentTemper=object_data.value("wendu").toString();this->Fever=object_data.value("ganmao").toString();if(object_data.contains("forecast")){QJsonValue value=object_data.value("forecast");if(value.isArray()){QJsonObject today_weather=value.toArray().at(0).toObject();WeatherType=today_weather.value("type").toString();currentDate=today_weather.value("date").toString();QString low=today_weather.value("low").toString();QString high=today_weather.value("high").toString();TemperMax=low.mid(low.length()-3,4)+"-"+high.mid(high.length()-3,4);QString windStrength=today_weather.value("fengli").toString();windStrength.remove(0,8);windStrength.remove(windStrength.length()-2,2);Wind=today_weather.value("fengxiang").toString()+windStrength;this->isGetData=true;}}}}}reply->deleteLater();if(isGetData){emit this->getDataSuccessedSignal();}else{emit this->getDataFailedSignal();}emit this->getDataFinisedSignal();
}QString Weather::getAllInfo()
{return this->allInfo;
}
void Weather::setCityName(QString city)
{this->CityName=city;
}QString Weather::GetCityName()
{return this->CityName;
}QString Weather::GetDate()
{return this->currentDate;
}QString Weather::GetTemperature()
{return this->currentTemper;
}QString Weather::GetTemperatureMax()
{return this->TemperMax;
}QString Weather::GetWind()
{return this->Wind;
}QString Weather::GetFluenza()
{return this->Fever;
}QString Weather::GetWeatherType()
{return this->WeatherType;
}
weather.h:
#ifndef WEATHER_H
#define WEATHER_H#include<QObject>
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include<QNetworkRequest>
#include<QJsonDocument>
#include<QJsonArray>
#include<QJsonObject>class Weather : public QObject
{Q_OBJECT
public:explicit Weather(QObject *parent = nullptr);explicit Weather(QString cityName = "");public:void refresh();void setCityName(QString city);QString GetCityName();  //获取城市QString GetDate();    //获取时间QString GetTemperature();  //获取当前温度QString GetTemperatureMax();  //获取温度范围QString GetWind();   //获取风向QString GetWeatherType();  //获取天气类型QString GetFluenza();   //获取感冒提醒QString getAllInfo(); //获取原始的所有字段void queryWeather();QMap<QString, QMap<QString, QString> > getDataMap(bool *ok=nullptr);    //获取昨天以及未来5天的天气预测private:QNetworkAccessManager *manager;QString allInfo;QString CityName;QString currentDate;QString currentTemper;QString TemperMax;QString Wind;QString WeatherType;QString Fever;bool isGetData=false;//是否成功获取数据QMap<QString,QMap<QString,QString>> dataMap;public slots:void replyFinished(QNetworkReply *reply);//刷新的槽signals:void getDataFinisedSignal();//获取数据结束的信号void getDataSuccessedSignal();//获取数据成功的信号void getDataFailedSignal();//获取数据失败的信号
};#endif // WEATHER_H
源码:
https://github.com/Ytlskys/Qt-WeatherForecast

http://www.ppmy.cn/news/201756.html

相关文章

i7 10700KF怎么样 i7 10700kf配什么主板好

i7-10700KF 拥有 8 核 16线程&#xff0c;基础频率为 4.00GHz&#xff0c;TVB频率可达5.3/4.91 GHz&#xff0c;i7-10700KF 单核可以睿频到了 5.1GHz。i7-10700KF 还搭载了 8256kB的二级缓存和 16MB 的三级缓存 i7 10700KF配什么主板好这些点很重要!看完你就知道了 https://lis…

ESP8266+0.96寸OLED+心知天气制作天气时钟

天气时钟的制作 一.准备代码演示效果 一.准备 我的天气时钟的制作是基于太极创客制作的<ESP8266_Seniverse.h>和网上天气时钟项目进行开发。 Nodemcu开发板 中景园0.96寸OLED(七线制SPI) GND - GND VCC - 3.3V D0 - D2 D1 - D1 RES - D7 DC - D5 CS - D6面包板 杜…

天气预报模块

目录 步骤1. 选择 API2. 发送网络请求3. 接收网络响应获得的json并进行解析4. 天气图标的显示 QT 界面设计 步骤 1. 选择 API 我用的是 聚合数据 上的 “天气预报” https://www.juhe.cn/docs/api/id/73 选择它的原因主要是&#xff1a;免费、数据简洁明了 2. 发送网络请求…

Python天气预报查询

功能&#xff1a; 实现城市天气预报查询&#xff0c;并提供未来四天查询 两个网站接口&#xff0c;直接返回数据&#xff0c;之后对数据进行读取和处理&#xff0c;很简单的小程序 效果图&#xff1a; 代码实现&#xff1a; import urllib.request import gzip import json…

酷睿i7 12800hx和i7 12800H区别

i7 12800H采用 14 核心 20 线程&#xff0c;应该是由 6 大核心 8 小核心组成&#xff0c;主频 2.8GHz&#xff0c;24MB 三级缓存&#xff0c;在 GeekBench 5 中单核心 1654 分&#xff0c;多核心 9618 分 笔记本cpu选i7 12800H还是i7 12800hx这些点很重要 http://www.adiannao.…

Qt 天气

写了一个简单的显示天气信息的例子。 其中涉及到http&#xff0c; xml&#xff0c; c11 天气信息是用webxml接口获取的&#xff0c;包括图片素材。 通过getSupportProvince获取支持的省份信息&#xff0c;getSupportCity获取当前省份中的城市&#xff0c;getWeatherbyCityNam…

网易(weather)天气预报接口

请求链接&#xff1a; http://weather.mail.163.com/weather/xhr/weather/info.do?sid&uid&host&verjs6&fontfaceyahei&style1&skinseablue&color&city101100410&callback00 参数&#xff1a; city 城市编号 返回结果&#xff1a; {&qu…

【空间天气】空间天气预报

文章目录 1.预报要素与时效2.太阳活动预报3.行星际天气预报4.磁层天气预报5.电离层天气预报6.中高层大气天气预报7.预报方法和预报检验8.空间天气机构和预报能力9.目前的主要预报业务模式9.目前的主要预报业务模式 1.预报要素与时效 空间天气预报水平主要取决于空间天气监测能力…