天气预报模块
目錄
- 步驟
- 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é)
- 上一篇: 【漫漫科研路\pgfplots】画局部放
- 下一篇: 计算机内外存储器论文,计算机组成原理探讨