日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

天气预报模块

發(fā)布時間:2024/3/24 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天气预报模块 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 步驟
    • 1. 選擇 API
    • 2. 發(fā)送網(wǎng)絡(luò)請求
    • 3. 接收網(wǎng)絡(luò)響應(yīng)獲得的json并進(jìn)行解析
    • 4. 天氣圖標(biāo)的顯示
  • QT 界面設(shè)計

步驟

1. 選擇 API

我用的是 聚合數(shù)據(jù) 上的 “天氣預(yù)報” https://www.juhe.cn/docs/api/id/73

選擇它的原因主要是:免費數(shù)據(jù)簡潔明了

2. 發(fā)送網(wǎng)絡(luò)請求

manager = new QNetworkAccessManager(this); QObject::connect(manager, &QNetworkAccessManager::finished, this, &WeatherWindow::replyFinished);//往天氣預(yù)報 API 發(fā)出請求 manager->get(QNetworkRequest(QUrl("http://apis.juhe.cn/simpleWeather/query?city=廣州&key=118d24f3f0c7b522a6142d9a0a4f01c9")));

注意:發(fā)送網(wǎng)絡(luò)請求的網(wǎng)絡(luò)地址開頭必須是 http , 不能是 https , 否則無法獲取網(wǎng)絡(luò)響應(yīng)

  • http:超文本傳輸協(xié)議,以明文方式發(fā)送內(nèi)容,不提供任何方式的數(shù)據(jù)加密
  • https:在HTTP的基礎(chǔ)上加入了SSL協(xié)議,SSL依靠證書來驗證服務(wù)器的身份,并為瀏覽器和服務(wù)器之間的通信加密
  • 具體關(guān)于 http 和 https 的介紹可參考博客 https://www.cnblogs.com/wqhwe/p/5407468.html

3. 接收網(wǎng)絡(luò)響應(yīng)獲得的json并進(jìn)行解析

//處理收到的 json 數(shù)據(jù) void WeatherWindow::replyFinished(QNetworkReply *reply) {QString json_msg = reply->readAll();qDebug() << json_msg;//定義一個變量用于記錄位置int position;weather_tmp = json_msg.split('"');//設(shè)置當(dāng)天的地點、天氣、溫度ui->label_loca_city->setText(weather_tmp[9].toUtf8());ui->label_local_temperature->setText(weather_tmp[15].toUtf8());//將保存在本文件中的天氣圖標(biāo)用鏈接顯示出來ui->label_local_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[23])));//減小 json_msg 的長度, weather_tmp 能存儲的內(nèi)容有限position = json_msg.indexOf("date");json_msg = json_msg.remove(0,position);weather_tmp = json_msg.split('"');ui->label1_date->setText(weather_tmp[2].toUtf8());weather_tmp[6].replace(QString("\\/"),QString("—"));ui->label1_temperature->setText(weather_tmp[6]);ui->label1_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));position = json_msg.indexOf("date",4);json_msg = json_msg.remove(0,position);weather_tmp = json_msg.split('"');ui->label2_date->setText(weather_tmp[2].toUtf8());weather_tmp[6].replace(QString("\\/"),QString("—"));ui->label2_temperature->setText(weather_tmp[6]);ui->label2_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));position = json_msg.indexOf("date",4);json_msg = json_msg.remove(0,position);weather_tmp = json_msg.split('"');ui->label3_date->setText(weather_tmp[2].toUtf8());weather_tmp[6].replace(QString("\\/"),QString("—"));ui->label3_temperature->setText(weather_tmp[6]);ui->label3_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));position = json_msg.indexOf("date",4);json_msg = json_msg.remove(0,position);weather_tmp = json_msg.split('"');ui->label4_date->setText(weather_tmp[2].toUtf8());weather_tmp[6].replace(QString("\\/"),QString("—"));ui->label4_temperature->setText(weather_tmp[6]);ui->label4_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));position = json_msg.indexOf("date",4);json_msg = json_msg.remove(0,position);weather_tmp = json_msg.split('"');ui->label5_date->setText(weather_tmp[2].toUtf8());weather_tmp[6].replace(QString("\\/"),QString("—"));ui->label5_temperature->setText(weather_tmp[6]);ui->label5_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10]))); }

4. 天氣圖標(biāo)的顯示

//返回天氣圖標(biāo)的路徑 QString WeatherWindow::get_weather_pic(QString name) {if(name == "中雨") return QString("heavy_rain.png");else if(name == "雷陣雨") return QString("rain_thunder.png");else if(name == "中雨轉(zhuǎn)雷陣雨") return QString("rain_thunder.png");else if(name == "多云") return QString("cloudy.png");else if(name == "晴") return QString("sunny.png");else return QString("unknown.png"); }

QT 界面設(shè)計

總結(jié)

以上是生活随笔為你收集整理的天气预报模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。