一. 最近在准备毕业设计的材料,先在设计上扩展一些小功能,天气这方式必然是一个很好的选择,跟自己的专业相关,合情合理,废话不多说上代码:
第一步:
对界面的设计很重要:
在设计方面我就这水平,哈哈,没有设计的细胞。
第二步不嘚直接上代码:
//在.pro上一定添加:
QT += core gui network//为什么要添加,我相信大家都知道
widget.h头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QString>
#include <QLabel>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonParseError>
#include <QTime>
#include <QTimer>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_comboBox_activated(const QString &arg1);//combox点击事件
void doProcessfinished(QNetworkReply*);//Http获取数据
void doPressTimeout();
//文本控件的初始化
private:
Ui::Widget *ui;
void Init();
void req_Init();
void set_TypePic(QString type,int i);//对应天气图标
void set_BlackDraw(QString type);
QLabel *week[5];//星期
QLabel *wind[5];//风向
QLabel *api[5];//七气体
QLabel *wendu[5];//温度
QLabel *wea[5];//天气实况
QLabel *date[5];//日期
QLabel *pic[5];//天气对应图片
QTimer *time;
QNetworkAccessManager *manager;
QNetworkRequest request;
QString str;
};
#endif // WIDGET_H
**关键代码部分在这里:
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include <QDateTime>
#include <QByteArray>
#include <QDebug>
#include "QImage"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Init();
this->setFixedSize(this->width(),this->height());//固定窗口大小
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init()
{
//获取COMbox中选择的值
QString addr = ui->comboBox->currentText();
//将值赋值给addr的文本
ui->addr->setText(addr);
//星期
week[0] = ui->week1; week[1] = ui->week2;
week[2] = ui->week3; week[3] = ui->week4; week[4] = ui->week5;
//天气
wea[0] = ui->wea1;wea[1] = ui->wea2;
wea[2] = ui->wea3;wea[3] = ui->wea4;wea[4] = ui->wea5;
//温度的存储
wendu[0] = ui->wendu1; wendu[1] = ui->wendu2;
wendu[2] = ui->wendu3; wendu[3] = ui->wendu4;wendu[4] = ui->wendu5;
//
api[0] = ui->api1;api[1] = ui->api2;
api[2] = ui->api3;api[3] = ui->api4;api[4] = ui->api5;
//风向
wind[0] = ui->wind1;wind[1] = ui->wind2;
wind[2] = ui->wind3;wind[3] = ui->wind4;wind[4] = ui->wind5;
//日期
date[1] = ui->date1;date[2] = ui->date2;
date[3] = ui->date3;date[4] = ui->date4;
//图片
pic[0] = ui->pic1;pic[1] = ui->pic2;
pic[2] = ui->pic3;pic[3] = ui->pic4;pic[4] = ui->pic5;
req_Init();
time = new QTimer();
time->start(1000);
connect(time,SIGNAL(timeout()),this,SLOT(doPressTimeout()));
}
void Widget::req_Init()
{
str = "http://wthrcdn.etouch.cn/weather_mini?city=厦门";
QUrl url(str);
manager = new QNetworkAccessManager(this);
request.setUrl(url);
manager->get(request);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(doProcessfinished(QNetworkReply*)));
}
void Widget::doProcessfinished(QNetworkReply *reply)
{
QByteArray arry = reply->readAll();
//qDebug()<<arry;
QJsonDocument doc = QJsonDocument::fromJson(arry);
if(!doc.isNull())
{
QJsonObject obj = doc.object();//将获取的数据转换为Json格式
if(obj.contains("data"))//判断是否有包含“data”的对象
{
QJsonValue val = obj.value("data");//获取“data”对象的
if(val.isObject())
{
QJsonObject jsonDate = val.toObject();
QString wendu_avg = jsonDate.value("wendu").toString();
ui->label_2->setText(wendu_avg);
QJsonValue fore = jsonDate.value("forecast");
if(fore.isArray())
{
QJsonArray Jsonarry = fore.toArray();
for(int i = 0;i<5;i++)
{
QJsonObject weaobj = Jsonarry.at(i).toObject();//遍历获取
QString high = weaobj.value("high").toString().mid(3);//最高温度
QString low = weaobj.value("low").toString().mid(3,2);//最低温度
QString sWeek = weaobj.value("date").toString().mid(2);//获取星期
QString sDate = weaobj.value("date").toString().mid(0,2);//日期
QString sWind = weaobj.value("fengxiang").toString();//风向
QString sfengli = weaobj.value("fengli").toString().mid(8,9);
QString sWind_Fingli = sWind+sfengli;
QString type = weaobj.value("type").toString();//获取天气实况
if(i == 0){
ui->label_4->setText(type+"(实时)");
set_BlackDraw(type);
}
if(sDate.toInt()>=10)//当日期大于等于10时
{
sDate = weaobj.value("date").toString().mid(0,3);//日期
sWeek = weaobj.value("date").toString().mid(3);//获取星期
}
if(i>0){date[i]->setText(sDate);}
QString wendu_fan = low+"~"+high;
wendu[i]->setText(wendu_fan);//获取温度的范围
week[i]->setText(sWeek);
wind[i]->setText(sWind_Fingli);
wea[i]->setText(type);
set_TypePic(type,i);
}
}
}
}
}
}
void Widget::set_TypePic(QString type,int i)
{
QString Path;
if(type =="小雨"){Path = ":/res/小雨.png";}
if(type =="大雨"||type == "暴雨"){Path = ":/res/大雨.png";}
if(type == "晴"){Path = ":/res/晴.png";}
if(type == "多云"){Path = ":/res/多云.png";}
if(type == "晴转多云"){Path = ":/res/晴转多云.png";}
if(type == "阵雨"|| type == "雷阵雨"){Path = ":/res/阵雨.png";}
if(type == "阴"){Path = ":/res/阴天.png";}
if(type == "中雨"){Path = ":/res/中雨.png";}
pic[i]->setPixmap(QPixmap(Path));
}
void Widget::set_BlackDraw(QString type)
{
QString Path;
if(type == "大雨" || type == "中雨" ){Path = ":res/big_rain.webp";}
else if(type=="暴雨" ){Path = ":res/rain.webp";}
else if(type == "小雨"){Path = ":res/small_rain.jpg";}
else if(type == "阴"){Path = ":res/yin.jpg";}
else if(type == "多云"){Path = ":res/wallpaper2.jpg";}
else if(type == "晴") {Path = ":res/sun.jpg";}
else if(type == "雷阵雨" || type == "阵雨") {Path = ":res/light_rain1.jpg";}
//设置背景图片
this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
QPalette palette = this->palette();
palette.setBrush(QPalette::Window,
QBrush(QPixmap(Path).scaled(// 缩放背景图.
this->size(),
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation)));// 使用平滑的缩放方式
this->setPalette(palette);// 给widget加上背景图
}
void Widget::on_comboBox_activated(const QString &arg1)
{
//获取COMbox中选择的值
QString addr = ui->comboBox->currentText();
ui->addr->setText(addr);
//将值赋值给addr的文本
if(addr == "莆田"){str = "http://wthrcdn.etouch.cn/weather_mini?city=莆田";}
if(addr == "厦门"){str = "http://wthrcdn.etouch.cn/weather_mini?city=厦门";}
if(addr == "福州"){str = "http://wthrcdn.etouch.cn/weather_mini?city=福州";}
if(addr == "杭州"){str = "http://wthrcdn.etouch.cn/weather_mini?city=杭州";}
if(addr == "上海"){str = "http://wthrcdn.etouch.cn/weather_mini?city=上海";}
if(addr == "南京"){str = "http://wthrcdn.etouch.cn/weather_mini?city=南京";}
if(addr == "海口"){str = "http://wthrcdn.etouch.cn/weather_mini?city=海口";}
if(addr == "宁德"){str = "http://wthrcdn.etouch.cn/weather_mini?city=宁德";}
if(addr == "南平"){str = "http://wthrcdn.etouch.cn/weather_mini?city=南平";}
if(addr == "泉州"){str = "http://wthrcdn.etouch.cn/weather_mini?city=泉州";}
if(addr == "三明"){str = "http://wthrcdn.etouch.cn/weather_mini?city=三明";}
if(addr == "龙岩"){str = "http://wthrcdn.etouch.cn/weather_mini?city=龙岩";}
if(addr == "漳州"){str = "http://wthrcdn.etouch.cn/weather_mini?city=漳州";}
if(addr == "北京"){str = "http://wthrcdn.etouch.cn/weather_mini?city=北京";}
if(addr == "深圳"){str = "http://wthrcdn.etouch.cn/weather_mini?city=深圳";}
if(addr == "乌鲁木齐"){str = "http://wthrcdn.etouch.cn/weather_mini?city=乌鲁木齐";}
if(addr == "郑州"){str = "http://wthrcdn.etouch.cn/weather_mini?city=郑州";}
QUrl url(str);
manager = new QNetworkAccessManager(this);
request.setUrl(url);
manager->get(request);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(doProcessfinished(QNetworkReply*)));
}
void Widget::doPressTimeout()//时间触发事件
{
//获取当前时间
QDateTime DT = QDateTime::currentDateTime();
ui->year->setText(DT.toString("yyyy-MM-dd"));
ui->time->setText(DT.toString("hh:mm:ss"));
}
运行结果如图:
主要参考:https://blog.youkuaiyun.com/qq_42449351/article/details/100088010
注:如果需要源码可以留言!!!(若有不足之处多多指教)