這是一個簡單的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 #------------------------------------------------- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets # 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
mainwindow.h
這個文件聲明了一個定時器的槽和一個獲得數據的方法
class MainWindow : public QMainWindow explicit MainWindow(QWidget *parent = 0); void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE; double getData(double time);
mainwindow.cpp
主程序中配置好Qchart,QLineSeries是用來存放數據。更新的工作放在定時器的槽中。其中
m_series->setUseOpenGL(true);//openGl 加速
采用openGL加速,繪圖速度加快
#include "ui_mainwindow.h" #include "QtCharts/QChart" //#include <QtCharts/QChartGlobal> //QList<double> dataList;//存儲業務數據 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),//默認初始化? 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->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); double MainWindow::getData(double time){ double s = qCos( time * M_PI * 2 ) ; 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; QVector<QPointF> oldPoints = m_series->pointsVector();//Returns the points in the series as a vector for(int i=size;i<oldPoints.count();++i){ points.append(QPointF(i-size ,oldPoints.at(i).y()));//替換數據用 qint64 sizePoints = points.count(); points.append(QPointF(k+sizePoints,getData((((double)lastpointtime+k+1)/1000)))); m_series->replace(points); MainWindow::~MainWindow()
main.cpp
沒什么好說的
int main(int argc, char *argv[]) QApplication a(argc, argv);
mainwindow.ui
這個里自己放一個vertical Layout
這只是我根據別人的程序寫的簡單demo,有問題歡迎討論。
demo文件:
鏈接:http://pan.baidu.com/s/1cKXtIQ 密碼:5sm2
|