Qt获取某地的天气情况
weather.pro (其中要加入network模块)
#------------------------------------------------- # # Project created by QtCreator 2019-03-30T13:54:31 # #------------------------------------------------- QT += core gui QT += network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = weather TEMPLATE = app SOURCES += main.cpp\ widget.cpp HEADERS += widget.h FORMS += widget.ui
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; QNetworkAccessManager *manager; QString fengli; //风力 QString wendu; //温度 QString weather_type; //天气类型 private slots: void replyFinished(QNetworkReply *); void on_pushButton_clicked(); }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QUrl> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, &Widget::replyFinished); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { QString address = "http://wthrcdn.etouch.cn/weather_mini?city="; QString city = ui->lineEdit->text(); QString total = address + city; const char *url = total.toUtf8().data(); /** qDebug()<<city; char url[256] = "http://wthrcdn.etouch.cn/weather_mini?city="; char *ch = city.toUtf8().data(); sprintf(url,"%s%s",url,ch); **/ manager->get(QNetworkRequest(QUrl(url))); } void Widget::replyFinished(QNetworkReply *reply) { 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 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); //显示风力 } } } } }else { qDebug()<<"json_recv is NULL or is not a object !!"; } reply->deleteLater(); }
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
图形界面: