这是一个简单的总结,如果有问题,欢迎留言。
效果图:
绘制前的准备
.配置QWt 建议参考博客 http://blog.youkuaiyun.com/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)
{
//初始化xdata,x对应长度为5的坐标,y初始全为0
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() ); //默认的滑轮及右键缩放功能 图形的整体缩放
// magnifier->setMouseButton(Qt::LeftButton); //设置哪个按钮与滑轮为缩放画布 如果不设置(注册掉当前行)按钮默认为滑轮以及右键为缩放
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);
}
/**
* @brief getData
* @param inteval
* @return
* 获取一个值 模拟串口接收到的值
*/
double MainWindow::getData(double time){
double s = qCos( time * M_PI * 2 ) ;
return s;
}
//用于更新ydata,实际情况就是read数据
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) // average fps over 2 seconds
{
ui->statusBar->showMessage(
QString("%1 FPS")
.arg(frameCount/fpstime, 0, 'f', 0)
, 0);
lastFpsKey = (double)eltime/1000.0;
frameCount = 0;
}
}
有不明白的地方欢迎留言!