Qt實現天氣預報與PM2.5監測系統(8)一周天氣

Qt實現天氣預報與PM2.5監測系統(8)一周天氣

程序界面

  • 界面上共顯示六天的天氣情況
  • 每一列都是類ShowWeek的一個實例化,程序中由對象數組showWeek[6]

這裏寫圖片描述

    //初始化一周天氣界面,postion value
    int x = 250;
    for(int i=0;i<MAX_DAY;i++){
        showWeek[i] = new DayData(this);
        showWeek[i]->setVal(x,100,QString::number(i));
        showWeek[i]->hide();
        x += 120;        
    }



#ifndef DAYDATA_H
#define DAYDATA_H
#include <QWidget>
#include "showlabel.h"


class DayData : public QWidget
{
    Q_OBJECT
public:
    DayData(QWidget *parent = 0);
    void setVal(int x, int y,QString wicon);
    void updateVal(QString *daily);
signals:

public slots:

private:
    ShowLabel *weekday;
    ShowLabel *date;
    ShowLabel *temp;
    ShowLabel *weather;
    ShowLabel *wind;
    ShowLabel *windpower;
    QLabel *weatherIcon;
};

#endif // DAYDATA_H



#include "daydata.h"

DayData::DayData(QWidget *parent) : QWidget(parent)
{
    this->setMinimumSize(1024,600);
    this->setMaximumSize(1024,600);
}


void DayData::setVal(int x, int y,QString wicon)
{
    QDateTime time = QDateTime::currentDateTime();//獲取系統現在的時間

    weekday = new ShowLabel(this);
    weekday->addFontSize(2);
    //weekday->setGeometry(QRect(250, 80, 100, 40));
    weekday->setGeometry(QRect(x, y, 100, 40));
    weekday->setAlignment(Qt::AlignHCenter);
    weekday->setText(tr("今天"));

    date = new ShowLabel(this);
    date->addFontSize(2);
    date->setGeometry(QRect(x, y+40, 100, 40));
    date->setAlignment(Qt::AlignHCenter);
    date->setText(time.toString("M月d日"));


    weatherIcon = new QLabel(this);
    weatherIcon->setGeometry(QRect(x+10, y+100, 70, 70));
    QString iconStr = ":/images/weathercn02/" + wicon + ".png" ;
    weatherIcon->setPixmap(QPixmap(iconStr));
    weatherIcon->setScaledContents(true);

    temp = new ShowLabel(this);
    temp->addFontSize(3);
    temp->setGeometry(QRect(x, y+210, 100, 40));
    temp->setAlignment(Qt::AlignHCenter);
    temp->setText(tr("2-9℃"));


    weather = new ShowLabel(this);
    weather->addFontSize(3);
    weather->setGeometry(QRect(x, y+250, 100, 40));
    weather->setAlignment(Qt::AlignHCenter);
    weather->setText(tr("晴"));

    wind = new ShowLabel(this);
    wind->addFontSize(-2);
    wind->setGeometry(QRect(x, y+340, 100, 40));
    wind->setAlignment(Qt::AlignHCenter);
    wind->setText(tr("風"));


    windpower = new ShowLabel(this);
    windpower->addFontSize(0);
    windpower->setGeometry(QRect(x, y+380, 100, 40));
    windpower->setAlignment(Qt::AlignHCenter);
    windpower->setText(tr("3級"));
}


void DayData::updateVal(QString *daily)
{
    weekday->setText(daily[1]);
    date->setText(daily[0].mid(5));
    QString iconStr = ":/images/weathercn02/" + daily[5] + ".png" ;
    weatherIcon->setPixmap(QPixmap(iconStr));
    temp->setText(daily[2]+tr("-")+daily[3]+tr("℃"));
    weather->setText(daily[4]);
    wind->setText(daily[6]);
    windpower->setText(daily[7]);
}

界面數據更新


//更新顯示的數據
    if(temp_timer>=300 || sysSet->getFlagVal()==1){
        ...

        this->get_week_value();
        for(int i=0;i<MAX_DAY;i++){
            showWeek[i]->updateVal(weekArr[i]);
        }

        ...
    }

解析數據
python程序從雲端獲取的一周天氣數據寫入文件week_file,通過get_week_value方法從文件中解析出有效的數據到QString數組中。

int SysDialog::get_week_value(void)
{
    FILE *fp;
    char read_buf[LEN];
    char buf[LEN];
    char *p,*temp;
    int i=0,len=0,daily=0,item=0;

    bzero(read_buf,LEN);

    fp = fopen(WEEK_FILE,"r");
    if(NULL == fp) return -1;

    len = fread(read_buf,1,1024,fp);
//    len= strlen(read_buf);
    printf("%s\n,len=%d\n",read_buf,len);

    p=read_buf;
    bzero(buf,LEN);
    temp = buf;

    for(i=0;i<len;i++){
        if(p[i] != '$'){
            *temp = p[i];
            temp++;
        }else{
            *temp=0;
            temp=buf;
            weekArr[daily][item] = QString(buf);
            if(++item > 7){
                item = 0;
                if(++daily > 5)
                    break;
            }
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章