文章参考自: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
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
- #include <QTimer>
- #include <QTime>
- #include <qwt_plot.h>
- #include <qwt_plot_curve.h>
- #include <qwt_legend.h>
- #include <math.h>
- #include <qwt_plot_zoomer.h>
- #include <qwt_plot_panner.h>
- #include <qwt_plot_magnifier.h>
- #include <qwt_plot_grid.h>
- #include <qwt_scale_draw.h>
-
- namespace Ui {
- class MainWindow;
- }
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- void setupRealtimeDataDemo(QwtPlot *qwtplot);
-
- private:
- Ui::MainWindow *ui;
- QVector<double> xdata;
- QVector<double> ydata;
- QTimer updateTimer;
- QString demoName;
- QwtPlotCurve *curve ;
- double getData(double inteval);
-
- private slots:
- void updatedataSlot();
- };
-
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
-
-
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- setupRealtimeDataDemo(ui->qwtPlot);
- }
-
-
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
-
-
- void MainWindow::setupRealtimeDataDemo(QwtPlot *qwtplot)
- {
-
-
-
- for(int i=1;i<5001;i++){
- xdata.append(double(i)/1000-5);
- ydata.append(0);
- }
-
- demoName = "Real Time Data Demo";
- qwtplot->setTitle(demoName);
- qwtplot->setCanvasBackground(Qt::gray);
- qwtplot->insertLegend(new QwtLegend(),QwtPlot::RightLegend);
-
- curve = new QwtPlotCurve();
- curve->setTitle("肌电信号");
- curve->setPen( Qt::yellow, 1 );
-
- QTime curtime;
- curtime=curtime.currentTime();
- qwtplot->setAxisTitle(QwtPlot::xBottom, " System Uptime");
- qwtplot->setAxisTitle(QwtPlot::yLeft,"EMG");
- qwtplot->setAxisScale(QwtPlot::yLeft,-2,2,1);
- qwtplot->setAxisScale(QwtPlot::xBottom,-5,0,1);
-
-
-
- QwtPlotZoomer *zoomer = new QwtPlotZoomer( qwtplot->canvas() );
- zoomer->setRubberBandPen( QColor( Qt::blue ) );
- zoomer->setTrackerPen( QColor( Qt::black ) );
- zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier );
- zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );
- QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( qwtplot->canvas() );
-
-
-
- QwtPlotGrid *grid = new QwtPlotGrid();
- grid->enableX( true );
- grid->enableY( true );
- grid->setMajorPen( Qt::black, 0, Qt::DotLine );
- grid->attach(qwtplot);
-
- connect(&updateTimer,SIGNAL(timeout()),this,SLOT(updatedataSlot()));
- updateTimer.start(0);
-
-
- }
-
-
-
-
-
-
-
-
- double MainWindow::getData(double time){
-
- double s = qCos( time * M_PI * 2 ) ;
- return s;
- }
-
-
-
- void MainWindow::updatedataSlot(){
- static QTime dataTime(QTime::currentTime());
- long int eltime = dataTime.elapsed();
- static int lastpointtime = 0;
-
- int size = (eltime - lastpointtime);
-
-
- if(size>0){
- ydata.erase(ydata.begin(),ydata.begin()+size);
- for(int i=1;i<size+1;i++){
- ydata.append(getData((((double)lastpointtime+i)/1000)));
- }
- lastpointtime = eltime;
- }
-
- curve->setSamples(xdata,ydata);
- curve->attach(ui->qwtPlot);
- ui->qwtPlot->replot();
-
- static double lastFpsKey;
- static int frameCount;
- ++frameCount;
- double fpstime = (double)eltime/1000.0-lastFpsKey;
- if ( fpstime> 2)
- {
- ui->statusBar->showMessage(
- QString("%1 FPS")
- .arg(frameCount/fpstime, 0, 'f', 0)
- , 0);
- lastFpsKey = (double)eltime/1000.0;
- frameCount = 0;
- }
- }
注意:(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