From 03f215f312a5a6ffafc5f58ed5cd140751c7bde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDiviner66=E2=80=9D=EF=BC=8C?= <“xxu715737@163.com”> Date: Thu, 31 Oct 2024 22:02:32 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E7=85=A4=E6=B5=81=E5=88=86?= =?UTF-8?q?=E5=B8=83=E5=8A=A8=E6=80=81chart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HX_CoalFlowDetect.pro | 13 +- mainwindow.cpp | 86 ++++++----- mainwindow.h | 29 ++-- mainwindow.ui | 241 ++++++++++++++++++++++++++---- view/coaldistributchartwidget.cpp | 86 +++++++++++ view/coaldistributchartwidget.h | 55 +++++++ view/coaldistributchartwidget.ui | 20 +++ 7 files changed, 447 insertions(+), 83 deletions(-) create mode 100644 view/coaldistributchartwidget.cpp create mode 100644 view/coaldistributchartwidget.h create mode 100644 view/coaldistributchartwidget.ui diff --git a/HX_CoalFlowDetect.pro b/HX_CoalFlowDetect.pro index 66617d5..1432645 100644 --- a/HX_CoalFlowDetect.pro +++ b/HX_CoalFlowDetect.pro @@ -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 diff --git a/mainwindow.cpp b/mainwindow.cpp index 2384f62..7950030 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,52 +1,64 @@ -#include "mainwindow.h" +#include "mainwindow.h" #include "ui_mainwindow.h" +#include "view/coaldistributchartwidget.h" #include #include -MainWindow::MainWindow(QWidget* parent) - : QMainWindow(parent) - , ui(new Ui::MainWindow) -{ - ui->setupUi(this); - this->setWindowFlag(Qt::FramelessWindowHint); - this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint); +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent), ui(new Ui::MainWindow) { + ui->setupUi(this); + this->setWindowFlag(Qt::FramelessWindowHint); + this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | + Qt::WindowMinimizeButtonHint); - calendarLoopPlay(); + calendarLoopPlay(); + + addcoalDistributChart(); } -MainWindow::~MainWindow() -{ - delete ui; +MainWindow::~MainWindow() { 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() -{ - this->close(); +void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); } + +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() -{ - if (isFullScreen()) { - this->showMaximized(); - } else { - this->showFullScreen(); - } -} +void MainWindow::addcoalDistributChart() { -void MainWindow::on_pushButton_min_clicked() -{ - this->showMinimized(); -} + CoalDistributChartWidget *coalDistributChartWidget = + new CoalDistributChartWidget(ui->widget_coalDistributChart); -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); //每一秒溢出一次进入槽函数 + // 可选:设置大小 + coalDistributChartWidget->setMinimumSize(400, 300); // 设置最小大小以确保可见 + + // 确保将部件添加到布局中 + QVBoxLayout *layout = + qobject_cast(ui->widget_coalDistributChart->layout()); + if (layout) { + layout->addWidget(coalDistributChartWidget); + } else { + // 如果没有布局,可以手动添加部件 + ui->widget_coalDistributChart->setLayout(new QVBoxLayout()); + ui->widget_coalDistributChart->layout()->addWidget( + coalDistributChartWidget); + } } diff --git a/mainwindow.h b/mainwindow.h index 8201905..7587b52 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -1,4 +1,4 @@ -#ifndef MAINWINDOW_H +#ifndef MAINWINDOW_H #define MAINWINDOW_H #include @@ -8,27 +8,32 @@ class MainWindow; } class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(QWidget* parent = nullptr); - ~MainWindow(); + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); 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: - /** - * @brief calendarLoopPlay 时间轮播 - */ - void calendarLoopPlay(); + /** + * @brief calendarLoopPlay 时间轮播 + */ + void calendarLoopPlay(); + + /** + * @brief addcoalDistributChart 添加煤流分布柱状图 + */ + void addcoalDistributChart(); private: - Ui::MainWindow* ui; + Ui::MainWindow *ui; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 6ee2fea..83332f3 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -1356,7 +1356,7 @@ QProgressBar::chunk { - + 16777215 @@ -1430,7 +1430,7 @@ QProgressBar::chunk { - + 16777215 @@ -1810,9 +1810,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -1864,9 +1884,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -1918,9 +1958,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -1972,9 +2032,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -2005,7 +2085,7 @@ QPushButton:pressed{ - + 0 @@ -2325,9 +2405,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -2379,9 +2479,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -2433,9 +2553,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -2487,9 +2627,29 @@ QPushButton:pressed{ - - - Qt::Horizontal + + + + 16777215 + 10 + + + + QProgressBar { + border: none; + background: #2E3133; + border-radius: 50px; + } +QProgressBar::chunk { + background: #53F2A4; + border-radius: 15px; + } + + + 24 + + + false @@ -2520,7 +2680,7 @@ QPushButton:pressed{ - + 0 @@ -2758,7 +2918,30 @@ QPushButton:pressed{ - + + + + 14 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + diff --git a/view/coaldistributchartwidget.cpp b/view/coaldistributchartwidget.cpp new file mode 100644 index 0000000..9830c21 --- /dev/null +++ b/view/coaldistributchartwidget.cpp @@ -0,0 +1,86 @@ +#include "coaldistributchartwidget.h" +#include "ui_coaldistributchartwidget.h" + +#include + +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); // 添加新的时间 +} diff --git a/view/coaldistributchartwidget.h b/view/coaldistributchartwidget.h new file mode 100644 index 0000000..6425e9a --- /dev/null +++ b/view/coaldistributchartwidget.h @@ -0,0 +1,55 @@ +#ifndef COALDISTRIBUTCHARTWIDGET_H +#define COALDISTRIBUTCHARTWIDGET_H + +#include +#include +#include + +#include +#include +#include +#include +#include + +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 diff --git a/view/coaldistributchartwidget.ui b/view/coaldistributchartwidget.ui new file mode 100644 index 0000000..f5bf738 --- /dev/null +++ b/view/coaldistributchartwidget.ui @@ -0,0 +1,20 @@ + + + CoalDistributChartWidget + + + + 0 + 0 + 619 + 270 + + + + Form + + + + + +