feat:煤流分布动态chart

This commit is contained in:
“Diviner66”, 2024-10-31 22:02:32 +08:00
parent 94793e3b62
commit 03f215f312
7 changed files with 447 additions and 83 deletions

View File

@ -4,8 +4,8 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui QT += core gui \
charts
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = HX_CoalFlowDetect TARGET = HX_CoalFlowDetect
@ -26,13 +26,16 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp mainwindow.cpp \
view/coaldistributchartwidget.cpp
HEADERS += \ HEADERS += \
mainwindow.h mainwindow.h \
view/coaldistributchartwidget.h
FORMS += \ FORMS += \
mainwindow.ui mainwindow.ui \
view/coaldistributchartwidget.ui
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin

View File

@ -1,52 +1,64 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "view/coaldistributchartwidget.h"
#include <QDateTime> #include <QDateTime>
#include <QTimer> #include <QTimer>
MainWindow::MainWindow(QWidget* parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
, ui(new Ui::MainWindow) ui->setupUi(this);
{ this->setWindowFlag(Qt::FramelessWindowHint);
ui->setupUi(this); this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
this->setWindowFlag(Qt::FramelessWindowHint); Qt::WindowMinimizeButtonHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
calendarLoopPlay(); calendarLoopPlay();
addcoalDistributChart();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow() { delete ui; }
{
delete ui; void MainWindow::on_pushButton_close_clicked() { this->close(); }
void MainWindow::on_pushButton_max_clicked() {
if (isFullScreen()) {
this->showMaximized();
} else {
this->showFullScreen();
}
} }
void MainWindow::on_pushButton_close_clicked() void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); }
{
this->close(); void MainWindow::calendarLoopPlay() {
ui->label_calendar->clear();
QTimer *timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, [this]() {
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->label_calendar->setText(str);
});
timer_calendar->start(1000); //每一秒溢出一次进入槽函数
} }
void MainWindow::on_pushButton_max_clicked() void MainWindow::addcoalDistributChart() {
{
if (isFullScreen()) {
this->showMaximized();
} else {
this->showFullScreen();
}
}
void MainWindow::on_pushButton_min_clicked() CoalDistributChartWidget *coalDistributChartWidget =
{ new CoalDistributChartWidget(ui->widget_coalDistributChart);
this->showMinimized();
}
void MainWindow::calendarLoopPlay() // 可选:设置大小
{ coalDistributChartWidget->setMinimumSize(400, 300); // 设置最小大小以确保可见
ui->label_calendar->clear();
QTimer* timer_calendar = new QTimer(this); // 确保将部件添加到布局中
connect(timer_calendar, &QTimer::timeout, this, [this]() { QVBoxLayout *layout =
QDateTime time = QDateTime::currentDateTime(); qobject_cast<QVBoxLayout *>(ui->widget_coalDistributChart->layout());
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); if (layout) {
ui->label_calendar->setText(str); layout->addWidget(coalDistributChartWidget);
}); } else {
timer_calendar->start(1000); //每一秒溢出一次进入槽函数 // 如果没有布局,可以手动添加部件
ui->widget_coalDistributChart->setLayout(new QVBoxLayout());
ui->widget_coalDistributChart->layout()->addWidget(
coalDistributChartWidget);
}
} }

View File

@ -1,4 +1,4 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
@ -8,27 +8,32 @@ class MainWindow;
} }
class MainWindow : public QMainWindow { class MainWindow : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit MainWindow(QWidget* parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
private slots: private slots:
void on_pushButton_close_clicked(); void on_pushButton_close_clicked();
void on_pushButton_max_clicked(); void on_pushButton_max_clicked();
void on_pushButton_min_clicked(); void on_pushButton_min_clicked();
private: private:
/** /**
* @brief calendarLoopPlay * @brief calendarLoopPlay
*/ */
void calendarLoopPlay(); void calendarLoopPlay();
/**
* @brief addcoalDistributChart
*/
void addcoalDistributChart();
private: private:
Ui::MainWindow* ui; Ui::MainWindow *ui;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -1356,7 +1356,7 @@ QProgressBar::chunk {
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QProgressBar" name="progressBar"> <widget class="QProgressBar" name="progressBar_belt1Temperature">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>16777215</width> <width>16777215</width>
@ -1430,7 +1430,7 @@ QProgressBar::chunk {
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QProgressBar" name="progressBar_2"> <widget class="QProgressBar" name="progressBar_belt1Wind">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>16777215</width> <width>16777215</width>
@ -1810,9 +1810,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_6"> <widget class="QProgressBar" name="progressBar_belt2Speed">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -1864,9 +1884,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_7"> <widget class="QProgressBar" name="progressBar_belt2electric">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -1918,9 +1958,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_8"> <widget class="QProgressBar" name="progressBar_belt2Temperature">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -1972,9 +2032,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_9"> <widget class="QProgressBar" name="progressBar_belt2Wind">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -2005,7 +2085,7 @@ QPushButton:pressed{
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="QLabel" name="label_23"> <widget class="QLabel" name="label_belt2State">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -2325,9 +2405,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_10"> <widget class="QProgressBar" name="progressBar_belt3Speed">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -2379,9 +2479,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_11"> <widget class="QProgressBar" name="progressBar_belt3electric">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -2433,9 +2553,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_12"> <widget class="QProgressBar" name="progressBar_belt3Temperature">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -2487,9 +2627,29 @@ QPushButton:pressed{
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSlider" name="horizontalSlider_13"> <widget class="QProgressBar" name="progressBar_belt3Wind">
<property name="orientation"> <property name="maximumSize">
<enum>Qt::Horizontal</enum> <size>
<width>16777215</width>
<height>10</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar {
border: none;
background: #2E3133;
border-radius: 50px;
}
QProgressBar::chunk {
background: #53F2A4;
border-radius: 15px;
}</string>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -2520,7 +2680,7 @@ QPushButton:pressed{
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="QLabel" name="label_31"> <widget class="QLabel" name="label_belt3State">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum"> <sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -2758,7 +2918,30 @@ QPushButton:pressed{
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QWidget" name="widget" native="true"/> <widget class="QWidget" name="widget_coalDistributChart" native="true">
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
<layout class="QVBoxLayout" name="verticalLayout_21">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</item> </item>
</layout> </layout>
</item> </item>

View File

@ -0,0 +1,86 @@
#include "coaldistributchartwidget.h"
#include "ui_coaldistributchartwidget.h"
#include <QVBoxLayout>
CoalDistributChartWidget::CoalDistributChartWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::CoalDistributChartWidget) {
initializeChart(); // 初始化图表
initializeTimeAxis(); // 初始化时间轴
// 设置定时器
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this,
&CoalDistributChartWidget::updateChart);
ui->setupUi(this);
m_timer->start(1000); // 每秒更新一次
}
CoalDistributChartWidget::~CoalDistributChartWidget() { delete ui; }
void CoalDistributChartWidget::updateChart() {
updateBarData();
updateTimeAxis();
}
void CoalDistributChartWidget::initializeChart() {
m_chart = new QChart(); // 将chart设为类成员
m_series = new QBarSeries();
// 创建柱状图数据
m_barSet = new QBarSet("数据1");
m_series->append(m_barSet);
m_chart->addSeries(m_series);
// 设置X轴为时间
m_axisX = new QCategoryAxis();
m_chart->addAxis(m_axisX, Qt::AlignBottom);
m_series->attachAxis(m_axisX);
// 设置Y轴为百分比
m_axisY = new QValueAxis();
m_axisY->setRange(0, 100); // 设置范围为0到100%
m_chart->addAxis(m_axisY, Qt::AlignLeft);
m_series->attachAxis(m_axisY);
m_chart->setTitle("动态柱状图示例");
auto *chartView = new QChartView(m_chart);
chartView->setRenderHint(QPainter::Antialiasing);
// 清空之前的布局,避免重复添加
QLayout *currentLayout = this->layout();
if (currentLayout) {
QLayoutItem *item;
while ((item = currentLayout->takeAt(0))) {
delete item->widget(); // 删除现有的widget
delete item; // 删除布局项
}
}
// 创建新的布局
QVBoxLayout *newLayout = new QVBoxLayout(this); // 使用不同的变量名
newLayout->addWidget(chartView);
setLayout(newLayout);
}
void CoalDistributChartWidget::initializeTimeAxis() {
for (int i = 0; i < 5; ++i) {
m_axisX->append(
QDateTime::currentDateTime().addSecs(i).toString("HH:mm:ss"), i);
}
}
void CoalDistributChartWidget::updateBarData() {
// 更新柱状图数据
*m_barSet << qrand() % 101; // 随机生成0到100之间的值
}
void CoalDistributChartWidget::updateTimeAxis() {
// 更新时间轴
m_axisX->remove(0); // 移除第一个时间
m_axisX->append(QDateTime::currentDateTime().toString("HH:mm:ss"),
4); // 添加新的时间
}

View File

@ -0,0 +1,55 @@
#ifndef COALDISTRIBUTCHARTWIDGET_H
#define COALDISTRIBUTCHARTWIDGET_H
#include <QDateTime>
#include <QTimer>
#include <QWidget>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QCategoryAxis>
#include <QtCharts/QChartView>
#include <QtCharts/QValueAxis>
namespace Ui {
class CoalDistributChartWidget;
}
QT_CHARTS_USE_NAMESPACE
class CoalDistributChartWidget : public QWidget {
Q_OBJECT
public:
explicit CoalDistributChartWidget(QWidget *parent = nullptr);
~CoalDistributChartWidget();
private slots:
void updateChart();
private:
/**
* @brief initializeChart初始化图表
*/
void initializeChart();
/**
* @brief initializeTimeAxis初始化时间轴
*/
void initializeTimeAxis();
void updateBarData();
void updateTimeAxis();
private:
Ui::CoalDistributChartWidget *ui;
QChart *m_chart;
QBarSeries *m_series;
QBarSet *m_barSet;
QCategoryAxis *m_axisX;
QValueAxis *m_axisY;
QTimer *m_timer;
};
#endif // COALDISTRIBUTCHARTWIDGET_H

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CoalDistributChartWidget</class>
<widget class="QWidget" name="CoalDistributChartWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>619</width>
<height>270</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2"/>
</widget>
<resources/>
<connections/>
</ui>