Qt QChart,利用QChart绘制动态曲线

xiaoxiao2021-02-28  32

这是一个简单的demo,在更新数据的时候我采用了官方中audio这个example的方式。

效果图

程序

-------------------------------------------------------------------------------------

运行环境 :

Qt Creator 4.2.1

Based on Qt 5.8.0 (MSVC 2015, 32 bit)

--------------------------------------------------------------------------------------

QtChartsTest.pro

这个文件中唯一需要添加的是

QT += charts

其他可以不看

#------------------------------------------------- # # Project created by QtCreator 2017-07-11T10:12:57 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = QtChartsTest TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as 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 you use 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\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui QT += charts

mainwindow.h

这个文件声明了一个定时器的槽和一个获得数据的方法

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); protected: void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE; private: Ui::MainWindow *ui; double getData(double time); }; #endif // MAINWINDOW_H mainwindow.cpp

主程序中配置好Qchart,QLineSeries是用来存放数据。更新的工作放在定时器的槽中。其中

m_series->setUseOpenGL(true);//openGl 加速

采用openGL加速,绘图速度加快

#include "mainwindow.h" #include "ui_mainwindow.h" #include "QtCharts/QChart" #include "QLineSeries" #include "QValueAxis" #include "QTimer" #include "QTime" #include "QList" #include "qmath.h" #include "QPointF" #include "QDebug" //#include <QtCharts/QChartGlobal> #include "QChartView" QT_CHARTS_USE_NAMESPACE QChart *m_chart; QLineSeries *m_series; //QList<double> dataList;//存储业务数据 int maxSize = 5000; //QTimer updateTimer; int timeId; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),//默认初始化? ui(new Ui::MainWindow) { ui->setupUi(this); m_chart = new QChart; QChartView *chartView = new QChartView(m_chart); // v.setRubberBand(QChartView::HorizontalRubberBand); chartView->setRubberBand(QChartView::RectangleRubberBand); // chartView->setRubberBand(); m_series = new QLineSeries; m_chart->addSeries(m_series); for(int i=0;i<maxSize;++i){ m_series->append(i,0); } m_series->setUseOpenGL(true);//openGl 加速 qDebug()<<m_series->useOpenGL(); QValueAxis *axisX = new QValueAxis; axisX->setRange(0,maxSize); axisX->setLabelFormat("%g"); axisX->setTitleText("axisX"); QValueAxis *axisY = new QValueAxis; axisY->setRange(-1.5,1.5); axisY->setTitleText("axisY"); m_chart->setAxisX(axisX,m_series); m_chart->setAxisY(axisY,m_series); m_chart->legend()->hide(); m_chart->setTitle("demo"); QVBoxLayout *layout = ui->verticalLayout; layout->addWidget(chartView); timeId = startTimer(30); } double MainWindow::getData(double time){ double s = qCos( time * M_PI * 2 ) ; return s; } void MainWindow::timerEvent(QTimerEvent *event){ if(event->timerId()==timeId){//定时器到时间,//模拟数据填充 static QTime dataTime(QTime::currentTime()); long int eltime = dataTime.elapsed(); static int lastpointtime = 0; int size = (eltime - lastpointtime);//数据个数 qDebug()<<"size-->"<<size; if(isVisible()){ QVector<QPointF> oldPoints = m_series->pointsVector();//Returns the points in the series as a vector QVector<QPointF> points; for(int i=size;i<oldPoints.count();++i){ points.append(QPointF(i-size ,oldPoints.at(i).y()));//替换数据用 } qint64 sizePoints = points.count(); for(int k=0;k<size;++k){ points.append(QPointF(k+sizePoints,getData((((double)lastpointtime+k+1)/1000)))); } m_series->replace(points); lastpointtime = eltime; } } } MainWindow::~MainWindow() { delete ui; }

main.cpp

没什么好说的

#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

mainwindow.ui

这个里自己放一个vertical Layout

这只是我根据别人的程序写的简单demo,有问题欢迎讨论。

demo文件:

链接:http://pan.baidu.com/s/1cKXtIQ 密码:5sm2

转载请注明原文地址: https://www.6miu.com/read-80186.html

最新回复(0)