Qt天气预报系统获取天气数据 1、获取天气数据 1.1添加天气类头文件 1.2定义今天和未来几天天气数据类 1.3定义一个解析JSON数据的函数 1.4在mainwindow中添加weatherData.h 1.5创建今天天气数据和未来几天天气数据对象 1.6添加parseJson定义 1.7把解析JSON数据添加进去 1.8添加错误 1.9解析整个天气数据 2、.h文件和.cpp文件 2.1 mainwindow.h文件 2.2weatherData.h文件 2.3 .cpp文件
1、获取天气数据
1.1添加天气类头文件
1.2定义今天和未来几天天气数据类
class Today {
public : Today ( ) { city = "广州" ; date = "2025-1-10" ; weatherType = "多云" ; highTem = 30 ; lowTem = 18 ; ganMao = "感冒指数" ; Temperature = 0 ; Humidity = "0%" ; PM25 = 0 ; windType = "南风" ; windPower = "2级" ; airQuality = "无数据" ; } QString city; QString date; int Temperature; QString weatherType; int highTem; int lowTem; QString ganMao; QString Humidity; int PM25; QString windType; QString windPower; QString airQuality; } ;
class Day {
public : Day ( ) { week = "周五" ; date = "2023-7-29" ; weatherType = "多云" ; airQuality = 0 ; highTem = 0 ; lowTem = 0 ; windType = "南风" ; windPower = "2级" ; } QString week; QString date; QString weatherType; int airQuality; int highTem; int lowTem; QString windType; QString windPower;
} ;
1.3定义一个解析JSON数据的函数
void parseJson ( QByteArray& byteArray) ;
1.4在mainwindow中添加weatherData.h
# include "weatherData.h"
1.5创建今天天气数据和未来几天天气数据对象
Today today;
Day day[ 7 ] ;
1.6添加parseJson定义
1.7把解析JSON数据添加进去
void MainWindow :: readHttpReply ( QNetworkReply * reply)
{ int status_code = reply-> attribute ( QNetworkRequest:: HttpStatusCodeAttribute) . toInt ( ) ; if ( reply-> error ( ) != QNetworkReply:: NoError || status_code != 200 ) { qDebug ( ) << reply-> errorString ( ) . toLatin1 ( ) . data ( ) ; QMessageBox :: warning ( this , "天气" , "请求数据失败" , QMessageBox:: Ok) ; } else { QByteArray byteArray = reply-> readAll ( ) ; qDebug ( ) << "read all:" << byteArray. data ( ) ; parseJson ( byteArray) ; } reply-> deleteLater ( ) ;
}
1.8添加错误
void MainWindow :: parseJson ( QByteArray & byteArray)
{ QJsonParseError err;
}
鼠标放到QJsonParseError 上, ALT+Enter 选择添加头文件,后面的也类似,使用一个 新类的时候,记得添加头文件
1.9解析整个天气数据
void MainWindow :: parseJson ( QByteArray & byteArray)
{ QJsonParseError err; QJsonDocument doc = QJsonDocument :: fromJson ( byteArray, & err) ; if ( err. error != QJsonParseError:: NoError) { return ; } QJsonObject rootObj = doc. object ( ) ; today. city = rootObj. value ( "cityInfo" ) . toObject ( ) . value ( "city" ) . toString ( ) ; today. date = rootObj. value ( "date" ) . toString ( ) ; QJsonObject objData = rootObj. value ( "data" ) . toObject ( ) ; QJsonObject objYesterday = objData. value ( "yesterday" ) . toObject ( ) ; day[ 0 ] . week = objYesterday. value ( "week" ) . toString ( ) ; day[ 0 ] . date = objYesterday. value ( "date" ) . toString ( ) ; day[ 0 ] . weatherType = objYesterday. value ( "type" ) . toString ( ) ; QString highT; highT = objYesterday. value ( "high" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ 0 ] . highTem = highT. left ( highT. length ( ) - 1 ) . toInt ( ) ; QString lowT; lowT = objYesterday. value ( "low" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ 0 ] . lowTem = lowT. left ( lowT. length ( ) - 1 ) . toInt ( ) ; day[ 0 ] . windType = objYesterday. value ( "fx" ) . toString ( ) ; day[ 0 ] . windPower = objYesterday. value ( "fl" ) . toString ( ) ; day[ 0 ] . airQuality = objYesterday. value ( "aqi" ) . toDouble ( ) ; QJsonArray forecastArray = objData. value ( "forecast" ) . toArray ( ) ; for ( int i= 0 ; i< 6 ; i++ ) { QJsonObject forecastObj = forecastArray[ i] . toObject ( ) ; day[ i+ 1 ] . week = forecastObj. value ( "week" ) . toString ( ) ; day[ i+ 1 ] . date = forecastObj. value ( "ymd" ) . toString ( ) ; day[ i+ 1 ] . weatherType = forecastObj. value ( "type" ) . toString ( ) ; day[ i+ 1 ] . airQuality = forecastObj. value ( "aqi" ) . toDouble ( ) ; QString highT; highT = forecastObj. value ( "high" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ i+ 1 ] . highTem = highT. left ( highT. length ( ) - 1 ) . toInt ( ) ; QString lowS; lowS = forecastObj. value ( "low" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ i+ 1 ] . lowTem = lowS. left ( lowS. length ( ) - 1 ) . toInt ( ) ; day[ i+ 1 ] . windType = forecastObj. value ( "fx" ) . toString ( ) ; day[ i+ 1 ] . windPower = forecastObj. value ( "fl" ) . toString ( ) ; today. Temperature = objData. value ( "wendu" ) . toInt ( ) ; today. ganMao = objData. value ( "ganmao" ) . toString ( ) ; today. Humidity = objData. value ( "shidu" ) . toString ( ) ; today. PM25 = objData. value ( "pm25" ) . toDouble ( ) ; today. airQuality = objData. value ( "quality" ) . toString ( ) ; today. weatherType = day[ 1 ] . weatherType; today. highTem = day[ 1 ] . highTem; today. lowTem = day[ 1 ] . lowTem; today. windType = day[ 1 ] . windType; today. windPower = day[ 1 ] . windPower; }
}
2、.h文件和.cpp文件
2.1 mainwindow.h文件
# ifndef MAINWINDOW_H
# define MAINWINDOW_H # include <QMainWindow>
# include <QMenu>
# include <QMessageBox>
# include <QNetworkAccessManager>
# include <QNetworkReply>
# include "weatherData.h" QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow ; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{ Q_OBJECTpublic : MainWindow ( QWidget * parent = nullptr ) ; ~ MainWindow ( ) ; protected : void mousePressEvent ( QMouseEvent * event) override ; void mouseMoveEvent ( QMouseEvent * event) override ; void getWeatherInfo ( QString citycode) ; void parseJson ( QByteArray& byteArray) ; private slots: void on_pushButton_quit_clicked ( ) ; void onQuitActTriggered ( ) ; public slots: void readHttpReply ( QNetworkReply * reply) ; private : Ui:: MainWindow * ui; QMenu * quitMenu; QAction * quitAct; QPoint moffset; QNetworkAccessManager * NetAccessManger; bool closePOPWindow ( ) ; Today today; Day day[ 7 ] ;
} ;
# endif
2.2weatherData.h文件
# ifndef WEATHERDATA_H
# define WEATHERDATA_H
# include <QString>
class Today {
public : Today ( ) { city = "广州" ; date = "2025-1-10" ; weatherType = "多云" ; highTem = 30 ; lowTem = 18 ; ganMao = "感冒指数" ; Temperature = 0 ; Humidity = "0%" ; PM25 = 0 ; windType = "南风" ; windPower = "2级" ; airQuality = "无数据" ; } QString city; QString date; int Temperature; QString weatherType; int highTem; int lowTem; QString ganMao; QString Humidity; int PM25; QString windType; QString windPower; QString airQuality; } ;
class Day {
public : Day ( ) { week = "周五" ; date = "2023-7-29" ; weatherType = "多云" ; airQuality = 0 ; highTem = 0 ; lowTem = 0 ; windType = "南风" ; windPower = "2级" ; } QString week; QString date; QString weatherType; int airQuality; int highTem; int lowTem; QString windType; QString windPower;
} ;
# endif
2.3 .cpp文件
# include "mainwindow.h"
# include "ui_mainwindow.h" # include <QJsonArray>
# include <QJsonObject>
# include <QJsonParseError>
# include <QMouseEvent>
# include "weatherData.h" MainWindow :: MainWindow ( QWidget * parent) : QMainWindow ( parent) , ui ( new Ui:: MainWindow)
{ ui-> setupUi ( this ) ; setWindowFlag ( Qt:: FramelessWindowHint) ; quitMenu = new QMenu ( this ) ; quitAct = new QAction ( QIcon ( ":/widget/quit.png" ) , tr ( "退出" ) , this ) ; quitMenu-> addAction ( quitAct) ; connect ( quitMenu, & QMenu:: triggered, this , & MainWindow:: onQuitActTriggered) ; NetAccessManger = new QNetworkAccessManager ( this ) ; connect ( NetAccessManger, & QNetworkAccessManager:: finished, this , & MainWindow:: readHttpReply) ; getWeatherInfo ( "101010100" ) ; } MainWindow :: ~ MainWindow ( )
{ delete ui;
}
void MainWindow :: mousePressEvent ( QMouseEvent * event)
{ if ( event-> button ( ) == Qt:: RightButton) { quitMenu-> exec ( QCursor :: pos ( ) ) ; } if ( event-> button ( ) == Qt:: LeftButton) { moffset = event-> globalPos ( ) - this -> pos ( ) ; }
}
void MainWindow :: mouseMoveEvent ( QMouseEvent * event)
{ this -> move ( event-> globalPos ( ) - moffset) ;
}
bool MainWindow :: closePOPWindow ( )
{ QMessageBox mes; mes. setWindowTitle ( "关闭窗口" ) ; mes. setIcon ( QMessageBox:: Warning) ; mes. setText ( "是否关闭窗口?" ) ; mes. setStandardButtons ( QMessageBox:: Ok | QMessageBox:: Cancel) ; mes. setWindowIcon ( QIcon ( ":/widget/dog.png" ) ) ; mes. setButtonText ( QMessageBox:: Ok, tr ( "确定" ) ) ; mes. setButtonText ( QMessageBox:: Cancel, tr ( "取消" ) ) ; int result = mes. exec ( ) ; if ( result == QMessageBox:: Ok) { this -> close ( ) ; return true ; } else { return false ; }
}
void MainWindow :: on_pushButton_quit_clicked ( )
{ closePOPWindow ( ) ;
}
void MainWindow :: onQuitActTriggered ( )
{ closePOPWindow ( ) ;
}
void MainWindow :: getWeatherInfo ( QString citycode)
{ QUrl url ( "http://t.weather.itboy.net/api/weather/city/" + citycode) ; NetAccessManger-> get ( QNetworkRequest ( url) ) ; }
void MainWindow :: parseJson ( QByteArray & byteArray)
{ QJsonParseError err; QJsonDocument doc = QJsonDocument :: fromJson ( byteArray, & err) ; if ( err. error != QJsonParseError:: NoError) { return ; } QJsonObject rootObj = doc. object ( ) ; today. city = rootObj. value ( "cityInfo" ) . toObject ( ) . value ( "city" ) . toString ( ) ; today. date = rootObj. value ( "date" ) . toString ( ) ; QJsonObject objData = rootObj. value ( "data" ) . toObject ( ) ; QJsonObject objYesterday = objData. value ( "yesterday" ) . toObject ( ) ; day[ 0 ] . week = objYesterday. value ( "week" ) . toString ( ) ; day[ 0 ] . date = objYesterday. value ( "date" ) . toString ( ) ; day[ 0 ] . weatherType = objYesterday. value ( "type" ) . toString ( ) ; QString highT; highT = objYesterday. value ( "high" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ 0 ] . highTem = highT. left ( highT. length ( ) - 1 ) . toInt ( ) ; QString lowT; lowT = objYesterday. value ( "low" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ 0 ] . lowTem = lowT. left ( lowT. length ( ) - 1 ) . toInt ( ) ; day[ 0 ] . windType = objYesterday. value ( "fx" ) . toString ( ) ; day[ 0 ] . windPower = objYesterday. value ( "fl" ) . toString ( ) ; day[ 0 ] . airQuality = objYesterday. value ( "aqi" ) . toDouble ( ) ; QJsonArray forecastArray = objData. value ( "forecast" ) . toArray ( ) ; for ( int i= 0 ; i< 6 ; i++ ) { QJsonObject forecastObj = forecastArray[ i] . toObject ( ) ; day[ i+ 1 ] . week = forecastObj. value ( "week" ) . toString ( ) ; day[ i+ 1 ] . date = forecastObj. value ( "ymd" ) . toString ( ) ; day[ i+ 1 ] . weatherType = forecastObj. value ( "type" ) . toString ( ) ; day[ i+ 1 ] . airQuality = forecastObj. value ( "aqi" ) . toDouble ( ) ; QString highT; highT = forecastObj. value ( "high" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ i+ 1 ] . highTem = highT. left ( highT. length ( ) - 1 ) . toInt ( ) ; QString lowS; lowS = forecastObj. value ( "low" ) . toString ( ) . split ( " " ) . at ( 1 ) ; day[ i+ 1 ] . lowTem = lowS. left ( lowS. length ( ) - 1 ) . toInt ( ) ; day[ i+ 1 ] . windType = forecastObj. value ( "fx" ) . toString ( ) ; day[ i+ 1 ] . windPower = forecastObj. value ( "fl" ) . toString ( ) ; today. Temperature = objData. value ( "wendu" ) . toInt ( ) ; today. ganMao = objData. value ( "ganmao" ) . toString ( ) ; today. Humidity = objData. value ( "shidu" ) . toString ( ) ; today. PM25 = objData. value ( "pm25" ) . toDouble ( ) ; today. airQuality = objData. value ( "quality" ) . toString ( ) ; today. weatherType = day[ 1 ] . weatherType; today. highTem = day[ 1 ] . highTem; today. lowTem = day[ 1 ] . lowTem; today. windType = day[ 1 ] . windType; today. windPower = day[ 1 ] . windPower; }
}
void MainWindow :: readHttpReply ( QNetworkReply * reply)
{ int status_code = reply-> attribute ( QNetworkRequest:: HttpStatusCodeAttribute) . toInt ( ) ; if ( reply-> error ( ) != QNetworkReply:: NoError || status_code != 200 ) { qDebug ( ) << reply-> errorString ( ) . toLatin1 ( ) . data ( ) ; QMessageBox :: warning ( this , "天气" , "请求数据失败" , QMessageBox:: Ok) ; } else { QByteArray byteArray = reply-> readAll ( ) ; qDebug ( ) << "read all:" << byteArray. data ( ) ; parseJson ( byteArray) ; } reply-> deleteLater ( ) ;
}
获取天气数据完成!