• Qt 天气预报程序解析


    主要包含:

    1.qt网络类和qjson类对象的使用

    2.免费的天气api网址

    3.全国大部分城市天气代码,使用第二种方式用到城市天气代码

    1. #weatherforecast.h 文件内容
    2. #ifndef WEATHERFORECAST_H
    3. #define WEATHERFORECAST_H
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. namespace Ui {
    16. class weatherforecast;
    17. }
    18. class weatherforecast : public QWidget
    19. {
    20. Q_OBJECT
    21. public:
    22. explicit weatherforecast(QWidget *parent = nullptr);
    23. ~weatherforecast();
    24. private:
    25. Ui::weatherforecast *ui;
    26. QNetworkAccessManager*manger; //
    27. QNetworkRequest request;
    28. QByteArray all;
    29. };
    30. #endif // WEATHERFORECAST_H
    31. #weatherforecast.cpp 文件内容
    32. #include "weatherforecast.h"
    33. #include "ui_weatherforecast.h"
    34. weatherforecast::weatherforecast(QWidget *parent) :
    35. QWidget(parent),
    36. ui(new Ui::weatherforecast)
    37. {
    38. ui->setupUi(this);
    39. manger=new QNetworkAccessManager(this);
    40. connect(manger,&QNetworkAccessManager::finished,[this](QNetworkReply * reply){
    41. all = reply->readAll();
    42. QString arr_str(all);
    43. qDebug()<
    44. QJsonParseError error;
    45. QJsonDocument jsonDoc=QJsonDocument::fromJson(arr_str.toUtf8(),&error);
    46. if(error.error != QJsonParseError::NoError)
    47. {
    48. }
    49. QJsonObject jsonobject=jsonDoc.object();
    50. if(jsonobject.contains("data")) // 是否包含data
    51. {
    52. QJsonValue data_value=jsonobject.value("data");
    53. if(data_value.isObject())
    54. {
    55. QJsonObject object_data =data_value.toObject();
    56. QJsonValue yesterday_value=object_data.value("yesterday");
    57. if(yesterday_value.isObject())
    58. {
    59. QJsonObject yesterday_data =yesterday_value.toObject();
    60. qDebug()<< yesterday_data.value("date").toString();
    61. qDebug()<< yesterday_data.value("high").toString();
    62. qDebug()<< yesterday_data.value("fx").toString();
    63. qDebug()<< yesterday_data.value("low").toString();
    64. }
    65. }
    66. }
    67. });
    68. //天气接口api1 城市名称获取天气信息
    69. // QString str1("http://wthrcdn.etouch.cn/weather_mini?city=深圳");
    70. //this->manger->get(QNetworkRequest(QUrl(str1)));
    71. //天气接口api2 城市代码获取天气信息
    72. //全国大部分城市天气代码网址:https://www.cnblogs.com/emo-Studio/p/6840534.html
    73. QString str2("http://t.weather.sojson.com/api/weather/city/CHXX0116");
    74. this->manger->get(QNetworkRequest(QUrl(str2)));
    75. }
    76. weatherforecast::~weatherforecast()
    77. {
    78. delete ui;
    79. }

  • 相关阅读:
    1076 Forwards on Weibo
    spring boot and php
    TCP连接的关键之谜:揭秘三次握手的必要性
    数据分析 - matplotlib示例代码
    [OpenCv]初识——图像的基本处理
    【2023最新版】DataGrip安装及使用教程
    maven的生命周期
    C语言 指针
    【毕设专用开发模板】前后端分离,基于 Vue 和 SpringBoot 的通用管理系统
    gin 配置文件
  • 原文地址:https://blog.csdn.net/jiletianzun/article/details/126020351