欧美性猛交xxxx免费看_牛牛在线视频国产免费_天堂草原电视剧在线观看免费_国产粉嫩高清在线观看_国产欧美日本亚洲精品一5区

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Qt(C++)使用QChart動(dòng)態(tài)顯示3個(gè)設(shè)備的溫度變化曲線

DS小龍哥-嵌入式技術(shù) ? 來(lái)源:DS小龍哥-嵌入式技術(shù) ? 作者:DS小龍哥-嵌入式技 ? 2023-06-02 09:06 ? 次閱讀

一、介紹

Qt的QChart是一個(gè)用于繪制圖表和可視化數(shù)據(jù)的類(lèi)。提供了一個(gè)靈活的、可擴(kuò)展的、跨平臺(tái)的圖表繪制解決方案,可以用于各種應(yīng)用程序,如數(shù)據(jù)分析、科學(xué)計(jì)算、金融交易等。

QChart支持多種類(lèi)型的圖表,包括折線圖、散點(diǎn)圖、柱狀圖、餅圖等。它還支持多個(gè)數(shù)據(jù)系列(datasets)在同一個(gè)圖表中顯示,并且可以自定義各種圖表屬性和樣式,如坐標(biāo)軸標(biāo)簽、標(biāo)題、圖例等。

QChart還支持多種數(shù)據(jù)源(data sources),可以來(lái)自Qt的數(shù)據(jù)模型(data models)、CSV文件、JSON文件等。數(shù)據(jù)源可以是任何支持迭代器(iterator)的類(lèi)型,因此可以輕松地與其他Qt組件集成。

使用QChart可以輕松地創(chuàng)建交互式圖表,如鼠標(biāo)懸停提示(hover tooltip)、數(shù)據(jù)選擇(data selection)等。此外,QChart還支持多種主題(themes)和自定義CSS樣式,使得圖表外觀可以靈活地定制。

out

image-20230529143347595

image-20230529143404140

二、實(shí)現(xiàn)代碼(1)QMainWindow

以下是使用Qt(C++)的QChart模塊顯示3個(gè)設(shè)備的動(dòng)態(tài)溫度曲線的代碼實(shí)現(xiàn):

【1】實(shí)現(xiàn)溫度動(dòng)態(tài)更新

mainwindow.h

#ifndef MAINWINDOW_H
 #define MAINWINDOW_H
 ?
 #include < QMainWindow >
 #include < QtCharts/QChart >
 #include < QtCharts/QLineSeries >
 #include < QTimer >
 ?
 QT_CHARTS_USE_NAMESPACE
 ?
 namespace Ui {
 class MainWindow;
 }
 ?
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
 ?
 public:
     explicit MainWindow(QWidget *parent = nullptr);
     ~MainWindow();
 ?
 private slots:
     void updateChartData(); // 更新數(shù)據(jù)槽函數(shù)
 ?
 private:
     Ui::MainWindow *ui;
     QTimer *m_timer; // 定時(shí)器
 ?
     QChart *m_chart; // 圖表指針
 ?
     QLineSeries *m_series1; // 設(shè)備1溫度曲線
     QLineSeries *m_series2; // 設(shè)備2溫度曲線
     QLineSeries *m_series3; // 設(shè)備3溫度曲線
 ?
     int m_timeCount; // 時(shí)間計(jì)數(shù)
 };
 ?
 #endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
 #include "ui_mainwindow.h"
 ?
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow),
     m_timer(new QTimer(this)),
     m_chart(new QChart()),
     m_series1(new QLineSeries()),
     m_series2(new QLineSeries()),
     m_series3(new QLineSeries()),
     m_timeCount(0)
 {
     ui- >setupUi(this);
 ?
     // 設(shè)置圖表標(biāo)題
     m_chart- >setTitle("Temperature Data");
 ?
     // 創(chuàng)建溫度曲線圖1并設(shè)置屬性
     m_series1- >setName(tr("Device 1"));
     m_series1- >setColor(Qt::red);
     m_series1- >setPen(QPen(Qt::red, 2));
     m_chart- >addSeries(m_series1);
 ?
     // 創(chuàng)建溫度曲線圖2并設(shè)置屬性
     m_series2- >setName(tr("Device 2"));
     m_series2- >setColor(Qt::green);
     m_series2- >setPen(QPen(Qt::green, 2));
     m_chart- >addSeries(m_series2);
 ?
     // 創(chuàng)建溫度曲線圖3并設(shè)置屬性
     m_series3- >setName(tr("Device 3"));
     m_series3- >setColor(Qt::blue);
     m_series3- >setPen(QPen(Qt::blue, 2));
     m_chart- >addSeries(m_series3);
 ?
     // 設(shè)置橫軸屬性
     QValueAxis *axisX = new QValueAxis;
     axisX- >setRange(0, 30);
     axisX- >setTitleText("Time (s)");
     m_chart- >addAxis(axisX, Qt::AlignBottom);
     m_series1- >attachAxis(axisX);
     m_series2- >attachAxis(axisX);
     m_series3- >attachAxis(axisX);
 ?
     // 設(shè)置縱軸屬性
     QValueAxis *axisY = new QValueAxis;
     axisY- >setRange(0, 60);
     axisY- >setTitleText("Temperature (℃)");
     m_chart- >addAxis(axisY, Qt::AlignLeft);
     m_series1- >attachAxis(axisY);
     m_series2- >attachAxis(axisY);
     m_series3- >attachAxis(axisY);
 ?
     // 定時(shí)更新數(shù)據(jù)
     connect(m_timer, &QTimer::timeout, this, &MainWindow::updateChartData);
     m_timer- >start(1000); // 每隔1秒鐘更新一次數(shù)據(jù)
 ?
     // 將圖表添加到ChartView中
     ui- >chartView- >setChart(m_chart);
     ui- >chartView- >setRenderHint(QPainter::Antialiasing);
 }
 ?
 MainWindow::~MainWindow()
 {
     delete ui;
 }
 ?
 void MainWindow::updateChartData()
 {
     // 更新時(shí)間計(jì)數(shù)
     m_timeCount++;
 ?
     // 在溫度曲線上增加一個(gè)點(diǎn),模擬溫度數(shù)據(jù)變化
     QPointF p1(m_timeCount, qrand() % 10 + 20);
     QPointF p2(m_timeCount, qrand() % 10 + 30);
     QPointF p3(m_timeCount, qrand() % 10 + 40);
     m_series1- >append(p1);
     m_series2- >append(p2);
     m_series3- >append(p3);
 ?
     // 清除多余的點(diǎn),只保留最新的30個(gè)數(shù)據(jù)點(diǎn)
     if (m_series1- >count() > 30) {
         m_series1- >removePoints(0, 1);
     }
     if (m_series2- >count() > 30) {
         m_series2- >removePoints(0, 1);
     }
     if (m_series3- >count() > 30) {
         m_series3- >removePoints(0, 1);
     }
 }

在此代碼中,定義了一個(gè)QTimer定時(shí)器對(duì)象,用于每隔一段時(shí)間更新溫度曲線數(shù)據(jù)。在定時(shí)器的timeout信號(hào)觸發(fā)時(shí),調(diào)用updateChartData()槽函數(shù)來(lái)更新溫度曲線數(shù)據(jù),同時(shí)控制數(shù)據(jù)量不超過(guò)30個(gè)點(diǎn)。

在updateChartData()函數(shù)中,使用了qrand()函數(shù)來(lái)生成隨機(jī)的溫度數(shù)據(jù),模擬動(dòng)態(tài)變化的效果??梢愿鶕?jù)實(shí)際情況修改此函數(shù)的實(shí)現(xiàn)方式。

最后,將圖表添加到QChartView控件中,并啟用抗鋸齒功能以提高顯示質(zhì)量。

【2】設(shè)置曲線可見(jiàn)范圍

為了保證曲線顯示一直在可見(jiàn)范圍內(nèi),可以添加如下代碼:

// 使圖表自適應(yīng)大小,確保曲線始終可見(jiàn)
     m_chart- >createDefaultAxes();
     m_chart- >axisX()- >setRange(0, 30);
     m_chart- >axisY()- >setRange(0, 60);

這段代碼的作用是讓圖表自適應(yīng)大小,并設(shè)置橫軸范圍為0到30,縱軸范圍為0到60。這樣當(dāng)新數(shù)據(jù)點(diǎn)增加到圖表之外時(shí),圖表會(huì)自動(dòng)調(diào)整大小和范圍,以確保曲線始終可見(jiàn)。

完整的mainwindow.cpp代碼如下所示:

#include "mainwindow.h"
     #include "ui_mainwindow.h"
     ?
     MainWindow::MainWindow(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::MainWindow),
         m_timer(new QTimer(this)),
         m_chart(new QChart()),
         m_series1(new QLineSeries()),
         m_series2(new QLineSeries()),
         m_series3(new QLineSeries()),
         m_timeCount(0)
     {
         ui- >setupUi(this);
     ?
         // 設(shè)置圖表標(biāo)題
         m_chart- >setTitle("Temperature Data");
     ?
         // 創(chuàng)建溫度曲線圖1并設(shè)置屬性
         m_series1- >setName(tr("Device 1"));
         m_series1- >setColor(Qt::red);
         m_series1- >setPen(QPen(Qt::red, 2));
         m_chart- >addSeries(m_series1);
     ?
         // 創(chuàng)建溫度曲線圖2并設(shè)置屬性
         m_series2- >setName(tr("Device 2"));
         m_series2- >setColor(Qt::green);
         m_series2- >setPen(QPen(Qt::green, 2));
         m_chart- >addSeries(m_series2);
     ?
         // 創(chuàng)建溫度曲線圖3并設(shè)置屬性
         m_series3- >setName(tr("Device 3"));
         m_series3- >setColor(Qt::blue);
         m_series3- >setPen(QPen(Qt::blue, 2));
         m_chart- >addSeries(m_series3);
     ?
         // 設(shè)置橫軸屬性
         QValueAxis *axisX = new QValueAxis;
         axisX- >setRange(0, 30);
         axisX- >setTitleText("Time (s)");
         m_chart- >addAxis(axisX, Qt::AlignBottom);
         m_series1- >attachAxis(axisX);
         m_series2- >attachAxis(axisX);
         m_series3- >attachAxis(axisX);
     ?
         // 設(shè)置縱軸屬性
         QValueAxis *axisY = new QValueAxis;
         axisY- >setRange(0, 60);
         axisY- >setTitleText("Temperature (℃)");
         m_chart- >addAxis(axisY, Qt::AlignLeft);
         m_series1- >attachAxis(axisY);
         m_series2- >attachAxis(axisY);
         m_series3- >attachAxis(axisY);
     ?
         // 使圖表自適應(yīng)大小,確保曲線始終可見(jiàn)
         m_chart- >createDefaultAxes();
         m_chart- >axisX()- >setRange(0, 30);
         m_chart- >axisY()- >setRange(0, 60);
     ?
         // 定時(shí)更新數(shù)據(jù)
         connect(m_timer, &QTimer::timeout, this, &MainWindow::updateChartData);
         m_timer- >start(1000); // 每隔1秒鐘更新一次數(shù)據(jù)
     ?
         // 將圖表添加到ChartView中
         ui- >chartView- >setChart(m_chart);
         ui- >chartView- >setRenderHint(QPainter::Antialiasing);
     }
     ?
     MainWindow::~MainWindow()
     {
         delete ui;
     }
     ?
     void MainWindow::updateChartData()
     {
         // 更新時(shí)間計(jì)數(shù)
         m_timeCount++;
     ?
         // 在溫度曲線上增加一個(gè)點(diǎn),模擬溫度數(shù)據(jù)變化
         QPointF p1(m_timeCount, qrand() % 10 + 20);
         QPointF p2(m_timeCount, qrand() % 10 + 30);
         QPointF p3(m_timeCount, qrand() % 10 + 40);
         m_series1- >append(p1);
         m_series2- >append(p2);
         m_series3- >append(p3);
     ?
         // 清除多余的點(diǎn),只保留最新的30個(gè)數(shù)據(jù)點(diǎn)
         if (m_series1- >count() > 30) {
             m_series1- >removePoints(0, 1);
         }
         if (m_series2- >count() > 30) {
             m_series2- >removePoints(0, 1);
         }
         if (m_series3- >count() > 30) {
             m_series3- >removePoints(0, 1);
         }
     }

【3】實(shí)現(xiàn)鼠標(biāo)交互拖動(dòng)

要實(shí)現(xiàn)折線圖的橫坐標(biāo)可以拖動(dòng),可以設(shè)置QChartView的交互模式為拖拽,在構(gòu)造函數(shù)中添加如下代碼:

// 設(shè)置 ChartView 交互模式為拖拽
     ui- >chartView- >setRubberBand(QChartView::HorizontalRubberBand);
     ui- >chartView- >setRenderHint(QPainter::Antialiasing);
     ui- >chartView- >setDragMode(QGraphicsView::ScrollHandDrag);

這樣用戶就可以通過(guò)鼠標(biāo)左鍵在橫軸上拖拽來(lái)改變曲線圖的可見(jiàn)范圍。同時(shí),還需要在mainwindow.cpp中添加橫坐標(biāo)的范圍更新函數(shù)updateAxisRange(),用于在拖拽時(shí)更新橫坐標(biāo)的范圍。

完整的mainwindow.cpp代碼如下所示:

#include "mainwindow.h"
     #include "ui_mainwindow.h"
     ?
     MainWindow::MainWindow(QWidget *parent) :
         QMainWindow(parent),
         ui(new Ui::MainWindow),
         m_timer(new QTimer(this)),
         m_chart(new QChart()),
         m_series1(new QLineSeries()),
         m_series2(new QLineSeries()),
         m_series3(new QLineSeries()),
         m_timeCount(0)
     {
         ui- >setupUi(this);
     ?
         // 設(shè)置圖表標(biāo)題
         m_chart- >setTitle("Temperature Data");
     ?
         // 創(chuàng)建溫度曲線圖1并設(shè)置屬性
         m_series1- >setName(tr("Device 1"));
         m_series1- >setColor(Qt::red);
         m_series1- >setPen(QPen(Qt::red, 2));
         m_chart- >addSeries(m_series1);
     ?
         // 創(chuàng)建溫度曲線圖2并設(shè)置屬性
         m_series2- >setName(tr("Device 2"));
         m_series2- >setColor(Qt::green);
         m_series2- >setPen(QPen(Qt::green, 2));
         m_chart- >addSeries(m_series2);
     ?
         // 創(chuàng)建溫度曲線圖3并設(shè)置屬性
         m_series3- >setName(tr("Device 3"));
         m_series3- >setColor(Qt::blue);
         m_series3- >setPen(QPen(Qt::blue, 2));
         m_chart- >addSeries(m_series3);
     ?
         // 設(shè)置橫軸屬性
         QValueAxis *axisX = new QValueAxis;
         axisX- >setRange(0, 30);
         axisX- >setTitleText("Time (s)");
         m_chart- >addAxis(axisX, Qt::AlignBottom);
         m_series1- >attachAxis(axisX);
         m_series2- >attachAxis(axisX);
         m_series3- >attachAxis(axisX);
     ?
         // 設(shè)置縱軸屬性
         QValueAxis *axisY = new QValueAxis;
         axisY- >setRange(0, 60);
         axisY- >setTitleText("Temperature (℃)");
         m_chart- >addAxis(axisY, Qt::AlignLeft);
         m_series1- >attachAxis(axisY);
         m_series2- >attachAxis(axisY);
         m_series3- >attachAxis(axisY);
     ?
         // 使圖表自適應(yīng)大小,確保曲線始終可見(jiàn)
         m_chart- >createDefaultAxes();
         m_chart- >axisX()- >setRange(0, 30);
         m_chart- >axisY()- >setRange(0, 60);
     ?
         // 設(shè)置 ChartView 交互模式為拖拽
         ui- >chartView- >setRubberBand(QChartView::HorizontalRubberBand);
         ui- >chartView- >setRenderHint(QPainter::Antialiasing);
         ui- >chartView- >setDragMode(QGraphicsView::ScrollHandDrag);
     ?
         // 定時(shí)更新數(shù)據(jù)
         connect(m_timer, &QTimer::timeout, this, &MainWindow::updateChartData);
         m_timer- >start(1000); // 每隔1秒鐘更新一次數(shù)據(jù)
     ?
         // 將圖表添加到ChartView中
         ui- >chartView- >setChart(m_chart);
     }
     ?
     MainWindow::~MainWindow()
     {
         delete ui;
     }
     ?
     void MainWindow::updateChartData()
     {
         // 更新時(shí)間計(jì)數(shù)
         m_timeCount++;
     ?
         // 在溫度曲線上增加一個(gè)點(diǎn),模擬溫度數(shù)據(jù)變化
         QPointF p1(m_timeCount, qrand() % 10 + 20);
         QPointF p2(m_timeCount, qrand() % 10 + 30);
         QPointF p3(m_timeCount, qrand() % 10 + 40);
         m_series1- >append(p1);
         m_series2- >append(p2);
         m_series3- >append(p3);
     ?
         // 清除多余的點(diǎn),只保留最新的30個(gè)數(shù)據(jù)點(diǎn)
         if (m_series1- >count() > 30) {
             m_series1- >removePoints(0, 1);
         }
         if (m_series2- >count() > 30) {
             m_series2- >removePoints(0, 1);
         }
         if (m_series3- >count() > 30) {
             m_series3- >removePoints(0, 1);
         }
     ?
         // 更新橫軸范圍
         updateAxisRange();
     }
     ?
     void MainWindow::updateAxisRange()
     {
         // 獲取橫軸范圍
         qreal minX = std::numeric_limits< qreal >::max();
         qreal maxX = std::numeric_limits< qreal >::min();
         foreach (QAbstractSeries *series, m_chart- >series()) {
             QXYSeries *xySeries = static_cast< QXYSeries* >(series);
             QPointF p1 = xySeries- >at(0);
             QPointF p2 = xySeries- >at(xySeries- >count() - 1);
     ?
             if (p1.x() < minX) {
                 minX = p1.x();
             }
             if (p2.x() > maxX) {
                 maxX = p2.x();
             }
         }
     ?
         // 更新橫軸范圍
         m_chart- >axisX()- >setRange(minX, maxX);
     }

為了更新橫坐標(biāo)的范圍,需要在MainWindow中添加了一個(gè)新函數(shù)updateAxisRange()。該函數(shù)會(huì)在數(shù)據(jù)更新時(shí)被調(diào)用來(lái)計(jì)算最新的橫軸范圍,以更新折線圖的顯示。

三、實(shí)現(xiàn)代碼(2)QWidget

當(dāng)前這份完整代碼實(shí)現(xiàn)了一個(gè)動(dòng)態(tài)折線圖的繪制,是一個(gè)典型的Qt Charts應(yīng)用程序。通過(guò)使用QLineSeries類(lèi)、QValueAxis類(lèi)和QChart類(lèi)來(lái)創(chuàng)建并顯示溫度隨時(shí)間變化的折線圖。

第一步:先創(chuàng)建了主窗口,其中包含一個(gè)QChartView控件,用于顯示溫度曲線圖。在構(gòu)造函數(shù)中,設(shè)置了一些基本屬性,比如標(biāo)題、橫縱坐標(biāo)的范圍和名稱(chēng)等,并為每個(gè)設(shè)備創(chuàng)建了一個(gè)QLineSeries對(duì)象,用于存儲(chǔ)溫度數(shù)據(jù)。

第二步:將這些QLineSeries對(duì)象添加到QChart對(duì)象中。接著,將QValueAxis對(duì)象添加到QChart中,設(shè)置其范圍和名稱(chēng),并將QLineSeries對(duì)象與其關(guān)聯(lián)。最后,將QChart對(duì)象添加到QChartView中,以便在界面上顯示折線圖。

第三步:在updateChartData()函數(shù)中,定時(shí)器每隔1秒鐘觸發(fā)一次,用于更新溫度數(shù)據(jù),并通過(guò)調(diào)用QLineSeries類(lèi)的append()函數(shù)向QLineSeries對(duì)象中添加新的溫度數(shù)據(jù)點(diǎn)。同時(shí),使用removePoints()函數(shù)刪除舊的數(shù)據(jù)點(diǎn),以保持折線圖中顯示的數(shù)據(jù)點(diǎn)不超過(guò)30個(gè)。在添加或刪除數(shù)據(jù)時(shí),使用updateAxisRange()函數(shù)更新橫坐標(biāo)的范圍,以便將折線圖自適應(yīng)地縮放到當(dāng)前數(shù)據(jù)范圍內(nèi)。

第四步:重載了updateAxisRange()函數(shù),根據(jù)QLineSeries對(duì)象的數(shù)據(jù)點(diǎn)計(jì)算出橫坐標(biāo)的最小值和最大值,并通過(guò)調(diào)用QChart類(lèi)的axisX()->setRange()函數(shù)更新QValueAxis對(duì)象的范圍。

【1】widget.cpp代碼

#include "widget.h"
     #include "ui_widget.h"
     ?
     Widget::Widget(QWidget *parent)
         : QWidget(parent)
         , ui(new Ui::Widget)
     {
         ui- >setupUi(this);
     ?
         this- >setWindowTitle("溫度數(shù)據(jù)可視化采集系統(tǒng)");
     ?
         m_timer=new QTimer(this);
         m_chart=new QChart();
     ?
         m_series1=new QLineSeries();
         m_series2=new QLineSeries();
         m_series3=new QLineSeries();
     ?
         m_timeCount=0;
     ?
         // 創(chuàng)建溫度曲線圖1并設(shè)置屬性
         m_series1- >setName(tr("設(shè)備1"));
     //    m_series1- >setColor(Qt::red);
     //    m_series1- >setPen(QPen(Qt::red, 2));
         m_chart- >addSeries(m_series1);
     ?
         // 創(chuàng)建溫度曲線圖2并設(shè)置屬性
         m_series2- >setName(tr("設(shè)備2"));
     //    m_series2- >setColor(Qt::green);
     //    m_series2- >setPen(QPen(Qt::green, 2));
         m_chart- >addSeries(m_series2);
     ?
         // 創(chuàng)建溫度曲線圖3并設(shè)置屬性
         m_series3- >setName(tr("設(shè)備3"));
     //    m_series3- >setColor(Qt::blue);
     //    m_series3- >setPen(QPen(Qt::blue, 2));
         m_chart- >addSeries(m_series3);
     ?
         // 設(shè)置橫軸屬性
         QValueAxis *axisX = new QValueAxis;
         axisX- >setRange(0, 30);
         axisX- >setTitleText("時(shí)間 (s)");
         m_chart- >addAxis(axisX, Qt::AlignBottom);
         m_series1- >attachAxis(axisX);
         m_series2- >attachAxis(axisX);
         m_series3- >attachAxis(axisX);
     ?
         // 設(shè)置縱軸屬性
         QValueAxis *axisY = new QValueAxis;
         axisY- >setRange(0, 60);
         axisY- >setTitleText("溫度 (℃)");
         m_chart- >addAxis(axisY, Qt::AlignLeft);
         m_series1- >attachAxis(axisY);
         m_series2- >attachAxis(axisY);
         m_series3- >attachAxis(axisY);
     ?
         // 使圖表自適應(yīng)大小,確保曲線始終可見(jiàn)
           m_chart- >createDefaultAxes();
           m_chart- >axisX()- >setRange(0, 30);
           m_chart- >axisY()- >setRange(0, 60);
     ?
     ?
         // 定時(shí)更新數(shù)據(jù)
         connect(m_timer, &QTimer::timeout, this, &Widget::updateChartData);
     ?
         // 將圖表添加到ChartView中
         QChartView* chartView = new QChartView(m_chart);
     ?
         // 設(shè)置 ChartView 交互模式為拖拽
         chartView- >setRubberBand(QChartView::HorizontalRubberBand);
         chartView- >setRenderHint(QPainter::Antialiasing);
         chartView- >setDragMode(QGraphicsView::ScrollHandDrag);
     ?
         chartView- >setRenderHint(QPainter::Antialiasing);
     ?
         //將視圖添加到布局
         ui- >view_verticalLayout- >addWidget(chartView);
     }
     ?
     ?
     ?
     Widget::~Widget()
     {
         delete ui;
     }
     ?
     ?
     void Widget::updateChartData()
     {
         // 更新時(shí)間計(jì)數(shù)
         m_timeCount++;
     ?
         // 在溫度曲線上增加一個(gè)點(diǎn),模擬溫度數(shù)據(jù)變化
         QPointF p1(m_timeCount, qrand() % 10 + 20);
         QPointF p2(m_timeCount, qrand() % 10 + 30);
         QPointF p3(m_timeCount, qrand() % 10 + 40);
         m_series1- >append(p1);
         m_series2- >append(p2);
         m_series3- >append(p3);
     ?
         // 清除多余的點(diǎn),只保留最新的30個(gè)數(shù)據(jù)點(diǎn)
         if (m_series1- >count() > 30) {
             m_series1- >removePoints(0, 1);
         }
         if (m_series2- >count() > 30) {
             m_series2- >removePoints(0, 1);
         }
         if (m_series3- >count() > 30) {
             m_series3- >removePoints(0, 1);
         }
     ?
         // 更新橫軸范圍
         updateAxisRange();
     }
     ?
     ?
     void Widget::updateAxisRange()
     {
         // 獲取橫軸范圍
         qreal minX = std::numeric_limits< qreal >::max();
         qreal maxX = std::numeric_limits< qreal >::min();
         foreach (QAbstractSeries *series, m_chart- >series()) {
             QXYSeries *xySeries = static_cast< QXYSeries* >(series);
             QPointF p1 = xySeries- >at(0);
             QPointF p2 = xySeries- >at(xySeries- >count() - 1);
     ?
             if (p1.x() < minX) {
                 minX = p1.x();
             }
             if (p2.x() > maxX) {
                 maxX = p2.x();
             }
         }
     ?
         // 更新橫軸范圍
         m_chart- >axisX()- >setRange(minX, maxX);
     }
     ?
     ?
     ?
     //開(kāi)始采集
     void Widget::on_pushButton_start_clicked()
     {
         m_timer- >start(1000); // 每隔1秒鐘更新一次數(shù)據(jù)
     }
     ?
     ?
     //停止采集
     void Widget::on_pushButton_stop_clicked()
     {
         m_timer- >stop(); //停止定時(shí)器
     }

【2】widget.h代碼

#ifndef WIDGET_H
     #define WIDGET_H
     ?
     #include < QWidget >
     ?
     // 包含line chart需要的頭文件
     #include < QtCharts/QChart >
     #include < QtCharts/QLineSeries >
     #include < QtCharts/QChartView >
     #include < QtCore/QRandomGenerator >
     #include < QValueAxis >
     #include < QScatterSeries >
     #include < QTimer >
     ?
     // 引用命名空間
     QT_CHARTS_USE_NAMESPACE
     ?
     QT_BEGIN_NAMESPACE
     namespace Ui { class Widget; }
     QT_END_NAMESPACE
     ?
     class Widget : public QWidget
     {
         Q_OBJECT
     ?
     public:
         Widget(QWidget *parent = nullptr);
         ~Widget();
     private slots:
         void updateChartData(); // 更新數(shù)據(jù)槽函數(shù)
         void updateAxisRange();
     ?
         void on_pushButton_start_clicked();
     ?
         void on_pushButton_stop_clicked();
     ?
     private:
         Ui::Widget *ui;
     ?
         QTimer *m_timer; // 定時(shí)器
     ?
         QChart *m_chart; // 圖表指針
     ?
         QLineSeries *m_series1; // 設(shè)備1溫度曲線
         QLineSeries *m_series2; // 設(shè)備2溫度曲線
         QLineSeries *m_series3; // 設(shè)備3溫度曲線
     ?
         int m_timeCount; // 時(shí)間計(jì)數(shù)
     };
     #endif // WIDGET_H

【3】UI文件代碼

image-20230529143214460

< ?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 >600< /width >
        < height >444< /height >
       < /rect >
      < /property >
      < property name="windowTitle" >
       < string >Widget< /string >
      < /property >
      < layout class="QVBoxLayout" name="verticalLayout" >
       < item >
        < widget class="QWidget" name="widget" native="true" >
         < property name="minimumSize" >
          < size >
           < width >100< /width >
           < height >100< /height >
          < /size >
         < /property >
         < layout class="QVBoxLayout" name="verticalLayout_2" >
          < 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 >
          < item >
           < layout class="QVBoxLayout" name="view_verticalLayout"/ >
          < /item >
         < /layout >
        < /widget >
       < /item >
       < item >
        < widget class="QWidget" name="widget_2" native="true" >
         < property name="maximumSize" >
          < size >
           < width >16777215< /width >
           < height >40< /height >
          < /size >
         < /property >
         < layout class="QHBoxLayout" name="horizontalLayout" >
          < 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 >
          < item >
           < widget class="QPushButton" name="pushButton_start" >
            < property name="text" >
             < string >開(kāi)始采集< /string >
            < /property >
           < /widget >
          < /item >
          < item >
           < widget class="QPushButton" name="pushButton_stop" >
            < property name="text" >
             < string >停止采集< /string >
            < /property >
           < /widget >
          < /item >
         < /layout >
        < /widget >
       < /item >
      < /layout >
     < /widget >
     < resources/ >
     < connections/ >
    < /ui >

【4】pro工程文件

QT       += core gui
    QT       += charts

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    CONFIG += c++11

    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS

    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

    SOURCES += \\
        main.cpp \\
        widget.cpp

    HEADERS += \\
        widget.h

    FORMS += \\
        widget.ui

    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • Qt
    Qt
    +關(guān)注

    關(guān)注

    1

    文章

    309

    瀏覽量

    38107
  • 可視化
    +關(guān)注

    關(guān)注

    1

    文章

    1203

    瀏覽量

    21040
  • 溫度變化
    +關(guān)注

    關(guān)注

    0

    文章

    5

    瀏覽量

    6501
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    Qt(C++)使用QChart靜態(tài)顯示3個(gè)設(shè)備溫度變化曲線

    QChart模塊是Qt Charts庫(kù)的基礎(chǔ),提供了用于創(chuàng)建和顯示各種類(lèi)型圖表的類(lèi)和接口。Qt Charts庫(kù)是一個(gè)功能豐富、易于使用的數(shù)據(jù)
    的頭像 發(fā)表于 05-29 14:47 ?2928次閱讀
    <b class='flag-5'>Qt</b>(<b class='flag-5'>C++</b>)使用<b class='flag-5'>QChart</b>靜態(tài)<b class='flag-5'>顯示</b><b class='flag-5'>3</b><b class='flag-5'>個(gè)</b><b class='flag-5'>設(shè)備</b>的<b class='flag-5'>溫度</b><b class='flag-5'>變化</b><b class='flag-5'>曲線</b>

    Labview,matlab混合編程如何動(dòng)態(tài)顯示曲線

    Labview,matlab混合編程如何動(dòng)態(tài)顯示曲線,如一階階躍響應(yīng)
    發(fā)表于 04-27 21:44

    FPGA 1602動(dòng)態(tài)顯示辦法

    我只會(huì)FPGA做靜態(tài)顯示,但是不知道該怎樣動(dòng)態(tài)顯示。求解決是不是在模塊內(nèi)設(shè)置一個(gè)變量啊,然后那個(gè)變量怎么變化,然后1602上面也變化。“電壓
    發(fā)表于 12-24 16:40

    labview怎么動(dòng)態(tài)顯示波形?

    本帖最后由 sunxuan 于 2015-1-11 15:08 編輯 請(qǐng)問(wèn)一下labview怎么動(dòng)態(tài)顯示個(gè)二維數(shù)組,比如說(shuō)(12,15),(9,7),(3,9),(20,23),(15,18),這五
    發(fā)表于 01-11 15:07

    動(dòng)態(tài)顯示電路的改進(jìn)

    本文主要講述的是動(dòng)態(tài)顯示電路的改進(jìn)。
    發(fā)表于 04-22 15:32 ?21次下載

    數(shù)碼管動(dòng)態(tài)顯示實(shí)驗(yàn)

    數(shù)碼管動(dòng)態(tài)顯示實(shí)驗(yàn)一、實(shí)驗(yàn)?zāi)康脑趯?shí)際的單片機(jī)系統(tǒng)中,往往需要多位顯示動(dòng)態(tài)顯示是一種最常見(jiàn)的多位顯示方法,應(yīng)用非常廣泛。本實(shí)驗(yàn)要求實(shí)驗(yàn)兩
    發(fā)表于 03-23 10:45 ?1w次閱讀

    共陽(yáng)型3動(dòng)態(tài)顯示電路圖

    下圖為共陽(yáng)型3動(dòng)態(tài)顯示電路圖
    發(fā)表于 07-31 11:38 ?3458次閱讀
    共陽(yáng)型<b class='flag-5'>3</b>位<b class='flag-5'>動(dòng)態(tài)顯示</b>電路圖

    數(shù)碼管(動(dòng)態(tài)顯示)【C語(yǔ)言版】

    數(shù)碼管(動(dòng)態(tài)顯示)【C語(yǔ)言版】數(shù)碼管(動(dòng)態(tài)顯示)【C語(yǔ)言版】數(shù)碼管(動(dòng)態(tài)顯示)【C語(yǔ)言版】數(shù)碼管(動(dòng)態(tài)顯示)【C語(yǔ)言版】
    發(fā)表于 12-29 15:51 ?0次下載

    動(dòng)態(tài)顯示-譯碼器片選實(shí)現(xiàn)【匯編版】

    動(dòng)態(tài)顯示-譯碼器片選實(shí)現(xiàn)【匯編版】動(dòng)態(tài)顯示-譯碼器片選實(shí)現(xiàn)【匯編版】動(dòng)態(tài)顯示-譯碼器片選實(shí)現(xiàn)【匯編版】
    發(fā)表于 12-29 15:51 ?0次下載

    C#教程之用樹(shù)型列表動(dòng)態(tài)顯示菜單

    C#教程之用樹(shù)型列表動(dòng)態(tài)顯示菜單,很好的C#資料,快來(lái)學(xué)習(xí)吧。
    發(fā)表于 04-20 09:59 ?11次下載

    MIN數(shù)碼管動(dòng)態(tài)顯示

    數(shù)碼管動(dòng)態(tài)顯示,MIN數(shù)碼管動(dòng)態(tài)顯示,MIN。
    發(fā)表于 05-03 10:48 ?9次下載

    TFT曲線顯示動(dòng)態(tài)曲線的原理和方法

    TFT曲線顯示動(dòng)態(tài)曲線的原理和方法
    發(fā)表于 09-20 14:27 ?17次下載
    TFT<b class='flag-5'>曲線</b><b class='flag-5'>顯示</b><b class='flag-5'>動(dòng)態(tài)</b><b class='flag-5'>曲線</b>的原理和方法

    數(shù)碼管的靜態(tài)與動(dòng)態(tài)顯示和74HC138及點(diǎn)陣的動(dòng)態(tài)顯示詳細(xì)資料說(shuō)明

    本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)碼管的靜態(tài)與動(dòng)態(tài)顯示和74HC138及點(diǎn)陣的動(dòng)態(tài)顯示詳細(xì)資料說(shuō)明。
    發(fā)表于 04-12 18:27 ?5次下載
    數(shù)碼管的靜態(tài)與<b class='flag-5'>動(dòng)態(tài)顯示</b>和74HC138及點(diǎn)陣的<b class='flag-5'>動(dòng)態(tài)顯示</b>詳細(xì)資料說(shuō)明

    基于89C51單片機(jī)的1602液晶動(dòng)態(tài)顯示源程序

    基于89C51單片機(jī)的1602液晶動(dòng)態(tài)顯示源程序
    發(fā)表于 05-16 10:33 ?1次下載

    基于QT5+OpenCV+OpenVINO C++的應(yīng)用打包過(guò)程

    我用QT C++寫(xiě)了一個(gè)YOLOv5模型推理演示應(yīng)用。
    的頭像 發(fā)表于 01-26 10:17 ?1472次閱讀
    基于<b class='flag-5'>QT</b>5+OpenCV+OpenVINO <b class='flag-5'>C++</b>的應(yīng)用打包過(guò)程