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
TARGET = HX_CoalFlowDetect
@ -26,13 +26,16 @@ CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
mainwindow.cpp \
view/coaldistributchartwidget.cpp
HEADERS += \
mainwindow.h
mainwindow.h \
view/coaldistributchartwidget.h
FORMS += \
mainwindow.ui
mainwindow.ui \
view/coaldistributchartwidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin

View File

@ -1,32 +1,27 @@
#include "mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "view/coaldistributchartwidget.h"
#include <QDateTime>
#include <QTimer>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint |
Qt::WindowMinimizeButtonHint);
calendarLoopPlay();
addcoalDistributChart();
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::~MainWindow() { delete ui; }
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()) {
this->showMaximized();
} else {
@ -34,15 +29,11 @@ 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();
QTimer* timer_calendar = new QTimer(this);
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");
@ -50,3 +41,24 @@ void MainWindow::calendarLoopPlay()
});
timer_calendar->start(1000); //每一秒溢出一次进入槽函数
}
void MainWindow::addcoalDistributChart() {
CoalDistributChartWidget *coalDistributChartWidget =
new CoalDistributChartWidget(ui->widget_coalDistributChart);
// 可选:设置大小
coalDistributChartWidget->setMinimumSize(400, 300); // 设置最小大小以确保可见
// 确保将部件添加到布局中
QVBoxLayout *layout =
qobject_cast<QVBoxLayout *>(ui->widget_coalDistributChart->layout());
if (layout) {
layout->addWidget(coalDistributChartWidget);
} else {
// 如果没有布局,可以手动添加部件
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
#include <QMainWindow>
@ -11,7 +11,7 @@ class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
@ -27,8 +27,13 @@ private:
*/
void calendarLoopPlay();
/**
* @brief addcoalDistributChart
*/
void addcoalDistributChart();
private:
Ui::MainWindow* ui;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

View File

@ -1356,7 +1356,7 @@ QProgressBar::chunk {
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<widget class="QProgressBar" name="progressBar_belt1Temperature">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -1430,7 +1430,7 @@ QProgressBar::chunk {
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar_2">
<widget class="QProgressBar" name="progressBar_belt1Wind">
<property name="maximumSize">
<size>
<width>16777215</width>
@ -1810,9 +1810,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt2Speed">
<property name="maximumSize">
<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>
</widget>
</item>
@ -1864,9 +1884,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt2electric">
<property name="maximumSize">
<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>
</widget>
</item>
@ -1918,9 +1958,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt2Temperature">
<property name="maximumSize">
<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>
</widget>
</item>
@ -1972,9 +2032,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_9">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt2Wind">
<property name="maximumSize">
<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>
</widget>
</item>
@ -2005,7 +2085,7 @@ QPushButton:pressed{
</spacer>
</item>
<item>
<widget class="QLabel" name="label_23">
<widget class="QLabel" name="label_belt2State">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
@ -2325,9 +2405,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_10">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt3Speed">
<property name="maximumSize">
<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>
</widget>
</item>
@ -2379,9 +2479,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_11">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt3electric">
<property name="maximumSize">
<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>
</widget>
</item>
@ -2433,9 +2553,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_12">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt3Temperature">
<property name="maximumSize">
<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>
</widget>
</item>
@ -2487,9 +2627,29 @@ QPushButton:pressed{
</widget>
</item>
<item>
<widget class="QSlider" name="horizontalSlider_13">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<widget class="QProgressBar" name="progressBar_belt3Wind">
<property name="maximumSize">
<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>
</widget>
</item>
@ -2520,7 +2680,7 @@ QPushButton:pressed{
</spacer>
</item>
<item>
<widget class="QLabel" name="label_31">
<widget class="QLabel" name="label_belt3State">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
@ -2758,7 +2918,30 @@ QPushButton:pressed{
</layout>
</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>
</layout>
</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>