目录
前言
一、ui界面
1、整体效果
2、主界面
3、天气查询
4、编辑日志
二、程序所有的文件
声明软件图标
1、widget.cpp
2、weather.cpp
3、text.cpp
4、main.cpp
5、widget.h
6、weather.h
7、text.h
这是一款基于QT制作的日历软件具有日期查询,日期播报,天气查询,日志安排等功能。
1、利用ui将自己需要的控键拖出来并合理布局,需要有功能的按键就转到槽函数编写功能代码(图片资源需要自己找素材)
2、点击更新天气以后会更新出当前城市的天气情况(默认为成都),天气情况会由动态的一组图片展示
3、双击空白处会播报当前日期
1、点击主界面中的天气查询按钮会弹出此窗口
在城市一栏输入你想查找的城市点击查询即可显示出具体的天气信息,(详细信息也会显示到文本浏览框中——但是文本是josn格式有需要可以解析出来)
点击添加日志按钮弹出此对话框,键入日志安排点击确定关闭窗口日志将显示到主界面
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
on_lcdNumber_overflow();//显示当前时间
QTimer *pTimer=new QTimer();
connect(pTimer,SIGNAL(timeout()),this,SLOT(on_lcdNumber_overflow()));
pTimer->start(500);
QDateTime date = QDateTime::currentDateTime();
ui->label_8->setText(date.toString("yyyy年MM月dd日 ddd"));
QMovie *m = new QMovie(":/img/3.gif");//TODO
ui->label_6->setMovie(m);
m->start();
http_acc = new QNetworkAccessManager;
connect(http_acc, SIGNAL(finished(QNetworkReply*)), this, SLOT(new_weather(QNetworkReply*)));
//界面优化
ui->pushButton->setStyleSheet(
//正常状态样式
"QPushButton{"
"background-color:rgb(8,189,253);"//设置按钮背景色
"border-radius:3px;"//设置圆角半径
"}"
"QPushButton:hover{"
"background-color:#999999;"//设置按钮点击时的背景颜色
"}");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_lcdNumber_overflow()
{
QDateTime date_t=QDateTime::currentDateTime();
this->ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
this->ui->lcdNumber->setStyleSheet("color:black;");
this->ui->lcdNumber->display(date_t.toString("HH:mm:ss"));
}
void Widget::on_pushButton_clicked()//天气查询
{
weather *weatherAsk = new weather;
weatherAsk->show();
}
void Widget::on_pushButton_2_clicked()//添加日志
{
text_window = new text;
connect(text_window,SIGNAL(successed(QString)),this,SLOT(receiveData(QString)));
text_window->show();
}
void Widget::on_pushButton_3_clicked()//删除日志
{
ui->textBrowser->clear();
}
//void Widget::on_pushButton_4_clicked()
//{}
void Widget::on_pushButton_4_clicked()//更新天气
{
QNetworkRequest request;
request.setUrl(QString("http://autodev.openspeech.cn/csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=成都&latitude=39.902895&longitude=116.427915&needMoreData=true&pageNo=1&pageSize=1"));
http_acc->get(request);
}
void Widget::new_weather(QNetworkReply *r)
{
QString all = r->readAll();
QJsonParseError err;
QJsonDocument json_recv = QJsonDocument::fromJson(all.toUtf8(),&err);
//qDebug() << err.error;
if(!json_recv.isNull())
{
//qDebug()<<"finish!!";
QJsonObject object = json_recv.object();
if(object.contains("data"))
{
QJsonValue value = object.value("data"); // 获取指定 key 对应的 value
//qDebug()<label_5->setText(city);
//qDebug()<label_9->setText(weather);
if(weather.contains("多云"))//动态的显示天气情况
{
QMovie *m = new QMovie(":/img/duoyun.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
if(weather.contains("大雨"))
{
QMovie *m = new QMovie(":/img/dayu.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
if(weather.contains("晴"))
{
QMovie *m = new QMovie(":/img/qing.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
if(weather.contains("小雨"))
{
QMovie *m = new QMovie(":/img/xiaoyu.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
if(weather.contains("阴天"))
{
QMovie *m = new QMovie(":/img/yintian.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
if(weather.contains("雷阵雨"))
{
QMovie *m = new QMovie(":/img/1.gif");//TODO
ui->label_6->setMovie(m);
m->start();
}
//qDebug()<label_7->setText(str);
//qDebug()<label_7->text();
ui->label_7->setText(str2+'~'+str1);
//qDebug()<deleteLater();
}
void Widget::mouseDoubleClickEvent(QMouseEvent *event)//鼠标双击事件
{
pos = event->globalPos();
//qDebug()<<"111111111";
QTextToSpeech *s = new QTextToSpeech;
s->say("今天是"+ui->label_8->text());
}
void Widget::receiveData(QString buf1)//接受日志信息的槽函数
{
//qDebug()<textBrowser->setText(buf1);
}
//void Widget::on_calendarWidget_clicked(const QDate &date)
//{
//}
#include "weather.h"
#include "ui_weather.h"
weather::weather(QWidget *parent) :
QWidget(parent),
ui(new Ui::weather)
{
ui->setupUi(this);
http_acc = new QNetworkAccessManager;
connect(http_acc, SIGNAL(finished(QNetworkReply*)), this, SLOT(recv_ack(QNetworkReply*)));
}
weather::~weather()
{
delete ui;
}
void weather::recv_ack(QNetworkReply *r)//天气查询
{
//QByteArray buf = r->readAll();//不能两次重复接收
//qDebug() << buf.toStdString().c_str();
//ui->textBrowser->setText(buf.toStdString().c_str());
//qDebug()<<"finish!!";
QString all = r->readAll();
ui->textBrowser->setText(all);
QJsonParseError err;
QJsonDocument json_recv = QJsonDocument::fromJson(all.toUtf8(),&err);
//qDebug() << err.error;
if(!json_recv.isNull())
{
//qDebug()<<"finish!!";
QJsonObject object = json_recv.object();
if(object.contains("data"))
{
QJsonValue value = object.value("data"); // 获取指定 key 对应的 value
//qDebug()<label_11->setText(weather);
if(weather.contains("大雨"))//动态的显示天气情况
{
QMovie *m = new QMovie(":/img/dayu.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
if(weather.contains("多云"))
{
QMovie *m = new QMovie(":/img/duoyun.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
if(weather.contains("晴"))
{
QMovie *m = new QMovie(":/img/qing.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
if(weather.contains("小雨"))
{
QMovie *m = new QMovie(":/img/xiaoyu.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
if(weather.contains("阴天"))
{
QMovie *m = new QMovie(":/img/yintian.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
if(weather.contains("雷阵雨"))
{
QMovie *m = new QMovie(":/img/1.gif");//TODO
ui->label_5->setMovie(m);
m->start();
}
//qDebug()<label_10->setText(wind);
//qDebug()<label_7->setNum(low);
//qDebug()<label_3->setNum(high);
qDebug()<deleteLater();
}
void weather::on_pushButton_clicked()//链接墨迹天气的API接口
{
//发送一个http请求
QNetworkRequest request;
request.setUrl(QString("http://autodev.openspeech.cn/csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=%0&latitude=39.902895&longitude=116.427915&needMoreData=true&pageNo=1&pageSize=1").arg(ui->lineEdit->text()));
http_acc->get(request);
}
#include "text.h"
#include "ui_text.h"
#include
text::text(QWidget *parent) :
QWidget(parent),
ui(new Ui::text)
{
ui->setupUi(this);
//me =new Widget;
}
text::~text()
{
delete ui;
}
void text::on_pushButton_clicked()//创建文件存储日志
{
buf = ui->textEdit->toPlainText();
//connect(this,SIGNAL(successed(QString)),this->me,SLOT(receiveData(QString)));
emit successed(buf);
//qDebug()<close();
QByteArray array=ui->textEdit->toPlainText().toUtf8();
QFile file("try.txt");
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(array);
file.close();
this->hide();
}
#include "widget.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "weather.h"
#include "text.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
private slots:
void on_pushButton_clicked();
void on_lcdNumber_overflow();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void receiveData(QString);//接受日志信息
void new_weather(QNetworkReply*);//更新天气
//void on_calendarWidget_clicked(const QDate &date);
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void mouseDoubleClickEvent(QMouseEvent *event); //鼠标双击事件
private:
Ui::Widget *ui;
QPoint pos;//鼠标位置
weather *weather_window;
text *text_window;
QNetworkAccessManager *http_acc;
};
#endif // WIDGET_H
#ifndef WEATHER_H
#define WEATHER_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace Ui {
class weather;
}
class weather : public QWidget
{
Q_OBJECT
public slots:
void recv_ack(QNetworkReply*);
public:
explicit weather(QWidget *parent = 0);
~weather();
private slots:
void on_pushButton_clicked();
private:
Ui::weather *ui;
QNetworkAccessManager *http_acc;
};
#endif // WEATHER_H
#ifndef TEXT_H
#define TEXT_H
#include
#include
namespace Ui {
class text;
}
class text : public QWidget
{
Q_OBJECT
signals:
void successed(QString);
public:
explicit text(QWidget *parent = 0);
~text();
private slots:
void on_pushButton_clicked();
private:
Ui::text *ui;
QString buf;
};
#endif // TEXT_H