I. Introduction
- Qt usually obtains weather information by calling the interface of the weather server, and the interface can be searched online by itself.
- The interface for obtaining weather information in this article is: http://wthrcdn.etouch.cn/weather_mini, and the request parameter name is city (to obtain weather information of a city). The request method is get request.
2. Realize the effect
Select a city from the drop-down box to get weather information
3. Implementation steps
1. Add network class
Add a network class that uses the weather interface to the Qt pro file to request data through http.
QT += network
2. Add related header files
//添加网络相关头文件
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
//添加JSON解析相关的头文件,因为从该服务器请求的天气数据是以json的形式回复的
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
3. Create a network request object, connect signals and slots
After defining the relevant variables, connect the signal and slot in the constructor
manager = new QNetworkAccessManager(this); //新建QNetworkAccessManager对象
//当收到http请求回复的数据,便会调用replyDone槽函数。
//关联信号和槽
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyDone(QNetworkReply*)));
4. Request and get weather related data
void myLockWindow::btn_getdata_clicked(const QString &)
{
//获得需要查询天气的城市名称
QString local_city = QString(city_combx->currentText()); .trimmed()
char quest_array[256]="http://wthrcdn.etouch.cn/weather_mini?city=";
QNetworkRequest quest;
sprintf(quest_array,"%s%s",quest_array,local_city.toUtf8().data());
quest.setUrl(QUrl(quest_array));
quest.setHeader(QNetworkRequest::UserAgentHeader,"RT-Thread ART");
//发送get网络请求
manager->get(quest);
}
5. Processing reply data - slot function
void myLockWindow::replyDone(QNetworkReply *reply)
{
QString all = reply->readAll();
// ui->textEdit->setText(all); //将接收到的数据显示出来
qDebug()<<"recv weather data!!"<<all;
QJsonParseError err;
QJsonDocument json_recv = QJsonDocument::fromJson(all.toUtf8(),&err);//解析json对象
qDebug() << err.error;
if(!json_recv.isNull())
{
QJsonObject object = json_recv.object();
if(object.contains("data"))
{
QJsonValue value = object.value("data"); // 获取指定 key 对应的 value
if(value.isObject())
{
QJsonObject object_data = value.toObject();
if(object_data.contains("forecast"))
{
QJsonValue value = object_data.value("forecast");
if(value.isArray())
{
QJsonObject today_weather = value.toArray().at(0).toObject();
weather_type = today_weather.value("type").toString();
QString low = today_weather.value("low").toString();
QString high = today_weather.value("high").toString();
// QString ganmao = today_weather.value("ganmao").toString();//感冒提示
temperature= low.mid(low.length()-3,4) +"~"+ high.mid(high.length()-3,4);
QString strength = today_weather.value("fengli").toString();
strength.remove(0,8);
strength.remove(strength.length()-2,2);
wind_power= today_weather.value("fengxiang").toString() + strength;
this->lab_type->setText(weather_type); //显示天气类型
this->lab_temperature->setText(temperature); //显示温度
this->lab_wind_power->setText(wind_power); //显示风力
// this->lab_ganmao->setText(ganmao);
}
}
}
}
}
else
{
qDebug()<<"请求数据失败";
}
reply->deleteLater(); //销毁请求对象
}