main 函数
#include "widget.h"
#include<QFile>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile qss(":/style/main.qss");
qss.open(QFile::ReadOnly);
a.setStyleSheet(qss.readAll());
qss.close();
Widget w;
w.show();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include<QtCharts>
#include <QWidget>
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include<QTimer>
#include<QCoreApplication>
using namespace QtCharts;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void loadStyleSheet(const QString &sheetName);//加载样式文件
void createChart();//创建图表
void setChartValue(QJsonArray &obj);//给图表设值
void httprequest();
void showData();
void showDetail(QJsonArray &array);
void chinaToday(QJsonObject &obj);
void chinaTotal(QJsonObject &obj);
public slots:
void startRequest();
void replyFinished(QNetworkReply*);
private:
Ui::Widget *ui;
QNetworkAccessManager *manager;
QNetworkReply *pReply;
QTimer *startTimer;
QNetworkRequest request;
QString path=QCoreApplication::applicationDirPath();
};
#endif // WIDGET_H
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include<QFile>
#include<QNetworkAccessManager>
#include<QNetworkCookie>
#include<QNetworkReply>
#include<QSslConfiguration>
#include<QJsonDocument>
#include<iostream>
#include<string>
#include<QDebug>
using namespace std;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle("新冠肺炎实时数据");
createChart();//创建图表
manager = new QNetworkAccessManager(this);//新建网络管理类
startTimer=new QTimer(this);
connect(startTimer,SIGNAL(timeout()),this,SLOT(startRequest()));
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));//关联信号和槽
QSslConfiguration conf = request.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(conf);
QString url="https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=1581743590402";
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
request.setUrl(QUrl(url));
startTimer->start(10000);
showData();
}
void Widget::createChart()
{
QChart *chart = new QChart();
QFont f;
f.setPointSize(12);
f.setBold(1);
chart->setTitleFont(f);
chart->setTitleBrush(QBrush(QColor(89,168,227)));
chart->setAnimationOptions(QChart::SeriesAnimations);
chart->legend()->setVisible(false);
ui->chartView->setRenderHint(QPainter::Antialiasing);
QBrush brush;
brush.setColor(QColor(1,33,56));
chart->setBackgroundBrush(brush);
brush.setColor(Qt::white);
chart->setAnimationOptions(QChart::NoAnimation);
QFont font;
font.setBold(true);
font.setFamily("方正粗黑宋简体");
font.setPointSize(8);
QBrush brushs;
brushs.setColor(Qt::white);
QBarCategoryAxis *axisX=new QBarCategoryAxis();
QValueAxis *axisY=new QValueAxis();
axisY->setTitleText("人数");
axisX->setLabelsFont(font);
axisX->setLabelsColor(Qt::white);
axisX->setGridLineColor(QColor(13,50,77));
axisX->setTitleText("日期");
axisX->setTitleBrush(brushs);
font.setPointSize(12);
axisX->setTitleFont(font);
axisX->setGridLineVisible(false);
font.setPointSize(8);
axisY->setLabelsFont(font);
axisY->setLabelsColor(Qt::white);
axisY->setGridLineVisible(false);
axisY->setTitleText("数目");
font.setPointSize(12);
axisY->setTitleFont(font);
axisY->setTitleBrush(brushs);
chart->addAxis(axisX,Qt::AlignBottom);
chart->addAxis(axisY,Qt::AlignLeft);
ui->chartView->setChart(chart);
}
void Widget::setChartValue(QJsonArray &obj)
{
//解析数据
QStringList axisxList=QStringList();
QList<QPointF> confirmList,deadList,healList,suspectList;\
QList<int> yu;
int i=0;
for(auto var:obj)
{
QJsonObject subObj=var.toObject();
QString axisX=subObj["date"].toString().mid(5,5);
axisxList.append(axisX);
int confirm= subObj["today"].toObject()["confirm"].toInt();
int dead= subObj["today"].toObject()["dead"].toInt();
int heal= subObj["today"].toObject()["heal"].toInt();
int suspect= subObj["today"].toObject()["suspect"].toInt();
yu.append(confirm);
QPoint point;
point.setX(i);
point.setY(confirm);
confirmList.append(point);
point.setY(dead);
deadList.append(point);
point.setY(heal);
healList.append(point);
point.setY(suspect);
suspectList.append(point);
i++;
}
QList<QAbstractAxis *> axes=ui->chartView->chart()->axes();
QBarCategoryAxis*axisX= qobject_cast<QBarCategoryAxis*>(axes.at(0));
axisX->append(axisxList);
QValueAxis*axisY= qobject_cast<QValueAxis*>(axes.at(1));
axisY->setRange(0,15000);
ui->chartView->chart()->removeAllSeries();
QLineSeries *seriesconfirm=new QLineSeries();
seriesconfirm->setName("确诊");
ui->chartView->chart()->addSeries(seriesconfirm);
seriesconfirm->append(confirmList);
seriesconfirm->attachAxis(axisX);
seriesconfirm->attachAxis(axisY);
QLineSeries *seriesdead=new QLineSeries();
seriesdead->setName("死亡");
ui->chartView->chart()->addSeries(seriesdead);
seriesdead->append(deadList);
seriesdead->attachAxis(axisX);
seriesdead->attachAxis(axisY);
QLineSeries *seriesheal=new QLineSeries();
seriesheal->setName("治愈");
ui->chartView->chart()->addSeries(seriesheal);
seriesheal->append(healList);
seriesheal->attachAxis(axisX);
seriesheal->attachAxis(axisY);
QLineSeries *seriessuspect=new QLineSeries();
seriessuspect->setName("疑似");
ui->chartView->chart()->addSeries(seriessuspect);
seriessuspect->append(suspectList);
seriessuspect->attachAxis(axisX);
seriessuspect->attachAxis(axisY);
ui->chartView->chart()->legend()->setVisible(true);
}
void Widget::startRequest()
{
/*发送get网络请求*/
manager->get(request);
}
void Widget::replyFinished(QNetworkReply *reply)
{
if(reply->error()== QNetworkReply::NoError)
{
qDebug("No Error");
QByteArray array= reply->readAll();
QString string =QString::fromUtf8(array);
QJsonObject l_ret;
QJsonParseError l_err;
QJsonDocument l_doc = QJsonDocument::fromJson(string.toUtf8(), &l_err);
QString filePath=path+"/Data.json";
QFile file(filePath);
file.open(QIODevice::WriteOnly);
file.write(l_doc.toJson());
file.close();
}
showData();
}
Widget::~Widget()
{
delete ui;
}
void Widget::loadStyleSheet(const QString &sheetName)
{
QFile file(sheetName);
file.open(QFile::ReadOnly);
if (file.isOpen())
{
QString styleSheet = this->styleSheet();
styleSheet += QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);
}
}
void Widget::showDetail(QJsonArray &array)
{
QJsonArray chinaObj;
for( auto var:array)
{
if(var.toObject()["id"].toInt()==0)
{
chinaObj=var.toObject()["children"].toArray();
break;
}
}
QList<QTreeWidgetItem*> itemlist;
for(auto var:chinaObj)
{
QString name=var.toObject()["name"].toString();
int addNew=var.toObject()["today"].toObject()["confirm"].toInt();
int totalConfirm=var.toObject()["total"].toObject()["confirm"].toInt();
int death=var.toObject()["total"].toObject()["dead"].toInt();
int heal=var.toObject()["total"].toObject()["heal"].toInt();
QTreeWidgetItem *item=new QTreeWidgetItem;
item->setText(0,name);
item->setText(1,QString::number(addNew));
item->setText(2,QString::number(totalConfirm));
item->setText(3,QString::number(death));
item->setText(4,QString::number(heal));
QJsonArray subChildren=var.toObject()["children"].toArray();
QList<QTreeWidgetItem*> list1;
for(auto var:subChildren)
{
QString name1=var.toObject()["name"].toString();
int addNew1=var.toObject()["today"].toObject()["confirm"].toInt();
int totalConfirm1=var.toObject()["total"].toObject()["confirm"].toInt();
int death1=var.toObject()["total"].toObject()["dead"].toInt();
int heal1=var.toObject()["total"].toObject()["heal"].toInt();
QTreeWidgetItem *items=new QTreeWidgetItem;
items->setText(0,name1);
items->setText(1,QString::number(addNew1));
items->setText(2,QString::number(totalConfirm1));
items->setText(3,QString::number(death1));
items->setText(4,QString::number(heal1));
list1.append(items);
}
item->addChildren(list1);
itemlist.append(item);
}
ui->treeWidget->clear();
ui->treeWidget->addTopLevelItems(itemlist);
}
void Widget::showData()
{
QString filePath=path+"/Data.json";
QFile file(filePath);
if(!file.open(QFile::ReadOnly))
{
qDebug("打开文件失败");
return;
}
QByteArray allData = file.readAll();
file.close();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
if(json_error.error != QJsonParseError::NoError)
{
qDebug() << "json error!";
return;
}
QJsonObject rootObj = jsonDoc.object();
//今日数据
QJsonObject todayObj=rootObj["data"].toObject()["chinaTotal"].toObject()["today"].toObject();
chinaToday(todayObj);
//累计数据
QJsonObject totalObj=rootObj["data"].toObject()["chinaTotal"].toObject()["total"].toObject();
chinaTotal(totalObj);
//数据截至时间
QString timeStr=rootObj["data"].toObject()["lastUpdateTime"].toString();
ui->timeLabel->clear();
ui->timeLabel->setText("<html><head/><body><p><span style=\" font-size:12pt; font-weight:600; color:#a1a1a1;\">统计截止于:</span><span style=\" font-family:'Consolas','Courier New','monospace'; font-size:12pt; font-weight:600; color:#989898;\">"+timeStr+"</span></p></body></html>");
//给图表设置
QJsonArray chinaDayListObj=rootObj["data"].toObject()["chinaDayList"].toArray();
setChartValue(chinaDayListObj);
//各省份列表
QJsonArray areaTree=rootObj["data"].toObject()["areaTree"].toArray();
showDetail(areaTree);
}
void Widget::chinaTotal(QJsonObject &obj)
{
int confirm=obj["confirm"].toInt();
int dead=obj["dead"].toInt();
int heal=obj["heal"].toInt();
int suspect=obj["suspect"].toInt();
ui->tconfirmLabel->clear();
ui->tdeadLabel->clear();
ui->thealLabel->clear();
ui->tsuspectLabel->clear();
ui->tdeadLabel->setText("<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; \">"+QString::number(dead)+"</span></p></body></html>");
ui->tconfirmLabel->setText("<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; color:#e32e09;\">"+QString::number(confirm)+"</span></p></body></html>");
ui->thealLabel->setText("<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; color:#2faa77;\">"+QString::number(heal)+"</span></p></body></html>");
ui->tsuspectLabel->setText("<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; color:#ffaa00;\">"+QString::number(suspect)+"</span></p></body></html>");
}
void Widget::chinaToday(QJsonObject &obj)
{
int confirm=obj["confirm"].toInt();
int dead=obj["dead"].toInt();
int heal=obj["heal"].toInt();
int suspect=obj["suspect"].toInt();
ui->confirmLabel->clear();
ui->deadLabel->clear();
ui->healLabel->clear();
ui->suspectLabel->clear();
ui->confirmLabel->setText("<html><head/><body><p align=\"center\">较昨日<span style=\" font-size:12pt; font-weight:600; color:#ff0000;\">+"+QString::number(confirm)+"</span></p></body></html>");
ui->deadLabel->setText("<html><head/><body><p align=\"center\">较昨日<span style=\" font-size:12pt; font-weight:600; \">+"+QString::number(dead)+"</span></p></body></html>");
ui->healLabel->setText("<html><head/><body><p align=\"center\">较昨日<span style=\" font-size:12pt; font-weight:600; color:#0036a2;\">+"+QString::number(heal)+"</span></p></body></html>");
ui->suspectLabel->setText("<html><head/><body><p align=\"center\">较昨日<span style=\" font-size:12pt; font-weight:600; color:#ffce6b;\">+"+QString::number(suspect)+"</span></p></body></html>");
}
//widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>886</width>
<height>640</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="3" column="0">
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QLabel" name="label_15">
<property name="minimumSize">
<size>
<width>30</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>全国新增趋势图</string>
</property>
</widget>
</item>
<item>
<widget class="QChartView" name="chartView" native="true"/>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labelPic">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>100</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="timeLabel">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>50</height>
</size>
</property>
<property name="font">
<font>
<family>Calibri</family>
</font>
</property>
<property name="text">
<string><html><head/><body><p><span style=" font-size:12pt; font-weight:600; color:#a1a1a1;">统计截止于:</span><span style=" font-family:'Consolas','Courier New','monospace'; font-size:12pt; font-weight:600; color:#989898;">2020-02-15 15:45:35</span></p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="4">
<widget class="QTreeWidget" name="treeWidget">
<property name="minimumSize">
<size>
<width>450</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>450</width>
<height>16777215</height>
</size>
</property>
<column>
<property name="text">
<string>地区</string>
</property>
</column>
<column>
<property name="text">
<string>新增确诊</string>
</property>
</column>
<column>
<property name="text">
<string>确诊</string>
</property>
</column>
<column>
<property name="text">
<string>死亡</string>
</property>
</column>
<column>
<property name="text">
<string>治愈</string>
</property>
</column>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="confirmLabel">
<property name="text">
<string><html><head/><body><p align="center">较昨日<span style=" font-size:12pt; font-weight:600; color:#ff0000;">+123</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tconfirmLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 255, 0);</string>
</property>
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:12pt; font-weight:600; color:#e32e09;">7990</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:10pt; font-weight:600;">确诊</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="suspectLabel">
<property name="text">
<string><html><head/><body><p align="center">较昨日<span style=" font-size:12pt; font-weight:600; color:#ffce6b;">+123</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tsuspectLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 255, 0);</string>
</property>
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:12pt; font-weight:600; color:#ffaa00;">68799</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:10pt; font-weight:600;">疑似</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="2">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="deadLabel">
<property name="text">
<string><html><head/><body><p align="center">较昨日<span style=" font-size:12pt; font-weight:600;">+123</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tdeadLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 255, 0);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="tdeadLabels">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:10pt; font-weight:600;">死亡</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="3">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="healLabel">
<property name="text">
<string><html><head/><body><p align="center">较昨日<span style=" font-size:12pt; font-weight:600; color:#0036a2;">+123</span></p></body></html></string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="thealLabel">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(170, 255, 0);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="thealLabelq">
<property name="text">
<string><html><head/><body><p align="center"><span style=" font-size:10pt; font-weight:600;">治愈</span></p></body></html></string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QChartView</class>
<extends>QWidget</extends>
<header>qchartview.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
样式表
*{
background-color:rgba(200,200,100,100);
border-color:rgba(5,39,175,100);
}
QLabel#labelPic
{
background-color: rgb(255, 255, 0);
}
](https://img-blog.csdnimg.cn/20200216100309835.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjIwMjY3NA==,size_16,color_FFFFFF,t_70)
新冠肺炎实时软件源码
最新推荐文章于 2021-04-27 10:08:05 发布