QwtDemo-利用qwt进行实时曲线绘制实例

文章参考自:http://blog.csdn.net/hiccuphiccup/article/details/52879573;感谢博主。

最近项目用到在win下绘制实时动态曲线,采用qwt进行绘制,我的qwt库是用Qtcreator进行编译的,然后手动将相应的头文件动态库放到Qt的安装目录下,由于采用的编译器不同,使得QtCreator的设计者模式里边不能直接用QwtPlot等插件,但右击ui界面,选择qDesigner打开,便会出现QwtPlot等插件,这时候在里边拖入一个QwtPlot控件,再回到QtCreator的设计者模式,便可用代码实现了。

绘制前的准备

.配置QWt   建议参考博客  http://blog.csdn.NET/zhuyunfei/article/details/51008228 亲测有效


绘制函数

mainwindow.h
[cpp]  view plain  copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. #include   
  11. #include   
  12. #include   
  13. #include   
  14. #include   
  15. #include   
  16.   
  17. namespace Ui {  
  18. class MainWindow;  
  19. }  
  20.   
  21. class MainWindow : public QMainWindow  
  22. {  
  23.     Q_OBJECT  
  24.   
  25. public:  
  26.     explicit MainWindow(QWidget *parent = 0);  
  27.     ~MainWindow();  
  28.     void setupRealtimeDataDemo(QwtPlot *qwtplot);  
  29.   
  30. private:  
  31.     Ui::MainWindow *ui;  
  32.     QVector<double> xdata;  
  33.     QVector<double> ydata;  
  34.     QTimer updateTimer;  
  35.     QString demoName;  
  36.     QwtPlotCurve *curve ;  
  37.     double getData(double inteval);  
  38.   
  39. private slots:  
  40.     void updatedataSlot();  
  41. };  
  42.   
  43. #endif // MAINWINDOW_H  


mainwindow.cpp

[cpp]  view plain  copy
  1. #include "mainwindow.h"  
  2. #include "ui_mainwindow.h"  
  3.   
  4.   
  5.   
  6.   
  7. MainWindow::MainWindow(QWidget *parent) :  
  8.     QMainWindow(parent),  
  9.     ui(new Ui::MainWindow)  
  10. {  
  11.     ui->setupUi(this);  
  12.     setupRealtimeDataDemo(ui->qwtPlot);  
  13. }  
  14.   
  15.   
  16.   
  17. MainWindow::~MainWindow()  
  18. {  
  19.     delete ui;  
  20. }  
  21.   
  22.   
  23.   
  24. void MainWindow::setupRealtimeDataDemo(QwtPlot *qwtplot)  
  25. {  
  26.   
  27.   
  28.     //初始化xdata,x对应长度为5的坐标,y初始全为0  
  29.     for(int i=1;i<5001;i++){  
  30.         xdata.append(double(i)/1000-5);  
  31.         ydata.append(0);  
  32.     }  
  33.   
  34.     demoName = "Real Time Data Demo";  
  35.     qwtplot->setTitle(demoName);  
  36.     qwtplot->setCanvasBackground(Qt::gray);//背景  
  37.     qwtplot->insertLegend(new QwtLegend(),QwtPlot::RightLegend);//标签  
  38.   
  39.     curve = new QwtPlotCurve();  
  40.     curve->setTitle("肌电信号");//曲线名字  
  41.     curve->setPen( Qt::yellow, 1 );//曲线的颜色 宽度;  
  42.   
  43.     QTime curtime;  
  44.     curtime=curtime.currentTime();  
  45.     qwtplot->setAxisTitle(QwtPlot::xBottom, " System Uptime");  
  46.     qwtplot->setAxisTitle(QwtPlot::yLeft,"EMG");  
  47.     qwtplot->setAxisScale(QwtPlot::yLeft,-2,2,1);  
  48.     qwtplot->setAxisScale(QwtPlot::xBottom,-5,0,1);  
  49.   
  50.   
  51.   
  52.     QwtPlotZoomer *zoomer = new QwtPlotZoomer( qwtplot->canvas() );  
  53.     zoomer->setRubberBandPen( QColor( Qt::blue ) );  
  54.     zoomer->setTrackerPen( QColor( Qt::black ) );  
  55.     zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );  
  56.     zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );  
  57.     QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( qwtplot->canvas() );                 //默认的滑轮及右键缩放功能  图形的整体缩放  
  58.   
  59.     //    magnifier->setMouseButton(Qt::LeftButton);     //设置哪个按钮与滑轮为缩放画布  如果不设置(注册掉当前行)按钮默认为滑轮以及右键为缩放  
  60.   
  61.     QwtPlotGrid *grid = new QwtPlotGrid();  
  62.     grid->enableX( true );//设置网格线  
  63.     grid->enableY( true );  
  64.     grid->setMajorPen( Qt::black, 0, Qt::DotLine );  
  65.     grid->attach(qwtplot);  
  66.   
  67.     connect(&updateTimer,SIGNAL(timeout()),this,SLOT(updatedataSlot()));  
  68.     updateTimer.start(0);  
  69.   
  70.   
  71. }  
  72.   
  73.   
  74. /** 
  75.  * @brief getData 
  76.  * @param inteval 
  77.  * @return 
  78.  * 获取一个值  模拟串口接收到的值 
  79.  */  
  80. double MainWindow::getData(double time){  
  81.   
  82.     double s = qCos( time * M_PI * 2 ) ;  
  83.     return s;  
  84. }  
  85.   
  86.   
  87. //用于更新ydata,实际情况就是read数据  
  88. void MainWindow::updatedataSlot(){  
  89.     static QTime dataTime(QTime::currentTime());  
  90.     long int eltime = dataTime.elapsed();  
  91.     static int lastpointtime = 0;  
  92.   
  93.     int size = (eltime - lastpointtime);  
  94.   
  95.   
  96.     if(size>0){//有数据传入  
  97.         ydata.erase(ydata.begin(),ydata.begin()+size);//擦除多余的数据  
  98.         for(int i=1;i
  99.             ydata.append(getData((((double)lastpointtime+i)/1000)));  
  100.         }  
  101.         lastpointtime = eltime;  
  102.     }  
  103.   
  104.     curve->setSamples(xdata,ydata);  
  105.     curve->attach(ui->qwtPlot);  
  106.     ui->qwtPlot->replot();  
  107.   
  108.     static double lastFpsKey;  
  109.     static int frameCount;  
  110.     ++frameCount;  
  111.     double fpstime = (double)eltime/1000.0-lastFpsKey;  
  112.     if ( fpstime> 2) // average fps over 2 seconds  
  113.     {  
  114.         ui->statusBar->showMessage(  
  115.                     QString("%1 FPS")  
  116.                     .arg(frameCount/fpstime, 0, 'f', 0)  
  117.                     , 0);  
  118.         lastFpsKey = (double)eltime/1000.0;  
  119.         frameCount = 0;  
  120.     }  
  121. }  
注意:(1)qt设计者模式下不能显示QwtPlot控件,但运行后可见;

    (2)用QtCreator编译Qwtt时,最好编译成Release模式,编写qwt的程序时也要编译成Release模式,否则会出现编译错误;

            (3)记得要在.pro文件下加入Qwt的文件路径:(我的Qt安装到了C盘,所以相应的qwt库也放到了C盘)

LIBS += -L"C:\Qt\Qt5.5.0\5.5\mingw492_32\lib" -lqwt
INCLUDEPATH += C:\Qt\Qt5.5.0\5.5\mingw492_32\include\Qwt
 
  

你可能感兴趣的:(QwtDemo-利用qwt进行实时曲线绘制实例)