找回密码
 立即注册
收起左侧

一个关于从串口读取数据并动态显示的例子,关于画动态.....

2
回复
7029
查看
[复制链接]
累计签到:14 天
连续签到:1 天
来源: 2016-2-23 21:01:53 显示全部楼层 |阅读模式
1Qter豆
本帖最后由 malouqizu 于 2016-2-23 21:09 编辑

下面的两个文件是关于动态显示数据曲线的,我是新手看不懂,麻烦各位帮忙指导指导

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

累计签到:14 天
连续签到:1 天
2016-2-23 21:05:43 显示全部楼层
myplot.h

#ifndef DATAPLOT_H
#define DATAPLOT_H

#include <qwt_plot_canvas.h>
#include <qwt_picker_machine.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_picker.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_plot_grid.h>
#include <stdlib.h>
#include <qwt_painter.h>
#include <qwt_plot_marker.h>
#include <qwt_scale_widget.h>
#include <qwt_legend.h>
#include <qwt_scale_draw.h>i
#include <qwt_math.h>
//#include <qwt_Plot_zoomer.h>

const int PLOT_SIZE = 101;      // 0 to 200 绘制曲线x长度

class QwtPlotPicker;
class DataPlot : public QwtPlot
{
    Q_OBJECT

public:
    DataPlot(QWidget* = NULL);
    void UpPlotdate(double,double);
    void setMaxYaxisValue(double,int);
    double getMaxYaxisValue(int);
    void resetplot();
private:
    void alignScales();
    QwtPlotPicker *picker;
    double MaxZValue;
    double MaxFValue;
    double d_x[PLOT_SIZE];
    double d_y[PLOT_SIZE];
    double d_z[PLOT_SIZE];
};

#endif
回复

使用道具 举报

累计签到:14 天
连续签到:1 天
2016-2-23 21:06:21 显示全部楼层
myplot.cpp

#include "mydataplot.h"
DataPlot:ataPlot(QWidget *parent):
    QwtPlot(parent)
{
    // Disable polygon clipping
    QwtPainter::setDeviceClipping(false);

    // We don't need the cache here
    canvas()->setPaintAttribute(QwtPlotCanvas:aintCached, false);
    canvas()->setPaintAttribute(QwtPlotCanvas:aintPacked, false);

    alignScales();

    //  Initialize data
    for (int i = 0; i< PLOT_SIZE; i++)
    {
        d_x[i] = i;     // time axis
        d_y[i] = 0;
        d_z[i] = 0;
    }
    MaxZValue=10;
    MaxFValue=-10;

    //这个会根据画板中的图在RightLegend显示一个图例
    //insertLegend(new QwtLegend(),QwtPlot::BottomLegend);

    // Insert new curves
    QwtPlotCurve *cRight = new QwtPlotCurve(tr("通道A"));
    cRight->setRenderHint(QwtPlotItem::RenderAntialiased);
    cRight->attach(this);

    QwtPlotCurve *cLeft = new QwtPlotCurve(tr(""));
    cLeft->attach(this);

    // Set curve styles
    cRight->setPen(QPen(Qt::red));
    cLeft->setPen(QPen(Qt::blue));

    // Attach (don't copy) data. Both curves use the same x array.
//   cRight->setData(d_x, d_y, PLOT_SIZE);
  //  cLeft->setData(d_x, d_z, PLOT_SIZE);

    // 设置坐标轴刻度
    setAxisTitle(QwtPlot::xBottom, tr("时间(/50mS)"));
    setAxisScale(QwtPlot::xBottom, 0, 100);

    setAxisTitle(QwtPlot::yLeft, tr("输入电压值(/mV)"));
    setAxisScale(QwtPlot::yLeft, -10,10);

    //右键平移曲线
    QwtPlotPanner *panner=new QwtPlotPanner(this->canvas());
    panner->setMouseButton(Qt::RightButton);

    //滚轮放大或缩小曲线,y轴在放大的时候,X坐标不变化
    QwtPlotMagnifier *magnifier=new QwtPlotMagnifier(this->canvas());
    magnifier->setMouseButton(Qt::NoButton);
    magnifier->setAxisEnabled(QwtPlot::xBottom,false);

    //QwtPlotZoomer *zoomer = new QwtPlotZoomer(this->canvas());
    //zoomer->setRubberBandPen(QColor( Qt::yellow));
    //zoomer->setTrackerPen(QColor( Qt::yellow ) );
    //zoomer->setMousePattern(QwtEventPattern::MouseSelect2,Qt::RightButton, Qt::ControlModifier);
    //zoomer->setMousePattern(QwtEventPattern::MouseSelect3,Qt::RightButton );

    //一个选择器,十字线,以xBottom和yLeft坐标
//    picker = new QwtPlotPicker(QwtPlot::xBottom,QwtPlot::yLeft,1,
                              //    QwtPlotPicker::CrossRubberBand,QwtPicker::ActiveOnly,
                               //   this->canvas());

    //picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽点起作用
    picker->setRubberBandPen(QPen(QColor(Qt::white)));
    picker->setTrackerPen(QColor(Qt::yellow));

    //设置背景色
    setAutoFillBackground(true);
    this->canvas()->setPalette(QPalette (QColor(Qt::darkCyan)));
    //设置网格
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableXMin(true);
    grid->setMajorPen(QPen(Qt::white, 0, Qt:otLine));//大格子
    grid->setMinorPen(QPen(Qt::gray, 0 , Qt:otLine));//大格子里的小格子
    grid->attach(this);
}


void DataPlot::alignScales()
{
   // canvas setFrameStyle(QFrame::Box | QFrame:lain );
    //canvas->setLineWidth(1);//边框宽度

    for ( int i = 0; i < QwtPlot::axisCnt; i++ )
    {
        QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
        if ( scaleWidget )
            scaleWidget->setMargin(0);

        QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
        if ( scaleDraw )
            scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
    }
}

void DataPlot::UpPlotdate(double x1,double x2)
{
    for ( int i = PLOT_SIZE - 1 ; i > 0; i-- )
        d_y[i] = d_y[i-1];

    d_y[0]=x1;

    //for ( int j = 0; j < PLOT_SIZE - 1; j++ )
    //  d_z[j] = d_z[j+1];

    d_z[PLOT_SIZE - 1]=x2;

    replot();
}

void DataPlot::setMaxYaxisValue(double value,int flag)
{
    if(flag==1)
    {
        MaxZValue=value;
        setAxisScale(QwtPlot::yLeft, MaxFValue, MaxZValue);
    }
    else if(flag==2)
    {
        MaxFValue=value;
        setAxisScale(QwtPlot::yLeft, MaxFValue, MaxZValue);
    }

}

double DataPlot::getMaxYaxisValue(int flag)
{   
    if(flag==1)
       return MaxZValue;
    else if(flag==2)
       return MaxFValue;
    else
       return 0;
}

void DataPlot::resetplot()
{
        MaxZValue=MaxFValue=0;
         for (int i = 0; i< PLOT_SIZE; i++)
    {
                if(d_y[i]>MaxZValue)
                MaxZValue=d_y[i];
        if(d_y[i]<MaxFValue)
                MaxFValue=d_y[i];
    }       
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了