Qt实现天气预报与PM2.5监测系统(10)系统设置
设置界面
功能说明:
1.城市设置–设置系统从云端要获取天气数据的城市,要求用户输入城市代码。(由类citysetdlg实现)
2.网络更新间隔–设置网络更新天气的时间间隔,用户输入间隔小时。(由类timersetdlg实现)
3.数据手动更新–用户点击“立即更新”按钮后,系统会从网络上的云端获取最新的天气数据。
设置界面头文件
#ifndef SYSSET_H
#define SYSSET_H
#include <QProcess>
#include <QWidget>
#include "showlabel.h"
#include "mybutton.h"
#include "timersetdlg.h"
#include "citysetdlg.h"
class SysSet : public QWidget
{
Q_OBJECT
public:
explicit SysSet(QWidget *parent = 0);
int getFlagVal();
void setFlagVal(int val);
void setSysInit(QString city,QString timer);
int getNetTimer();
int writeFile(QString fileName, QString data);
protected:
bool eventFilter(QObject *obj, QEvent *ev);
signals:
void hourChange(int);
public slots:
void goSave();
void goRefresh();
void goSetTimer();
void goSetCity();
private:
int falgRefresh;
QLabel * line1;
QLabel * line2;
QLabel * line3;
QLabel * line4;
QLabel * rightCity;
QLabel * rightNet;
QLabel * cityIcon;
QLabel * netIcon;
QLabel * refreshIcon;
ShowLabel *cityName;
ShowLabel *netName;
ShowLabel *refreshName;
ShowLabel *cityValue;
ShowLabel *netValue;
ShowLabel *hourChar;
Button *refreshBut;
Button *saveBut;
QProcess *myProcess;
QString cityCode;
};
#endif // SYSSET_H
设置界面实现程序
#include "sysset.h"
SysSet::SysSet(QWidget *parent) : QWidget(parent)
{
this->setMinimumSize(1024,600);
this->setMaximumSize(1024,600);
int y = 150;
int x = 350;
line1 = new QLabel(this);
line1->setAlignment(Qt::AlignHCenter);
line1->setGeometry(QRect(x, y, 500, 10));
line1->setPixmap(QPixmap(":/images/line.png"));
line2 = new QLabel(this);
line2->setAlignment(Qt::AlignHCenter);
line2->setGeometry(QRect(x, y+80, 500, 10));
line2->setPixmap(QPixmap(":/images/line.png"));
line3 = new QLabel(this);
line3->setAlignment(Qt::AlignHCenter);
line3->setGeometry(QRect(x, y+160, 500, 10));
line3->setPixmap(QPixmap(":/images/line.png"));
cityIcon = new QLabel(this);
cityIcon->setAlignment(Qt::AlignHCenter);
cityIcon->setGeometry(QRect(x, y+10, 60, 60));
cityIcon->setPixmap(QPixmap(":/images/city_icon.png"));
cityIcon->setScaledContents(true);
netIcon = new QLabel(this);
netIcon->setAlignment(Qt::AlignHCenter);
netIcon->setGeometry(QRect(x, y+90, 60, 60));
netIcon->setPixmap(QPixmap(":/images/timer_icon.png"));
netIcon->setScaledContents(true);
refreshIcon = new QLabel(this);
refreshIcon->setAlignment(Qt::AlignHCenter);
refreshIcon->setGeometry(QRect(x, y+175, 60, 60));
refreshIcon->setPixmap(QPixmap(":/images/update_icon.png"));
refreshIcon->setScaledContents(true);
cityName = new ShowLabel(this);
cityName->addFontSize(2);
cityName->setAlignment(Qt::AlignHCenter);
cityName->setGeometry(QRect(420,y+30,160,60));
cityName->setText(tr("城市设置"));
netName = new ShowLabel(this);
netName->addFontSize(2);
netName->setAlignment(Qt::AlignHCenter);
netName->setGeometry(QRect(420,y+110,160,60));
netName->setText(tr("网络更新间隔"));
refreshName = new ShowLabel(this);
refreshName->addFontSize(2);
refreshName->setAlignment(Qt::AlignHCenter);
refreshName->setGeometry(QRect(420,y+190,160,60));
refreshName->setText(tr("数据手动更新"));
cityValue = new ShowLabel(this);
cityValue->addFontSize(2);
cityValue->setAlignment(Qt::AlignHCenter);
cityValue->setGeometry(QRect(650,y+30,160,60));
cityValue->setText(tr("--N--"));
connect(cityValue,SIGNAL(clicked()),this,SLOT(goSetCity()) );
netValue = new ShowLabel(this);
netValue->addFontSize(2);
netValue->setAlignment(Qt::AlignHCenter);
netValue->setGeometry(QRect(690,y+110,50,60));
netValue->setText(tr("--N--"));
connect(netValue,SIGNAL(clicked()),this,SLOT(goSetTimer()) );
hourChar = new ShowLabel(this);
hourChar->addFontSize(2);
hourChar->setAlignment(Qt::AlignHCenter);
hourChar->setGeometry(QRect(730,y+110,60,60));
hourChar->setText(tr("小时"));
connect(hourChar,SIGNAL(clicked()),this,SLOT(goSetTimer()) );
rightCity = new QLabel(this);
rightCity->setAlignment(Qt::AlignHCenter);
rightCity->setGeometry(QRect(800, y+25, 40, 40));
rightCity->setPixmap(QPixmap(":/images/right_icon.png"));
rightCity->setScaledContents(true);
rightNet = new QLabel(this);
rightNet->setAlignment(Qt::AlignHCenter);
rightNet->setGeometry(QRect(800, y+105, 40, 40));
rightNet->setPixmap(QPixmap(":/images/right_icon.png"));
rightNet->setScaledContents(true);
//rightNet->installEventFilter(this);
refreshBut = new Button(this);
refreshBut->setButtonPicture(QPixmap( ":/images/refresh_1.png"));
refreshBut->setPressPicture(QPixmap( ":/images/refresh_2.png"));
refreshBut->setReleasePicture(QPixmap( ":/images/refresh_1.png"));
refreshBut->setEnterPicture(QPixmap( ":/images/refresh_2.png"));
refreshBut->setReleasePicture(QPixmap( ":/images/refresh_1.png"));
refreshBut->set_X_Y_width_height(680,y+180,140,50);
connect(refreshBut,SIGNAL(clicked()),this,SLOT(goRefresh()) );
falgRefresh = 0;
//显示按钮
saveBut = new Button(this);
saveBut->setButtonPicture(QPixmap( ":/images/save_1.png"));
saveBut->setPressPicture(QPixmap( ":/images/save_2.png"));
saveBut->setReleasePicture(QPixmap( ":/images/save_1.png"));
saveBut->setEnterPicture(QPixmap( ":/images/save_2.png"));
saveBut->setReleasePicture(QPixmap( ":/images/save_1.png"));
saveBut->set_X_Y_width_height(526,450,160,60);
connect(saveBut,SIGNAL(clicked()),this,SLOT(goSave()) );
myProcess = new QProcess(parent);
}
void SysSet::setSysInit(QString city,QString timer)
{
cityValue->setText(city);
netValue->setText(timer);
}
int SysSet::getFlagVal()
{
return falgRefresh;
}
void SysSet::setFlagVal(int val)
{
falgRefresh = val;
}
void SysSet::goSave()
{
QString oldCode;
QFile f("/home/fa/code_file");
if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Open failed." << endl;
}else{
QTextStream fStream(&f);
oldCode = fStream.readLine();
f.close();
}
writeFile("/home/fa/code_file",cityCode);
writeFile("/home/fa/timer_file",netValue->text());
emit hourChange(netValue->text().toInt());
if(oldCode != cityCode){
QString program = "/usr/bin/python";
QStringList arguments;
arguments << "/home/fa/temp_p2_7.py" ;
myProcess->execute(program, arguments);
falgRefresh = 1;
qDebug() << oldCode << cityCode << endl;
}
}
int SysSet::writeFile(QString fileName, QString data)
{
QFile f(fileName);
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Open failed." << endl;
return -1;
}
QTextStream fStream(&f);
fStream << data;
f.close();
return 0;
}
void SysSet::goSetCity()
{
CitySetDlg * setCity = new CitySetDlg(NULL);
setCity->setGeometry(QRect(300, 100, 600, 300));
setCity->setWindowModality(Qt::ApplicationModal);
setCity->setItem(cityValue,&cityCode);
setCity->show();
}
void SysSet::goSetTimer()
{
TimerSetDlg * setTimer = new TimerSetDlg(NULL);
setTimer->setGeometry(QRect(300, 100, 600, 300));
setTimer->setWindowModality(Qt::ApplicationModal);
setTimer->setItem(netValue);
setTimer->show();
}
int SysSet::getNetTimer()
{
return netValue->text().toInt();
}
void SysSet::goRefresh()
{
QString program = "/usr/bin/python";
QStringList arguments;
arguments << "/home/fa/temp_p2_7.py" ;
myProcess->execute(program, arguments);
falgRefresh = 1;
}
键盘界面
键盘类是城市设置类与间隔时间设置类的基类。
键盘界面头文件
#ifndef KEYNUM_H
#define KEYNUM_H
#include <QWidget>
#include "mybutton.h"
#include "showlabel.h"
class KeyNum : public QWidget
{
Q_OBJECT
public:
explicit KeyNum(QWidget *parent = 0);
Button* createButton(int x, int y,const QString &num, const char *member);
void setItem(QString itemName);
void showRet(QString ret);
QString getValue();
signals:
public slots:
void digitClicked();
void backspaceClicked();
void cleanClicked();
private:
Button * keyC;
Button * keyB;
Button *digitButtons[10];
QLabel * label_pic_bg;
ShowLabel *display;
ShowLabel *item;
};
#endif // KEYNUM_H
键盘界面实现文件
#include "keynum.h"
KeyNum::KeyNum(QWidget *parent) : QWidget(parent)
{
this->setMinimumSize(500,180);
this->setMaximumSize(500,180);
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
label_pic_bg = new QLabel(this);
label_pic_bg->setPixmap(QPixmap(":/images/line.png"));
label_pic_bg->setGeometry(50,55,400,2);
// label_pic_bg->setScaledContents(true);
item = new ShowLabel(this);
item->addFontSize(-1);
item->setAlignment(Qt::AlignCenter);
item->setGeometry(QRect(50,10,200,50));
item->setText(tr("设置: "));
display = new ShowLabel(this);
display->addFontSize(1);
display->setAlignment(Qt::AlignCenter);
display->setGeometry(QRect(250,10,200,50));
display->setText(tr(""));
for (int i = 0; i < 10; ++i) {
int x = 20,y = 0;
if(i<5){
y = 75;
x = x + i*80;
}else{
y = 130;
x = x + (i-5)*80;
}
digitButtons[i] = createButton(x,y,QString::number(i), SLOT(digitClicked()));
}
keyB = createButton(420,75,tr("B"), SLOT(backspaceClicked()));
keyC = createButton(420,130,tr("C"), SLOT(cleanClicked()));
}
void KeyNum::setItem(QString itemName)
{
item->setText(itemName);
}
void KeyNum::showRet(QString ret)
{
display->setText(ret);
}
QString KeyNum::getValue()
{
return display->text();
}
void KeyNum::digitClicked()
{
//获取发送者对象指针
Button *clickedButton = qobject_cast<Button *>(sender());
QString digitValue = clickedButton->getId();//按键的数值
display->setText(display->text() + digitValue);
}
void KeyNum::backspaceClicked()
{
QString text = display->text();
text.chop(1);
if (text.isEmpty()) {
text = "";
}
display->setText(text);
}
void KeyNum::cleanClicked()
{
display->setText(tr(""));
}
Button *KeyNum::createButton(int x, int y,const QString &num, const char *member)
{
Button *button = new Button(this);
QString pixName1 = QString(tr(":/images/numkey/")+num+tr("_1.png"));
QString pixName2 = QString(tr(":/images/numkey/")+num+tr("_2.png"));
qDebug() << pixName1 << pixName2 << "x=" << x << "y=" << y;
button->setId(num);
button->setButtonPicture(pixName1);
button->setPressPicture(pixName2);
button->setReleasePicture(pixName1);
button->setEnterPicture(pixName2);
button->setReleasePicture(pixName1);
button->set_X_Y_width_height(x,y,60,50);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
城市设置界面
头文件
#ifndef CITYSETDLG_H
#define CITYSETDLG_H
#include <QWidget>
#include "mybutton.h"
#include "showlabel.h"
#include "keynum.h"
#define CODE_NUM 4
struct city_code
{
QString name;
QString code;
};
class CitySetDlg : public QWidget
{
Q_OBJECT
public:
explicit CitySetDlg(QWidget *parent = 0);
void setItem(ShowLabel *item,QString *code);
int writeCode(QString code);
signals:
public slots:
void goSet();
private:
Button *ok;
Button *cancel;
QLabel *label_pic_bg;
KeyNum *keyNum;
ShowLabel *inputItem;
QString *inputCode;
struct city_code codeList[CODE_NUM];
};
#endif // CITYSETDLG_H
实现文件
#include "citysetdlg.h"
CitySetDlg::CitySetDlg(QWidget *parent) : QWidget(parent)
{
this->setGeometry(QRect(300, 100, 600, 300));
this->setMinimumSize(600,300);
this->setMaximumSize(600,300);
this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
label_pic_bg = new QLabel(this);
// label_pic_bg->setGeometry(QRect(300, 150, 600, 400));
label_pic_bg->setPixmap(QPixmap(":/images/set_bg.png"));
label_pic_bg->setScaledContents(true);
//显示按钮
ok = new Button(this);
ok->setButtonPicture(QPixmap( ":/images/ok_1.png"));
ok->setPressPicture(QPixmap( ":/images/ok_2.png"));
ok->setReleasePicture(QPixmap( ":/images/ok_1.png"));
ok->setEnterPicture(QPixmap( ":/images/ok_2.png"));
ok->setReleasePicture(QPixmap( ":/images/ok_1.png"));
ok->set_X_Y_width_height(150,230,120,60);
connect(ok,SIGNAL(clicked()),this,SLOT(goSet()) );
cancel = new Button(this);
cancel->setButtonPicture(QPixmap( ":/images/cancel_1.png"));
cancel->setPressPicture(QPixmap( ":/images/cancel_2.png"));
cancel->setReleasePicture(QPixmap( ":/images/cancel_1.png"));
cancel->setEnterPicture(QPixmap( ":/images/cancel_2.png"));
cancel->setReleasePicture(QPixmap( ":/images/cancel_1.png"));
cancel->set_X_Y_width_height(330,230,120,60);
connect(cancel,SIGNAL(clicked()),this,SLOT(close()) );
keyNum = new KeyNum(this);
keyNum->setItem("城市设置(输入编号):");
keyNum->setGeometry(50,30,500,200);
/*
101010100,北京
101020100,上海,上海,上海
101030100,天津,天津,天津
101200101,武汉,武汉,湖北
*/
QString cityName[CODE_NUM] = {tr("北京"),tr("上海"),tr("天津"),tr("武汉")};
QString cityCode[CODE_NUM] = {tr("101010100"),tr("101020100"),tr("101030100"),tr("101200101")};
for(int i=0;i<CODE_NUM;i++){
codeList[i].name = cityName[i];
codeList[i].code = cityCode[i];
}
}
void CitySetDlg::setItem(ShowLabel *item,QString *code)
{
inputItem = item;
inputCode = code;
}
int CitySetDlg::writeCode(QString code)
{
QFile f("/home/fa/code_file");
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Open failed." << endl;
return -1;
}
QTextStream fStream(&f);
fStream << code;
f.close();
return 0;
}
void CitySetDlg::goSet()
{
QString code = keyNum->getValue();
QString cityName;
QString cityCode;
bool findCode = false;
for(int i=0;i<CODE_NUM;i++){
if(codeList[i].code == code){
cityName = codeList[i].name;
cityCode = codeList[i].code;
findCode = true;
break;
}
}
if(findCode)
{
// keyNum->showRet(tr("设置成功!"));
qDebug() << keyNum->getValue();
inputItem->setText( cityName);
*inputCode=cityCode;
//open
//write cityCode
//writeCode(cityCode);
close();
}else{
keyNum->showRet(tr("设置无效!"));
}
}