QT实现天气预报

1. MainWindow类设计的成员变量和方法

public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

   protected:

形成文本菜单来用来右键关闭窗口
    void contextMenuEvent(QContextMenuEvent* event);

鼠标被点击之后此事件被调用

    void mousePressEvent(QMouseEvent *ev);

移动窗口
    void mouseMoveEvent(QMouseEvent* ev);
    //重写过滤器方法
    bool eventFilter(QObject *watched, QEvent *event);

private slots://信号槽函数
    void onReplied(QNetworkReply* reply);

    void on_btnSearch_clicked();

public:

    void getWeaterInfo(QString cityCode);
    //解析json数据
    void parseJson(QByteArray& byteArray);
    int parseString(QString str);
    //更新UI
    void UpdateUi();
    //检测空气质量
   int checkedQulity(int api);
    //绘制高低温曲线
    void paintHighCurve();
   void paintLowCurve();


   private:
    Ui::MainWindow* ui;

    QMenu* mExitMenu;   // 右键退出的菜单
    QAction* mExitAct;  // 退出的行为
    QPoint m_offset;//鼠标和左上角坐标的偏移距离
    QNetworkAccessManager *m_netAccessManager;
    Today m_today;
    Day m_day[7];

    //星期和日期
    QList m_weekList;
    QList m_dateList;

    //天气和天气图标
    QList m_typeList;
    QList m_typeIconList;

    //天气污染指数
    QList m_ApiList;

    //风力和风向
    QList m_FLList;
    QList m_FXList;

把UI控件都放到QList集合里面

//将UI控件放到数组里边,方便使用循环进行处理
    m_dateList << ui->lblDate0 << ui->lblDate1 << ui->lblDate2 << ui->lblDate3 << ui->lblDate4 << ui->lblDate5;
    m_weekList << ui->lblWeek0 << ui->lblWeek1 << ui->lblWeek2 << ui->lblWeek3 << ui->lblWeek4 << ui->lblWeek5;
    m_ApiList << ui->lblQuality0 << ui->lblQuality1 << ui->lblQuality2 << ui->lblQuality3 << ui->lblQuality4 << ui->lblQuality5;
    m_typeList << ui->lblType0 << ui->lblType1 << ui->lblType2 << ui->lblType3 << ui->lblType4 << ui->lblType5;
    m_typeIconList << ui->lblTypeIcon0 << ui->lblTypeIcon1 << ui->lblTypeIcon2 << ui->lblTypeIcon3 << ui->lblTypeIcon4 << ui->lblTypeIcon5;
    m_FXList << ui->lblFx0 << ui->lblFx1 << ui->lblFx2 << ui->lblFx3 << ui->lblFx4 << ui->lblFx5;
    m_FLList << ui->lblFl0 << ui->lblFl1 << ui->lblFl2 << ui->lblFl3 << ui->lblFl4 << ui->lblFl5;

    QMap m_typeMap;

2. 右键退出

void contextMenuEvent(QContextMenuEvent* event);

  mExitMenu->exec(QCursor::pos());
    event->accept();

QMenu* mExitMenu;   右键退出的菜单

 // 右键菜单:退出程序
    mExitMenu = new QMenu(this);//添加一个菜单框
    mExitAct = new QAction();
    mExitAct->setText(tr("退出"));
    mExitAct->setIcon(QIcon(":/res/close.png"));
    mExitMenu->addAction(mExitAct);//把这个行为设置到菜单里面去

    connect(mExitAct, &QAction::triggered, this, [=]() {
        qApp->exit(0); 
    });

3.存放天气信息的类


class Today
{
public:
    Today()
    {
        date = "2022-10-20";
        city = "广州";

        ganmao = "感冒指数";

        wendu = 0;
        shidu = "0%";
        pm25 = 0;
        quality = "无数据";

        type = "多云";

        fl = "2级";
        fx = "南风";

        high = 30;
        low = 18;
    }

    QString date;
    QString city;

    QString ganmao;

    int wendu;
    QString shidu;
    int pm25;
    QString quality;

    QString type;

    QString fx;
    QString fl;

    int high;
    int low;
};

class Day
{
public:
    Day()
    {
        date = "2022-10-20";
        week = "周五";

        type = "多云";

        high = 0;
        low = 0;

        fx = "南风";
        fl = "2级";

        aqi = 0;
    }

    QString date;
    QString week;

    QString type;
    QString ymd;

    int high;
    int low;

    QString fx;
    QString fl;

    int aqi;
};

4.get请求数据

void MainWindow::getWeaterInfo(QString cityName)
{
    QString cityCode=weatherTool::getCityCode(cityName);
    qDebug()<<"cityCode"<get(QNetworkRequest(url));//发送get请求
}

5.重写void onReplied函数

void onReplied(QNetworkReply* reply);在get请求成功之后此函数被调用

void MainWindow::onReplied(QNetworkReply *reply)
{
    qDebug()<<"onReplied success";
    int status_code=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    qDebug()<<"参数: "<operation();
    qDebug()<<"状态码: "<url();
    qDebug()<<"请求头: "<rawHeaderList();
                                   
    if(reply->error()!=QNetworkReply::NoError||status_code!=200){
        qDebug()<errorString().toLatin1().data();
        QMessageBox::warning(this,"天气预报","数据请求失败",QMessageBox::Ok);
    }
    else{
        QByteArray all=reply->readAll();

        qDebug()<<"read all : "<deleteLater();

}

在此函数里把请求到的数据全部读出来

6.读出类之后在调用parseJson(all);来进行解析JSON数据

void MainWindow::parseJson(QByteArray &byteArray)
{
    QJsonParseError err;
    QJsonDocument doc=QJsonDocument::fromJson(byteArray,&err);
    if(err.error!=QJsonParseError::NoError){
        qDebug()<<"解析Json出错了";
        return ;
    }

    //Json对象,包括了请求的全部数据
    QJsonObject rootObj = doc.object();
    //qDebug() << rootObj;
    QJsonObject dataObj = rootObj.value("data").toObject();
    QJsonObject yesterdayObj = dataObj.value("yesterday").toObject();
    //Json数组,未来几天的数据
    QJsonArray forecastArray = dataObj.value("forecast").toArray();
    //1.解析日期和城市
   QString str = rootObj.value("time").toString();
    m_today.date= str.split(" ").at(0);
    qDebug()<<"今天:"<lblHighCurve->update();
    ui->lblLowCurve->update();
}

解析完JSON数据之后在更新ui(UpdateUi();)

7.更新UI

void MainWindow::UpdateUi()
{
    ui->lblDate->setText(QDateTime::fromString(m_today.date,"yyyyMMdd").toString("yyyy/MM/dd")+" "+m_day[1].week);
    ui->lblCity->setText(m_today.city);
    //更新今天数据
    ui->lblTypeIcon->setPixmap(m_typeMap[m_today.type]);//更新图片
    ui->lblTemp->setText(QString ::number(m_today.wendu));
    ui->lblType->setText(m_today.type);
    ui->lblLowHigh->setText(QString ::number(m_today.low)+"℃ ~ "+QString::number(m_today.high)+"℃");

    ui->lblGanMao->setText("感冒指数: "+m_today.ganmao);
    ui->lblWindFl->setText(m_today.fl);
    ui->lblWindFx->setText(m_today.fx);

    ui->lblPM25->setText(QString::number(m_today.pm25));
    ui->lblShiDu->setText(m_today.shidu);
    ui->lblQuality->setText(m_today.quality);
    //更新六天
    for(int i=0;i<6;i++){
        //更新日期和时间
        m_weekList[i]->setText("周"+m_day[i].week.right(1));//从右边取第一个
        ui->lblWeek0->setText("昨天");
         ui->lblWeek1->setText("今天");
         ui->lblWeek2->setText("明天");

//         //把日期提取出来变成 10/10格式
         QStringList slist=m_day[i].ymd.split("-");
         qDebug()<setText(slist.at(1)+"/"+slist.at(2));

         //更新天气类型
         m_typeList[i]->setText(m_day[i].type);
         m_typeIconList[i]->setPixmap(m_typeMap[m_day[i].type]);//更新图片
//空气质量
        switch (checkedQulity(m_day[i].aqi)) {
        case 1:
            m_ApiList[i]->setText("优");
            m_ApiList[i]->setStyleSheet("background-color: rgb(121,184,0);");
            break;
        case 2:
            m_ApiList[i]->setText("良");
            m_ApiList[i]->setStyleSheet("background-color: rgb(255,187,23);");
            break;
        case 3:
            m_ApiList[i]->setText("轻度");
            m_ApiList[i]->setStyleSheet("background-color: rgb(255,87,97);");
            break;
        case 4:
            m_ApiList[i]->setText("中度");
            m_ApiList[i]->setStyleSheet("background-color: rgb(235,17,27);");
            break;
        case 5:
            m_ApiList[i]->setText("重度");
            m_ApiList[i]->setStyleSheet("background-color: rgb(170,0,0);");
            break;
        case 6:
            m_ApiList[i]->setText("严重");
            m_ApiList[i]->setStyleSheet("background-color: rgb(110,0,0);");
            break;
        }
        //风力风向
        m_FXList[i]->setText(m_day[i].fx);
        m_FLList[i]->setText(m_day[i].fl);
    }

}

//检查空气质量
int MainWindow::checkedQulity(int aqi)
{
    if (aqi >= 0 && aqi <= 50) {
         return 1;
    }else if(aqi > 50 && aqi <= 100){
         return 2;
    }else if(aqi > 100 && aqi <= 150){
         return 3;
    }else if(aqi > 150 && aqi <= 200){
         return 4;
    }else if(aqi > 200 && aqi <= 250){
         return 5;
    }else{
         return 6;
    }
    return -1;
}

8.实现搜索框搜索城市

获取LeCity搜索框输入的数据

void MainWindow::on_btnSearch_clicked()
{
    QString cityName=ui->leCity->text();
    getWeaterInfo(cityName);
}

天气工具类

//天气工具类
class weatherTool{
private:
    //内联静态变量 在占用调用处的内存
    inline static QMap m_cityMap={};
    static void initCityMap(){
        QFile fd("E:\\code\\citycode.json");
        fd.open(QIODevice::ReadOnly|QIODevice::Text);
        QByteArray allJson=fd.readAll();
        fd.close();
        QJsonParseError err;
        //读出的数据转换为json的
        QJsonDocument doc=  QJsonDocument::fromJson(allJson,&err);

        if(err.error!=QJsonParseError::NoError||!doc.isArray()){
          //不是数组就错误
            qDebug()<<"json数据转换失败...";
            return;
      }  //转换成json数组
        QJsonArray rootArray=doc.array();
      for(int i=0;i0){
                m_cityMap.insert(cityName,cityCode);
            }
        }

    }

public :
    QString static getCityCode(QString cityName){
        qDebug()<<"cityName"<::iterator it = m_cityMap.find(cityName);
        //到结尾了,未找到,加上市,再搜一遍
        if (it == m_cityMap.end()) {
           it = m_cityMap.find(cityName + "市");
        }
        //找到了
        if (it != m_cityMap.end()) {
           qDebug()<<" if (it != m_cityMap.end())";
           return it.value();
        }

        QMessageBox::warning(nullptr,"城市名字错误","输入的城市名字错误...",QMessageBox::Ok);
        //否则
        return "";
    }


};

读取这个文件里面的数据QFile fd("E:\\code\\citycode.json");把city_name和city_code取出来放到QMap集合里面去,之后在和输入的CityName进行查找获得city_code.

QT实现天气预报_第1张图片

 QT实现天气预报_第2张图片

 

你可能感兴趣的:(qt,开发语言)