feat:统计界面

This commit is contained in:
徐超 2024-11-05 17:48:05 +08:00
parent d5fb5c64dd
commit ac69d356e8
9 changed files with 620 additions and 363 deletions

View File

@ -9,7 +9,7 @@ QLable#label_splite{font: bold 14px;}
QWidget#widgetTitle{background-color:#061f65;} QWidget#widgetTitle{background-color:#061f65;}
QWidget#baseWidget{background-color:#EE013B6B;border-radius: 11px;} QWidget#baseWidget{background-color:#EE013B6B;border-radius: 11px;}
QComboBox{background-color: transparent; selection-background-color: transparent; color:#ffffff; font-size: 14px; min-height: 20px; selection-text-align: right;border:0px solid} QComboBox{background-color: transparent; selection-background-color: transparent; color:#ffffff; font-size: 14px; min-height: 20px; border:0px solid}
QComboBox::down-arrow{ image:url(:/component/ZDateEdit/Resources/ComboBoxArrow.png); } QComboBox::down-arrow{ image:url(:/component/ZDateEdit/Resources/ComboBoxArrow.png); }
QComboBox::down-arrow:on{ top: 1px; left: 1px; } QComboBox::drop-down{ width: 26px; border:none; } QComboBox::down-arrow:on{ top: 1px; left: 1px; } QComboBox::drop-down{ width: 26px; border:none; }
QComboBox QAbstractItemView {border: none; outline:0px;} QComboBox QAbstractItemView::item{ height: 26px; min-height:26px;} QComboBox QAbstractItemView {border: none; outline:0px;} QComboBox QAbstractItemView::item{ height: 26px; min-height:26px;}

View File

@ -8,14 +8,21 @@
#include <QTimer> #include <QTimer>
MainWindow::MainWindow(QWidget* parent) MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow) { : QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint); this->setWindowFlag(Qt::FramelessWindowHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
Qt::WindowMinimizeButtonHint);
ui->widget_head->installEventFilter(this);
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
calendarLoopPlay(); calendarLoopPlay();
setSystemTitle();
setBeltWarnInfoTextEditStyle(); setBeltWarnInfoTextEditStyle();
addcoalDistributChart(); addcoalDistributChart();
@ -29,7 +36,39 @@ MainWindow::MainWindow(QWidget *parent)
MainWindow::~MainWindow() { delete ui; } MainWindow::~MainWindow() { delete ui; }
void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) { bool MainWindow::eventFilter(QObject* watched, QEvent* event)
{
// 处理鼠标按下、释放和移动事件
if (watched == ui->widget_head) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->type() == QEvent::MouseButtonPress) {
if (mouseEvent->button() == Qt::LeftButton) {
m_mousePressed = true; // 启动拖拽
m_mousePoint = mouseEvent->globalPos() - this->pos();
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint);
this->show(); // 确保窗口显示
return true;
}
} else if (mouseEvent->type() == QEvent::MouseButtonRelease) {
m_mousePressed = false;
return true;
} else if (mouseEvent->type() == QEvent::MouseMove) {
if (m_mousePressed && (mouseEvent->buttons() & Qt::LeftButton)) {
this->move(mouseEvent->globalPos() - m_mousePoint);
// 每次移动时设置为最上方
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint);
this->show();
return true;
}
}
}
}
void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo)
{
m_textEditConsoleInfo.append(consoleInfo); m_textEditConsoleInfo.append(consoleInfo);
QDateTime startDateTime = QDateTime::currentDateTime(); QDateTime startDateTime = QDateTime::currentDateTime();
QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss"); QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss");
@ -38,14 +77,12 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) {
return; return;
QString lastConsoleInfo = m_textEditConsoleInfo.last(); QString lastConsoleInfo = m_textEditConsoleInfo.last();
qint64 timeDifferenceInMilliseconds = qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime);
m_previousConsleInfoDateTime.secsTo(startDateTime);
if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2)
return; return;
QString consoleInfoResult = QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo);
ui->textEdit_beltWarnInfo->append(consoleInfoResult); ui->textEdit_beltWarnInfo->append(consoleInfoResult);
m_previousConsleInfoDateTime = QDateTime::currentDateTime(); m_previousConsleInfoDateTime = QDateTime::currentDateTime();
@ -53,7 +90,8 @@ void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) {
void MainWindow::on_pushButton_close_clicked() { this->close(); } void MainWindow::on_pushButton_close_clicked() { this->close(); }
void MainWindow::on_pushButton_max_clicked() { void MainWindow::on_pushButton_max_clicked()
{
if (isFullScreen()) { if (isFullScreen()) {
this->showMaximized(); this->showMaximized();
} else { } else {
@ -63,7 +101,8 @@ void MainWindow::on_pushButton_max_clicked() {
void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); } void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }
void MainWindow::calendarLoopPlay() { void MainWindow::calendarLoopPlay()
{
ui->label_calendar->clear(); ui->label_calendar->clear();
QTimer* timer_calendar = new QTimer(this); QTimer* timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, [this]() { connect(timer_calendar, &QTimer::timeout, this, [this]() {
@ -74,18 +113,28 @@ void MainWindow::calendarLoopPlay() {
timer_calendar->start(1000); //每一秒溢出一次进入槽函数 timer_calendar->start(1000); //每一秒溢出一次进入槽函数
} }
void MainWindow::setBeltWarnInfoTextEditStyle() { void MainWindow::setSystemTitle()
{
QString titleName = m_systemSetting->value("system/name").toString();
if (!titleName.isEmpty()) {
ui->label_title->clear();
ui->label_title->setText(titleName);
}
}
void MainWindow::setBeltWarnInfoTextEditStyle()
{
ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100); ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100);
ui->textEdit_beltWarnInfo->setReadOnly(true); ui->textEdit_beltWarnInfo->setReadOnly(true);
ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction); ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction);
} }
void MainWindow::addcoalDistributChart() { void MainWindow::addcoalDistributChart()
{
// addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!"); // addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!");
ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!"); ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!");
CoalDistributBarChartWidget *coalDistributBarChartWidget = CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this);
new CoalDistributBarChartWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
if (!ui->widget_coalDistributChart->layout()) { if (!ui->widget_coalDistributChart->layout()) {
@ -96,7 +145,8 @@ void MainWindow::addcoalDistributChart() {
coalDistributBarChartWidget); coalDistributBarChartWidget);
} }
void MainWindow::addbeltSpeedChat() { void MainWindow::addbeltSpeedChat()
{
ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!"); ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!");
BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this); BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
@ -107,10 +157,10 @@ void MainWindow::addbeltSpeedChat() {
ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget); ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget);
} }
void MainWindow::addCoalStatisticsChart() { void MainWindow::addCoalStatisticsChart()
{
ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!"); ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!");
CoalStatisticsChartWidget *coalStatisticsChartWidget = CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this);
new CoalStatisticsChartWidget(this);
// 检查布局是否已设置 // 检查布局是否已设置
if (!ui->widget_beltSpeedChart->layout()) { if (!ui->widget_beltSpeedChart->layout()) {
ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); ui->widget_beltSpeedChart->setLayout(new QVBoxLayout);
@ -120,9 +170,12 @@ void MainWindow::addCoalStatisticsChart() {
coalStatisticsChartWidget); coalStatisticsChartWidget);
} }
void MainWindow::addCoalBeltVideo() { void MainWindow::addCoalBeltVideo()
QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/" {
"20200218114723HDu3hhxqIT.mp4"; // QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/"
// "20200218114723HDu3hhxqIT.mp4";
// QString url = "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101";
QString url = m_systemSetting->value("camera/rtsp").toString();
ui->widget_beltVideo->setUrl(url); ui->widget_beltVideo->setUrl(url);
ui->widget_beltVideo->open(); ui->widget_beltVideo->open();
} }

View File

@ -3,6 +3,7 @@
#include <QDateTime> #include <QDateTime>
#include <QMainWindow> #include <QMainWindow>
#include <QSettings>
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -15,6 +16,9 @@ public:
explicit MainWindow(QWidget* parent = nullptr); explicit MainWindow(QWidget* parent = nullptr);
~MainWindow(); ~MainWindow();
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
public slots: public slots:
/** /**
* @brief * @brief
@ -35,6 +39,11 @@ private:
*/ */
void calendarLoopPlay(); void calendarLoopPlay();
/**
* @brief setSystemTitle
*/
void setSystemTitle();
/** /**
* @brief setBeltWarnInfoTextEditStyle * @brief setBeltWarnInfoTextEditStyle
*/ */
@ -72,6 +81,15 @@ private:
* @brief m_previousConsleInfoDateTime * @brief m_previousConsleInfoDateTime
*/ */
QDateTime m_previousConsleInfoDateTime; QDateTime m_previousConsleInfoDateTime;
/**
* @brief m_systemSetting
*/
QSettings* m_systemSetting;
// 窗口拖拽
QPoint m_mousePoint;
bool m_mousePressed { false };
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -14,7 +14,7 @@
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset resource="resource.qrc"> <iconset>
<normaloff>:/Resource/image/logo.png</normaloff>:/Resource/image/logo.png</iconset> <normaloff>:/Resource/image/logo.png</normaloff>:/Resource/image/logo.png</iconset>
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
@ -47,16 +47,15 @@
</property> </property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">#widget_head{ <string notr="true">#widget_head{
border-image: url(:/Resource/image/title.png); border-image: url(:/Resource/image/title.png);
}</string> }</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="2,4,4,4,0"> <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="2,3,4,3,0">
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
</property> </property>
<property name="leftMargin"> <property name="leftMargin">
<number>0</number> <number>9</number>
</property> </property>
<property name="rightMargin"> <property name="rightMargin">
<number>0</number> <number>0</number>
@ -77,6 +76,8 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_56">
<item> <item>
<widget class="QLabel" name="label_title"> <widget class="QLabel" name="label_title">
<property name="styleSheet"> <property name="styleSheet">
@ -90,6 +91,8 @@
</property> </property>
</widget> </widget>
</item> </item>
</layout>
</item>
<item> <item>
<spacer name="horizontalSpacer_2"> <spacer name="horizontalSpacer_2">
<property name="orientation"> <property name="orientation">
@ -103,6 +106,8 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_57">
<item> <item>
<widget class="QLabel" name="label_calendar"> <widget class="QLabel" name="label_calendar">
<property name="minimumSize"> <property name="minimumSize">
@ -125,6 +130,8 @@
</property> </property>
</widget> </widget>
</item> </item>
</layout>
</item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_12"> <layout class="QHBoxLayout" name="horizontalLayout_12">
<property name="spacing"> <property name="spacing">
@ -3293,8 +3300,6 @@ QPushButton:pressed{
<container>1</container> <container>1</container>
</customwidget> </customwidget>
</customwidgets> </customwidgets>
<resources> <resources/>
<include location="resource.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

View File

@ -20,15 +20,20 @@
#include <memory> #include <memory>
CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget* parent) CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget* parent)
: QWidget(parent), m_tcpSocket(new QTcpSocket(this)) { : QWidget(parent)
, m_tcpSocket(new QTcpSocket(this))
{
initializeChart(); initializeChart();
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
connectTCPSocket(); connectTCPSocket();
setupTimer(); setupTimer();
} }
CoalDistributBarChartWidget::~CoalDistributBarChartWidget() { CoalDistributBarChartWidget::~CoalDistributBarChartWidget()
{
if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) { if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) {
m_tcpSocket->disconnectFromHost(); m_tcpSocket->disconnectFromHost();
m_tcpSocket->waitForDisconnected(); m_tcpSocket->waitForDisconnected();
@ -36,21 +41,21 @@ CoalDistributBarChartWidget::~CoalDistributBarChartWidget() {
delete m_tcpSocket; delete m_tcpSocket;
} }
void CoalDistributBarChartWidget::updateChart() { void CoalDistributBarChartWidget::updateChart()
{
int radio = getCoalRatioByTCPSocket(); int radio = getCoalRatioByTCPSocket();
// if (radio <= 0 || radio > 100) { if (radio <= 0 || radio > 100) {
// return; return;
// } }
int setCount = m_set->count(); int setCount = m_set->count();
qDebug() << "setCount" << setCount;
m_set->remove(setCount - 1); m_set->remove(setCount - 1);
m_set->insert(0, radio); m_set->insert(0, radio);
} }
void CoalDistributBarChartWidget::initializeChart() { void CoalDistributBarChartWidget::initializeChart()
{
// 创建图标 // 创建图标
m_chart = new QChart(); m_chart = new QChart();
@ -110,7 +115,8 @@ void CoalDistributBarChartWidget::initializeChart() {
setLayout(layout); setLayout(layout);
} }
void CoalDistributBarChartWidget::setupTimer() { void CoalDistributBarChartWidget::setupTimer()
{
// 定时器设置 // 定时器设置
m_timer = new QTimer(this); m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, connect(m_timer, &QTimer::timeout, this,
@ -118,9 +124,10 @@ void CoalDistributBarChartWidget::setupTimer() {
m_timer->start(1000); // 每秒更新一次 m_timer->start(1000); // 每秒更新一次
} }
void CoalDistributBarChartWidget::connectTCPSocket() { void CoalDistributBarChartWidget::connectTCPSocket()
QString ipAddress = "192.168.80.101"; // 替换为实际 IP 地址 {
quint16 port = 3000; // Modbus TCP 端口 QString ipAddress = m_systemSetting->value("addr/ip").toString();
quint16 port = m_systemSetting->value("addr/port").toInt();
// 连接到 Modbus TCP 服务器 // 连接到 Modbus TCP 服务器
m_tcpSocket->connectToHost(ipAddress, port); m_tcpSocket->connectToHost(ipAddress, port);
@ -129,7 +136,8 @@ void CoalDistributBarChartWidget::connectTCPSocket() {
} }
} }
int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() { int CoalDistributBarChartWidget::getCoalRatioByTCPSocket()
{
if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) { if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) {
qDebug() << "未连接到服务器"; qDebug() << "未连接到服务器";
return -1; // 返回错误值 return -1; // 返回错误值
@ -175,8 +183,6 @@ int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() {
return -1; // 返回错误值 return -1; // 返回错误值
} }
qDebug() << "response" << response.toHex();
// 解析响应 // 解析响应
QDataStream responseStream(response); QDataStream responseStream(response);
responseStream.setByteOrder(QDataStream::LittleEndian); responseStream.setByteOrder(QDataStream::LittleEndian);
@ -208,12 +214,12 @@ int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() {
bool isOk; bool isOk;
int decimalValue = lastFourBytes.toHex().toInt(&isOk, 16); int decimalValue = lastFourBytes.toHex().toInt(&isOk, 16);
qDebug() << "最后四位:" << decimalValue;
return decimalValue; // 返回煤量占比 return decimalValue; // 返回煤量占比
} }
QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() { QByteArray CoalDistributBarChartWidget::createModbusTcpRequest()
{
QByteArray request; QByteArray request;
QDataStream stream(&request, QIODevice::WriteOnly); QDataStream stream(&request, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序

View File

@ -3,6 +3,7 @@
#include <QDateTime> #include <QDateTime>
#include <QObject> #include <QObject>
#include <QSettings>
#include <QTimer> #include <QTimer>
#include <QWidget> #include <QWidget>
@ -58,13 +59,9 @@ private:
QBarSet* m_set; QBarSet* m_set;
int currentIndex; // 当前更新的柱子的索引
static const int MaxDataPoints = 300; // 最大数据点数5分钟每秒1条
QList<QDateTime> m_timeLabels; // 存储时间标签
QTcpSocket* m_tcpSocket; QTcpSocket* m_tcpSocket;
QSettings* m_systemSetting;
}; };
#endif // COALDISTRIBUTBARCHARTWIDGET_H #endif // COALDISTRIBUTBARCHARTWIDGET_H

View File

@ -1,6 +1,7 @@
#include "coalstatisticschartwidget.h" #include "coalstatisticschartwidget.h"
#include "ui_coalstatisticschartwidget.h" #include "ui_coalstatisticschartwidget.h"
#include <QHttpPart>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
@ -15,6 +16,8 @@ CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent)
{ {
ui->setupUi(this); ui->setupUi(this);
m_systemSetting = new QSettings("system.ini", QSettings::IniFormat);
initCoalStatisticsBarChart(); initCoalStatisticsBarChart();
getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08"); getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08");
@ -25,6 +28,104 @@ CoalStatisticsChartWidget::~CoalStatisticsChartWidget()
delete ui; delete ui;
} }
void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked()
{
// 时间范围
QString beginTimeStr = ui->dateTimeEdit_startTime->text();
QString endTimeStr = ui->dateTimeEdit_endTime->text();
qDebug() << "beginTimeStr" << beginTimeStr;
qDebug() << "endTimeStr" << endTimeStr;
// 创建 QDateTime 对象
QDateTime beginTime = QDateTime::fromString(beginTimeStr, "yyyy/MM/d HH:mm");
QDateTime endTime = QDateTime::fromString(endTimeStr, "yyyy/MM/d HH:mm");
// 判断是否转换成功
if (!beginTime.isValid() || !endTime.isValid()) {
qDebug() << "Invalid date/time format!";
return;
}
int unitIndex = ui->comboBox_unit->currentIndex();
// 选择的时间单位
switch (unitIndex) {
case TimeUnit::Hour: {
// 计算整时
QList<QDateTime> dateTimes;
int minutes = beginTime.time().minute(); // 获取分钟部分
beginTime = beginTime.addSecs(-minutes * 60); // 向下取整
// 遍历时间范围
while (beginTime <= endTime) {
dateTimes.append(beginTime);
beginTime = beginTime.addSecs(3600); // 增加1小时
}
// 输出结果
for (const QDateTime& dateTime : dateTimes) {
qDebug() << "整小时数" << dateTime.toString("yyyy/MM/d HH:mm");
}
break;
}
case TimeUnit::Day: {
// 计算整天
QList<QDateTime> dateTimes;
beginTime.setTime(QTime(0, 0)); // 将起始时间设置为当天的00:00:00
// 遍历时间范围
while (beginTime <= endTime) {
dateTimes.append(beginTime);
beginTime = beginTime.addDays(1); // 增加1天
}
// 输出结果
for (const QDateTime& dateTime : dateTimes) {
qDebug() << "整天数" << dateTime.toString("yyyy/MM/d");
}
break;
}
case TimeUnit::Month: {
// 计算整月
QList<QDateTime> dateTimes;
beginTime.setDate(QDate(beginTime.date().year(), beginTime.date().month(), 1)); // 设置为当月的1号
// 遍历时间范围
while (beginTime <= endTime) {
dateTimes.append(beginTime);
beginTime = beginTime.addMonths(1); // 增加1个月
}
// 输出结果
for (const QDateTime& dateTime : dateTimes) {
qDebug() << "整月数" << dateTime.toString("yyyy/MM");
}
break;
}
case TimeUnit::Year: {
// 计算整年
QList<QDateTime> dateTimes;
beginTime.setDate(QDate(beginTime.date().year(), 1, 1)); // 设置为当年的1月1日
// 遍历时间范围
while (beginTime <= endTime) {
dateTimes.append(beginTime);
beginTime = beginTime.addYears(1); // 增加1年
}
// 输出结果
for (const QDateTime& dateTime : dateTimes) {
qDebug() << "整年数" << dateTime.toString("yyyy");
}
break;
}
default:
qDebug() << "无效的时间单位!";
break;
}
}
void CoalStatisticsChartWidget::initCoalStatisticsBarChart() void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
{ {
// 创建图表 // 创建图表
@ -45,29 +146,35 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
m_chart->addSeries(m_series); m_chart->addSeries(m_series);
// 设置X轴为时间 // 设置X轴为时间
QValueAxis* axisX = new QValueAxis; m_axisX = new QDateTimeAxis;
axisX->setRange(1, 300); m_axisX->setFormat("yyyy/MM/d H"); // 设置日期时间格式
axisX->setTickCount(10); // m_axisX->setTitleText("时间");
axisX->setLabelFormat("%d"); m_axisX->setTitleBrush(QBrush(Qt::gray));
axisX->setLinePen(QPen(Qt::gray)); m_axisX->setLinePen(QPen(Qt::gray));
axisX->setLabelsColor(Qt::gray); m_axisX->setLabelsColor(Qt::gray);
axisX->setMinorTickCount(0);
m_chart->addAxis(axisX, Qt::AlignBottom); // 获取当前时间并设置时间范围为最近六个小时
m_series->attachAxis(axisX); QDateTime currentTime = QDateTime::currentDateTime();
QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间减去六个小时
// m_axisX->setLabelsAngle(45);
// 设置X轴时间范围
m_axisX->setRange(startTime, currentTime);
m_chart->addAxis(m_axisX, Qt::AlignBottom);
m_series->attachAxis(m_axisX);
// 设置Y轴为百分比 // 设置Y轴为百分比
QValueAxis* axisY = new QValueAxis; m_axisY = new QValueAxis;
axisY->setTitleText(""); m_axisY->setTitleText("");
axisY->setTitleBrush(QBrush(Qt::gray)); m_axisY->setTitleBrush(QBrush(Qt::gray));
axisY->setLinePen(QPen(Qt::gray)); m_axisY->setLinePen(QPen(Qt::gray));
axisY->setLabelsColor(Qt::gray); m_axisY->setLabelsColor(Qt::gray);
axisY->setRange(0, 100); m_axisY->setRange(0, 100);
m_chart->addAxis(axisY, Qt::AlignLeft); m_chart->addAxis(m_axisY, Qt::AlignLeft);
m_series->attachAxis(axisY); m_series->attachAxis(m_axisY);
m_axisY->setTitleFont(QFont("Arial", 10));
// 设置Y轴标题位置 m_axisY->setTitleVisible(true);
axisY->setTitleFont(QFont("Arial", 10));
axisY->setTitleVisible(true);
// 获取 widget_coalChart 的布局 // 获取 widget_coalChart 的布局
QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout()); QVBoxLayout* layout = static_cast<QVBoxLayout*>(ui->widget_coalChart->layout());
@ -89,25 +196,37 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart()
void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime) void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime)
{ {
QNetworkAccessManager* manager = new QNetworkAccessManager(); // 使用类成员变量 QNetworkAccessManager
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString();
// URL // URL
QUrl url("http://192.168.5.205:5002/api/get/coal/statistics/value/"); QUrl url(coalStaticUrl);
QNetworkRequest request(url); QNetworkRequest request(url);
// Form data // Form data
QUrlQuery params; QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
params.addQueryItem("begin_time", beginTime);
params.addQueryItem("end_time", endTime);
// 设置请求头 // begin_time 参数
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QHttpPart beginTimePart;
beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\""));
beginTimePart.setBody(beginTime.toUtf8());
// end_time 参数
QHttpPart endTimePart;
endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"end_time\""));
endTimePart.setBody(endTime.toUtf8());
// 将表单字段附加到 multipart 请求体中
multiPart->append(beginTimePart);
multiPart->append(endTimePart);
// 发送 POST 请求 // 发送 POST 请求
QNetworkReply* reply = manager->post(request, params.query(QUrl::FullyEncoded).toUtf8()); QNetworkReply* reply = manager->post(request, multiPart);
// 连接信号和槽 // 连接信号和槽
QObject::connect(reply, &QNetworkReply::finished, [reply]() { QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() {
if (reply->error() == QNetworkReply::NoError) { if (reply->error() == QNetworkReply::NoError) {
// 处理成功的响应 // 处理成功的响应
QByteArray responseData = reply->readAll(); QByteArray responseData = reply->readAll();
@ -117,15 +236,19 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be
int code = jsonObj["code"].toInt(); int code = jsonObj["code"].toInt();
QString message = jsonObj["message"].toString(); QString message = jsonObj["message"].toString();
QJsonObject data = jsonObj["data"].toObject(); QJsonObject data = jsonObj["data"].toObject();
double totalVolume = data["total_volume"].toDouble(); double totalVolume = data["total_volum"].toDouble();
qDebug() << "Code:" << code; qDebug() << "Code:" << code;
qDebug() << "Message:" << message; qDebug() << "Message:" << message;
qDebug() << "Total Volume:" << totalVolume; qDebug() << "Total Volume:" << totalVolume;
} else { } else {
// 处理错误 // 处理错误
qDebug() << "Error:" << reply->errorString(); QString errorString = reply->errorString();
qDebug() << "Error:" << errorString;
} }
// 清理资源
reply->deleteLater(); reply->deleteLater();
multiPart->deleteLater();
}); });
} }

View File

@ -9,6 +9,13 @@ namespace Ui {
class CoalStatisticsChartWidget; class CoalStatisticsChartWidget;
} }
enum TimeUnit {
Hour = 0, // 时
Day = 1, // 天
Month = 2, // 月
Year = 3 // 年
};
class CoalStatisticsChartWidget : public QWidget { class CoalStatisticsChartWidget : public QWidget {
Q_OBJECT Q_OBJECT
@ -16,6 +23,13 @@ public:
explicit CoalStatisticsChartWidget(QWidget* parent = nullptr); explicit CoalStatisticsChartWidget(QWidget* parent = nullptr);
~CoalStatisticsChartWidget(); ~CoalStatisticsChartWidget();
private slots:
/**
* @brief on_pushButton_coalStatisOk_clicked
*/
void on_pushButton_coalStatisOk_clicked();
private: private:
/** /**
* @brief initCoalStatisticsBarChart * @brief initCoalStatisticsBarChart
@ -33,7 +47,12 @@ private:
QChart* m_chart; QChart* m_chart;
QChartView* m_chartView; QChartView* m_chartView;
QBarSeries* m_series; QBarSeries* m_series;
QDateTimeAxis* m_axisX;
QValueAxis* m_axisY;
QTimer* m_timer; QTimer* m_timer;
QSettings* m_systemSetting;
}; };
#endif // COALSTATISTICSCHARTWIDGET_H #endif // COALSTATISTICSCHARTWIDGET_H

View File

@ -32,13 +32,13 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing"> <property name="spacing">
<number>15</number> <number>5</number>
</property> </property>
<property name="leftMargin"> <property name="leftMargin">
<number>10</number> <number>6</number>
</property> </property>
<property name="rightMargin"> <property name="rightMargin">
<number>10</number> <number>6</number>
</property> </property>
<item> <item>
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
@ -204,6 +204,42 @@ QDateTimeEdit::up-button, QDateTimeEdit::down-button {
</item> </item>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="pushButton_coalStatisOk">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>45</width>
<height>20</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QPushButton{
border-image: url(:/Resource/image/labelunSelect.png);
color:white;
font-size: 14px;
border: none;
}
QPushButton:hover{
border-image: url(:/Resource/image/labelhover.png);
}
QPushButton:pressed{
border-image: url(:/Resource/image/labelSelect.png);
}
</string>
</property>
<property name="text">
<string>确定</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
<item> <item>