300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > QT绘制实时动态曲线——qcustomplot使用(一)

QT绘制实时动态曲线——qcustomplot使用(一)

时间:2021-04-05 06:02:16

相关推荐

QT绘制实时动态曲线——qcustomplot使用(一)

下载与安装

要使用qcustomplot,首先需要在官网下载qcustomplot。

下载链接。

下载完成后,打开解压压缩包,添加qcustomplot.c和qcustomplot.h到目标工程文件。

打开工程文件中的examples中的实例。

可以看到有许多工程示例:

动态实时曲线绘制就需要用到setupRealtimeDataDemo这个demo:

源码如下:

void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot){demoName = "Real Time Data Demo";// include this section to fully disable antialiasing for higher performance:/*customPlot->setNotAntialiasedElements(QCP::aeAll);QFont font;Afont.setStyleStrategy(QFont::NoAntialias);customPlot->xAxis->setTickLabelFont(font);customPlot->yAxis->setTickLabelFont(font);customPlot->legend->setFont(font);*/customPlot->addGraph(); // blue linecustomPlot->graph(0)->setPen(QPen(QColor(40, 110, 255)));customPlot->addGraph(); // red linecustomPlot->graph(1)->setPen(QPen(QColor(255, 110, 40)));customPlot->addGraph(); // red linecustomPlot->graph(2)->setPen(QPen(QColor(90, 140, 40)));QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);timeTicker->setTimeFormat("%h:%m:%s");customPlot->xAxis->setTicker(timeTicker);customPlot->axisRect()->setupFullAxesBox();customPlot->yAxis->setRange(0, 10);// make left and bottom axes transfer their ranges to right and top axes:connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));// setup a timer that repeatedly calls MainWindow::realtimeDataSlot:connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));dataTimer.start(10); // Interval 0 means to refresh as fast as possible}

定时器溢出的槽函数:

void MainWindow::realtimeDataSlot(){static QTime timeStart = QTime::currentTime();// calculate two new data points:double key = timeStart.msecsTo(QTime::currentTime())/1000.0; // time elapsed since start of demo, in secondsstatic double lastPointKey = 0;if (key-lastPointKey > 0.002) // at most add point every 2 ms{// add data to lines:ui->customPlot->graph(0)->addData(key, qSin(key)+std::rand()/(double)RAND_MAX*1*qSin(key/0.3843));ui->customPlot->graph(1)->addData(key, qCos(key)+std::rand()/(double)RAND_MAX*0.5*qSin(key/0.4364));// rescale value (vertical) axis to fit the current data://ui->customPlot->graph(0)->rescaleValueAxis();//ui->customPlot->graph(1)->rescaleValueAxis(true);lastPointKey = key;}// make key axis range scroll with the data (at a constant range size of 8):ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);ui->customPlot->replot();// calculate frames per second:static double lastFpsKey;static int frameCount;++frameCount;if (key-lastFpsKey > 2) // average fps over 2 seconds{ui->statusBar->showMessage(QString("%1 FPS, Total Data points: %2").arg(frameCount/(key-lastFpsKey), 0, 'f', 0).arg(ui->customPlot->graph(0)->data()->size()+ui->customPlot->graph(1)->data()->size()), 0);lastFpsKey = key;frameCount = 0;}}

实现效果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。