Qt工作笔记-QCustomPlot让曲线动起来
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-QCustomPlot让曲线动起来
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
其實就是用了一個記時器,而且相當極端,把startTime搞成了0,真是可怕啊
運行截圖如下:
代碼如下:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>#include "qcustomplot.h"namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();void timerEvent(QTimerEvent* event);private:Ui::Widget *ui;QCPItemTracer *m_myPhaseTracer;QString m_myName;QVector<double> x,y;int pos;int m_timer;};#endif // WIDGET_Hwidget.cpp
#include "widget.h" #include "ui_widget.h"#include <QDebug> #include <math.h> #include <QTime>Widget::Widget(QWidget *parent) :QWidget(parent),x(100),y(100),ui(new Ui::Widget) {ui->setupUi(this);m_myName="我的折線圖";for(int i=0;i<100;i++){x[i]=i;y[i]=1;}pos=0;m_timer=startTimer(0);ui->plot->addGraph();ui->plot->graph()->setData(x,y);ui->plot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom); }Widget::~Widget() {delete ui; }void Widget::timerEvent(QTimerEvent* event){double secs = QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime());int n = 500;double phase = secs*5;double k = 3;QVector<double> x(n), y(n);for (int i=0; i<n; ++i){x[i] = i/(double)(n-1)*34 - 17;y[i] = qExp(-x[i]*x[i]/20.0)*qSin(k*x[i]+phase);}ui->plot->graph()->setData(x, y);ui->plot->replot(); }main.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }總結
以上是生活随笔為你收集整理的Qt工作笔记-QCustomPlot让曲线动起来的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-通过信号与槽实现定时器
- 下一篇: Qt工作笔记-Qt奇淫技巧把ToolBa