- 界面上共显示六天的天气情况
- 每一列都是类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;
}