|
| 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