QwtDemo-利⽤qwt 进⾏实时曲线绘制实例
最近项⽬⽤到在win下绘制实时动态曲线,采⽤qwt进⾏绘制,我的qwt库是⽤Qtcreator进⾏编译的,然后⼿动将相应的头⽂件动态库放到Qt的安装⽬录下,由于采⽤的编译器不同,使得QtCreator的设计者模式⾥边不能直接⽤QwtPlot等插件,但右击ui界⾯,选择qDesigner 打开,便会出现QwtPlot等插件,这时候在⾥边拖⼊⼀个QwtPlot控件,再回到QtCreator的设计者模式,便可⽤代码实现了。绘制前的准备
绘制函数
mainwindow.h
mainwindow.cpp [cpp]
01. #ifndef MAINWINDOW_H 02. #define MAINWINDOW_H 03. 04. #include <QMainWindow> 05. #include <QTimer> 06. #include <QTime> 07. #include <qwt_plot.h> 08. #include <qwt_plot_curve.h> 09. #include <qwt_legend.h> 10. #include <math.h> 11. #include <qwt_plot_zoomer.h> 12. #include <qwt_plot_panner.h> 13. #include <qwt_plot_magnifier.h> 14. #include <qwt_plot_grid.h> 15. #include <qwt_scale_draw.h> 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 tupRealtimeDataDemo(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
[cpp]
老艾堂01. #include "mainwindow.h" 02. #include "ui_mainwindow.h" 03. 04. 05. 06. 07. MainWindow::MainWindow(QWidget *parent) : 08. QMainWindow(parent),
09. ui(new Ui::MainWindow)
10. {
11. ui->tupUi(this);
12. tupRealtimeDataDemo(ui->qwtPlot);
13. }
14.
15.
16.
17. MainWindow::~MainWindow()
18. {
19. delete ui;
20. }
21.
22.
23.
24. void MainWindow::tupRealtimeDataDemo(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->tTitle(demoName);
专题报告怎么写
36. qwtplot->tCanvasBackground(Qt::gray);//背景
37. qwtplot->inrtLegend(new QwtLegend(),QwtPlot::RightLegend);//标签
38.
39. curve = new QwtPlotCurve();
40. curve->tTitle("肌电信号");//曲线名字
41. curve->tPen( Qt::yellow, 1 );//曲线的颜⾊宽度;
42.
43. QTime curtime;
44. curtime=curtime.currentTime();
45. qwtplot->tAxisTitle(QwtPlot::xBottom, " System Uptime");
46. qwtplot->tAxisTitle(QwtPlot::yLeft,"EMG");
47. qwtplot->tAxisScale(QwtPlot::yLeft,-2,2,1);
48. qwtplot->tAxisScale(QwtPlot::xBottom,-5,0,1);
49.
50.
51.
52. QwtPlotZoomer *zoomer = new QwtPlotZoomer( qwtplot->canvas() );
53. zoomer->tRubberBandPen( QColor( Qt::blue ) );
54. zoomer->tTrackerPen( QColor( Qt::black ) );
世界旦夕之间55. zoomer->tMouPattern(QwtEventPattern::MouSelect2,Qt::RightButton, Qt::ControlModifier );
56. zoomer->tMouPattern(QwtEventPattern::MouSelect3,Qt::RightButton );
57. QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( qwtplot->canvas() ); //默认的滑轮及右键缩放功
能图形的整体缩放
58.
59. // magnifier->tMouButton(Qt::LeftButton); //设置哪个按钮与滑轮为缩放画布如果不设置(注册掉当前⾏)按
钮默认为滑轮以及右键为缩放
60.
61. QwtPlotGrid *grid = new QwtPlotGrid();
62. grid->enableX( true );//设置⽹格线
63. grid->enableY( true );
64. grid->tMajorPen( 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.
注意:(1)qt设计者模式下不能显⽰QwtPlot控件,但运⾏后可见;
(2)⽤QtCreator编译Qwtt时,最好编译成Relea模式,编写qwt的程序时也要编译成Relea模式,否则会出现编译错误;
(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 86. 87. //⽤于更新ydata,实际情况就是read 数据 88. void MainWindow::updatedataSlot(){ 89. static QTime dataTime(QTime::currentTime()); 90. long int eltime = dataTime.elapd(); 91. static int lastpointtime = 0; 92. 93. int size = (eltime - lastpointtime); 94. 95. 96. if (size>0){//有数据传⼊ 97. a(ydata.begin(),ydata.begin()+size);//擦除多余的数据 98. for (int i=1;i<size+1;i++){ 99. ydata.append(getData((((double )lastpointtime+i)/1000))); 100. } 101. lastpointtime = eltime; 102. } 103. 104. curve->tSamples(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 conds 113. { 114. u
i->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. }
122.
>关于雨的诗句