Skip to content

Commit fe2450e

Browse files
committed
11.27
1 parent 48425ec commit fe2450e

16 files changed

+738
-0
lines changed

weather/main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "widget.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
Widget w;
8+
w.show();
9+
10+
return a.exec();
11+
}

weather/pic.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/env python
2+
import urllib
3+
import os
4+
import sys
5+
6+
url1 = sys.argv[1]
7+
url2 = sys.argv[2]
8+
#url = "http://api.map.baidu.com/images/weather/day/zhongyu.png"
9+
url = url1
10+
path = url2
11+
data = urllib.urlopen(url).read()
12+
f = file(path,"wb")
13+
f.write(data)
14+
f.close

weather/pix/1.png

141 KB
Loading

weather/pix/2.png

302 KB
Loading

weather/pix/3.png

158 KB
Loading

weather/pix/4.png

166 KB
Loading

weather/pix/5.png

218 KB
Loading

weather/pix/6.png

122 KB
Loading

weather/pix/7.png

217 KB
Loading

weather/pix/8.png

344 KB
Loading

weather/untitled-1.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/env python
2+
import types
3+
import urllib2
4+
import json
5+
import os
6+
import sys
7+
#-*-coding:utf-8 -*-
8+
url0 = "http://api.map.baidu.com/telematics/v3/weather?location="
9+
url1 = "&output=json&ak=ozO9AVLiHTGEltbNzUxVKPCk"
10+
url2 = sys.argv[1]
11+
def registerUrl():
12+
try:
13+
url =url0 + url2 + url1
14+
data = urllib2.urlopen(url).read()
15+
print data
16+
return data
17+
print (url)
18+
#return url
19+
except Exception,e:
20+
print e
21+
22+
23+
f = open('./weathers','w')
24+
f.write(registerUrl())
25+
f.close
26+
27+

weather/weather.pro

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2015-11-26T20:09:12
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
QT += network
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = weather
12+
TEMPLATE = app
13+
14+
15+
SOURCES += main.cpp\
16+
widget.cpp
17+
18+
HEADERS += widget.h

weather/weather.pro.user

+271
Large diffs are not rendered by default.

weather/weathers

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"error":0,"status":"success","date":"2015-11-24","results":[{"currentCity":"南京","pm25":"22","index":[{"title":"穿衣","zs":"冷","tipt":"穿衣指数","des":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。"},{"title":"洗车","zs":"不宜","tipt":"洗车指数","des":"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"},{"title":"旅游","zs":"较不宜","tipt":"旅游指数","des":"天气稍凉,风大,可能对出行产生一定影响,同时有较强雨,若坚持旅行建议带上雨具。"},{"title":"感冒","zs":"极易发","tipt":"感冒指数","des":"将有一次强降温过程,天气寒冷,且风力较强,极易发生感冒,请特别注意增加衣服保暖防寒。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"有较强降水,建议您选择在室内进行健身休闲运动。"},{"title":"紫外线强度","zs":"最弱","tipt":"紫外线强度指数","des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}],"weather_data":[{"date":"周二 11月24日 (实时:7℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhongyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhongyu.png","weather":"小到中雨","wind":"东北风5-6级","temperature":"9 ~ 2℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"西北风4-5级","temperature":"7 ~ -1℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"西北风3-4级","temperature":"5 ~ -3℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东南风3-4级","temperature":"10 ~ 3℃"}]}]}

weather/widget.cpp

+322
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
#include "widget.h"
2+
#include <QVBoxLayout>
3+
#include <QHBoxLayout>
4+
#include <QProcess>
5+
#include <QJsonDocument>
6+
#include <QJsonParseError>
7+
#include <QVariantMap>
8+
#include <QVariantList>
9+
#include <iostream>
10+
#include <QDebug>
11+
#include <QProcess>
12+
#include <QTimer>
13+
#include <iostream>
14+
#include <QMessageBox>
15+
#include <QMovie>
16+
#include <QUrl>
17+
#include <QNetworkReply>
18+
using namespace std;
19+
20+
Widget::Widget(QWidget *parent)
21+
: QWidget(parent)
22+
{
23+
this->resize(800,600);
24+
25+
linedit = new QLineEdit(this);
26+
linedit->setPlaceholderText("which city you want to kown");
27+
linedit->setStyleSheet("border: 3px solid blue;border-radius:10px");
28+
//linedit->setGeometry(200,0,300,100);
29+
button = new QPushButton(this);
30+
31+
button->setStyleSheet("background-color:rgb(200,155,100)");
32+
button->setText("clicked");
33+
label = new QLabel(this);
34+
site = new QLabel;
35+
pm2 = new QLabel;
36+
datetime = new QLabel;
37+
waitlabel = new QLabel;
38+
// waitlabel->setText("请不要着急,天气小子为您加载数据...");
39+
waitlabel->setText(" ");
40+
label->setText("please input city");
41+
//label->setGeometry(0,0,200,100);
42+
site->setText("place");
43+
pm2->setText("pm2");
44+
datetime->setText("datetime");
45+
first = new QWidget;
46+
second = new QWidget;
47+
third = new QWidget;
48+
four = new QWidget;
49+
stack = new QStackedWidget;
50+
stack->addWidget(first);
51+
stack->addWidget(second);
52+
stack->addWidget(third);
53+
stack->addWidget(four);
54+
stack->setStyleSheet("border: 3px solid green;border-radius:5px");
55+
textedit = new QTextEdit;
56+
textedit->setStyleSheet("border: 3px solid red ;border-radius:10px");
57+
combobox = new QComboBox;
58+
combobox->setStyleSheet("border: 3px solid blue;border-radius:10px");
59+
combobox->addItem("今天");
60+
combobox->addItem("明天");
61+
combobox->addItem("后天");
62+
combobox->addItem("大后天");
63+
QVBoxLayout *vbox = new QVBoxLayout;
64+
QHBoxLayout *hbox = new QHBoxLayout;
65+
QHBoxLayout *citybox = new QHBoxLayout;
66+
QHBoxLayout *strechbox = new QHBoxLayout;
67+
68+
strechbox->addWidget(waitlabel);
69+
70+
// strechbox->addWidget(button);
71+
// strechbox->addStretch(100);
72+
73+
citybox->addWidget(site);
74+
citybox->addWidget(pm2);
75+
citybox->addWidget(datetime);
76+
hbox->addWidget(label);
77+
hbox->addWidget(linedit);
78+
hbox->addWidget(button);
79+
vbox->addLayout(hbox);
80+
// vbox->addWidget(button);
81+
vbox->addLayout(strechbox);
82+
vbox->addLayout(citybox);
83+
// vbox->addWidget(site);
84+
// vbox->addWidget(pm2);
85+
vbox->addWidget(combobox);
86+
vbox->addWidget(stack);
87+
vbox->addWidget(textedit);
88+
89+
manager = new QNetworkAccessManager(this);
90+
91+
92+
93+
this->setLayout(vbox);
94+
95+
connect(button,&QPushButton::clicked,this,&Widget::getjson);
96+
// connect(combobox,&QComboBox::activated,stack,&QStackedWidget::setCurrentIndex);
97+
connect(combobox, SIGNAL(activated(int)),
98+
stack, SLOT(setCurrentIndex(int)));
99+
showwidget();
100+
101+
timer = new QTimer;
102+
connect(timer, SIGNAL(timeout()), this, SLOT(showData()));
103+
picTimer = new QTimer;
104+
connect(picTimer,&QTimer::timeout,this,&Widget::showstack);
105+
106+
picTimer->setInterval(3000);
107+
108+
109+
}
110+
111+
Widget::~Widget()
112+
{
113+
114+
}
115+
void Widget::getjson()
116+
{
117+
process = new QProcess;
118+
QString processString = "python untitled-1.py "+ linedit->text();
119+
process->start(processString);
120+
//QProcess::execute("./untitled-1.py 北京");
121+
timer->start(3000);
122+
waitlabel->setText("请不要着急,天气小子为您加载数据... ... ...");
123+
picTimer->start();
124+
pixstackint = -1;
125+
}
126+
127+
void Widget::showData()
128+
{
129+
130+
waitlabel->setText(" ");
131+
QFile file("./weathers");
132+
if(!file.open(QIODevice::ReadOnly))
133+
return ;
134+
QTextStream in(&file);
135+
QString str = in.readAll();
136+
// textedit->setPlainText(str);
137+
QJsonParseError error;
138+
QJsonDocument jsonDocument = QJsonDocument::fromJson(str.toUtf8(),&error);
139+
QVariantMap result1 = jsonDocument.toVariant().toMap();
140+
city[0] = result1["date"].toString();
141+
foreach (QVariant variant1, result1["results"].toList()) {
142+
QVariantMap result2 = variant1.toMap();
143+
city[1] = result2["currentCity"].toString();
144+
city[2] = result2["pm25"].toString();
145+
site->setText(city[1]);
146+
pm2->setText("pm25: "+city[2]);
147+
datetime->setText(city[0]);
148+
int i = 0 ;
149+
foreach (QVariant variant2, result2["index"].toList()) {
150+
QVariantMap maps = variant2.toMap();
151+
indexstring[i][0] = maps["title"].toString();
152+
indexstring[i][1] = maps["zs"].toString();
153+
indexstring[i][2] = maps["tipt"].toString();
154+
indexstring[i][3] = maps["des"].toString();
155+
i++;
156+
}
157+
i = 0;
158+
foreach (QVariant variant3, result2["weather_data"].toList()) {
159+
QVariantMap mapss = variant3.toMap();
160+
weatherstring[i][0] = mapss["date"].toString();
161+
weatherstring[i][1] = mapss["dayPictureUrl"].toString();
162+
weatherstring[i][2] = mapss["nightPictureUrl"].toString();
163+
weatherstring[i][3] = mapss["weather"].toString();
164+
weatherstring[i][4] = mapss["wind"].toString();
165+
weatherstring[i][5] = mapss["temperature"].toString();
166+
i++;
167+
}
168+
}
169+
picNumber = 0;
170+
showTextedit();
171+
showPic();
172+
//downPix();
173+
}
174+
175+
void Widget::showTextedit()
176+
{
177+
QString indexweather;
178+
for(int i = 0;i< 6; i++)
179+
{
180+
for(int j = 0;j< 4; j++)
181+
{
182+
indexweather += indexstring[i][j];
183+
}
184+
indexweather += "\n";
185+
186+
}
187+
textedit->setText(indexweather);
188+
showinformation();
189+
}
190+
void Widget::showPic()
191+
{
192+
pixmap[0][0]->load("./pix/1.png");
193+
pixmap[0][1]->load("./pix/2.png");
194+
pixmap[1][0]->load("./pix/3.png");
195+
pixmap[1][1]->load("./pix/4.png");
196+
pixmap[2][0]->load("./pix/5.png");
197+
pixmap[2][1]->load("./pix/6.png");
198+
pixmap[3][0]->load("./pix/7.png");
199+
pixmap[3][1]->load("./pix/8.png");
200+
for(int i = 0;i< 4;i++)
201+
{
202+
for(int j=0;j<2;j++)
203+
{
204+
picLable[i][j]->setPixmap(*pixmap[i][j]);
205+
picLable[i][j]->setScaledContents(true);
206+
}
207+
}
208+
209+
}
210+
211+
void Widget::showwidget()
212+
{
213+
QVBoxLayout *stackbox[4];
214+
stackbox[0] = new QVBoxLayout;
215+
stackbox[1] = new QVBoxLayout;
216+
stackbox[2] = new QVBoxLayout;
217+
stackbox[3] = new QVBoxLayout;
218+
QHBoxLayout *picBox[4];
219+
220+
221+
/*for(int i = 0; i< 4;i++)
222+
{
223+
picBox[i] = new QHBoxLayout;
224+
picLable[i][0] = new QLabel;
225+
fileString = pointString + QString::number(j);
226+
j++;
227+
//pixmap[i][0] = new QPixmap(fileString);
228+
picLable[i][0]->setPixmap(pixmap[i][0]);
229+
picBox[i]->addWidget(picLable[i][0]);
230+
picLable[i][1] = new QLabel;
231+
picBox[i]->addWidget(picLable[i][1]);
232+
fileString = pointString + QString::number(j);
233+
j++;
234+
//pixmap[i][1] = new QPixmap(fileString);
235+
picLable[i][1]->setPixmap(pixmap[i][1]);
236+
stackbox[i]->addLayout(picBox[i]);
237+
}*/
238+
for(int i = 0; i< 4;i++)
239+
{
240+
picBox[i] = new QHBoxLayout;
241+
picLable[i][0] = new QLabel;
242+
picLable[i][1] = new QLabel;
243+
pixmap[i][0] = new QPixmap();
244+
pixmap[i][1] = new QPixmap();
245+
picBox[i]->addWidget(picLable[i][0]);
246+
picBox[i]->addWidget(picLable[i][1]);
247+
stackbox[i]->addLayout(picBox[i]);
248+
}
249+
250+
for(int j = 0;j < 4;j++)
251+
{
252+
linedits[0][j] = new QLineEdit(first);
253+
stackbox[0]->addWidget(linedits[0][j]);
254+
}
255+
for(int j = 0;j < 4;j++)
256+
{
257+
linedits[1][j] = new QLineEdit(second);
258+
stackbox[1]->addWidget(linedits[1][j]);
259+
}
260+
for(int j = 0;j < 4;j++)
261+
{
262+
linedits[2][j] = new QLineEdit(third);
263+
stackbox[2]->addWidget(linedits[2][j]);
264+
}
265+
for(int j = 0;j < 4;j++)
266+
{
267+
linedits[3][j] = new QLineEdit(four);
268+
stackbox[3]->addWidget(linedits[3][j]);
269+
}
270+
271+
first->setLayout(stackbox[0]);
272+
second->setLayout(stackbox[1]);
273+
third->setLayout(stackbox[2]);
274+
four->setLayout(stackbox[3]);
275+
276+
}
277+
void Widget::showinformation()
278+
{
279+
linedits[0][0]->setText(weatherstring[0][0]);
280+
linedits[1][0]->setText(weatherstring[1][0]);
281+
linedits[2][0]->setText(weatherstring[2][0]);
282+
linedits[3][0]->setText(weatherstring[3][0]);
283+
for(int i=0;i<4;i++)
284+
{
285+
linedits[i][1]->setText(weatherstring[i][3]);
286+
linedits[i][2]->setText(weatherstring[i][4]);
287+
linedits[i][3]->setText(weatherstring[i][5]);
288+
}
289+
290+
}
291+
void Widget::showstack()
292+
{
293+
294+
pixstackint = combobox->currentIndex();
295+
296+
pixstackint++;
297+
298+
if(pixstackint == 4)
299+
{
300+
pixstackint = 0;
301+
}
302+
combobox->setCurrentIndex(pixstackint);
303+
stack->setCurrentIndex(pixstackint);
304+
305+
}
306+
307+
308+
309+
310+
311+
312+
313+
314+
315+
316+
317+
318+
319+
320+
321+
322+

0 commit comments

Comments
 (0)