vs版本:2017
qt版本:qt5
为了实现自己的设计,今天试一试利用接口来获取网络数据,搜了搜资料发现,网上的天气数据较多,采用这个。
首先是利用vs2017创建一个qt的项目,我用的wiget,创建好之后,在.pro 文件中添加语句
QT += network
可是这时问题就来了,.pro文件只有qtcreator才会生成,vs是没有的,这时我们需要在项目中生存.pro 文件。
这样就能找到了,添加上述语句;
之后就是添加代码:
首先是在头文件中添加网页需要的头文件:
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
然后是添加需要的读取JSON文件的头文件:
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
头文件代码:
#pragma once
#include <QtWidgets/QWidget>
#include "ui_widget.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
class widget : public QWidget
{
Q_OBJECT
public:
widget(QWidget *parent = 0);
private slots:
void checkW();
void replyFinished(QNetworkReply *reply);
private:
Ui::widgetClass *ui;
QNetworkAccessManager *manager; //请求句柄
QString fengli; //风力
QString wendu; //温度
QString weather_type; //天气类型
};
源文件:
#include "widget.h"
widget::widget(QWidget *parent)
: QWidget(parent),
ui(new Ui::widgetClass)
{
ui->setupUi(this);
manager = new QNetworkAccessManager(this); //新建QNetworkAccessManager对象
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));//关联信号和槽
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(checkW()));//关联信号和槽
}
void widget::checkW() //点击查询请求天气数据
{
QString local_city = ui->lineEdit->text().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);
}
void widget::replyFinished(QNetworkReply *reply) //天气数据处理槽函数
{
qDebug() << "recv weather data!!";
QString all = reply->readAll();
//ui->textBrowser->setText(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 tuijian = object.value("data").toObject().value("ganmao").toString();
QString low = today_weather.value("low").toString();
QString high = today_weather.value("high").toString();
wendu = 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);
fengli = today_weather.value("fengxiang").toString() + strength;
ui->type->setText(weather_type); //显示天气类型
ui->wendu->setText(wendu); //显示温度
ui->fengli->setText(fengli); //显示风力
ui->textBrowser2->setText(tuijian);
}
}
}
}
}
else
{
qDebug() << "json_recv is NULL or is not a object !!";
}
reply->deleteLater(); //销毁请求对象
}
ui设计:
前四个是lineEdit,推荐是textBrowser,按照代码中修改名字。
效果:
完成。
参考:基于QT5 HTTP GET方式获取天气信息
Qt5获取并解析中国天气网数据