qwt学习一

53 篇文章 2 订阅
53 篇文章 36 订阅

学习这个东西方向对了,就对了一半

今天我开始学习基于qt库的一个开源的绘制2维的统计图的库--qwt。

我们画东西首先要有一个容器,不然都是徒劳,今天我们就介绍这个类--QwtPlot

它继承自QFrame和QwtPlotDict,QFrame提供一个QWidget的框架,QwtPlotDict为QwtPlot管理在其中的plot items,就是绘制的项。在QwtPlot上我们可以绘制无限多个的plot items,这些plot items可以是曲线,标记,格子以及继承自QwtPlotItem的子类。一个QwtPlot可以有四个轴,每个plot item连接到x和y轴上。在轴上的比例变换可以使用QwtScaleDiv,对于plot items比例可以使用QwtScaleEngine来计算,在每个轴上,QwtScaleEngine可以被单独设置。

在QwtPlot中有两个枚举类型。

Axis,轴,5个值,一个QwtPlot除了x和y,还有top和bottom轴,第五个是axisCnt,轴数,枚举从0开始,第五个为4,说明一共四个轴。另一个是LegendPosition,图例的位置。

它有五个值,分别指定插入一个图例仔什么位置,四个都是和x和y轴的位置有关,最后一个是特殊的,它允许不在这个Plot中,就是外部的。


这是今天写的一个小例子

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#ifndef PLOT_H
#define PLOT_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_legend.h>
#include <qwt_plot_grid.h>
#include <qwt_picker_machine.h>




class Plot : public QwtPlot
{
    Q_OBJECT
public:
    explicit Plot(QWidget *parent = 0);
    ~Plot();
signals:
    
public slots:


private:
    QwtPlotCurve *sinCurve;
    QwtPlotCurve *cosCurve;
    QwtPlotPicker *picker;


    void initCanvas();
    void initAxes();
    void initCurves();
    
};

#endif // PLOT_H

/************************************************
*
*author:周翔
*e-mail:604487178@qq.com
*blog:http://blog.csdn.net/zhx6044
*
*
*************************************************/

#include "plot.h"


double _sin2(double x)
{
    return ::sin(2 * x);
}

/**
 * @brief The FunctionData class 将一个函数包装一下,就像一个函数对象一样,但是又不一样,没有基于运行符()重载
 * 而是基于多态,我们只需要继承它,实现相关的虚函数即可,它内部有自己的调用规则
 */
class FunctionData: public QwtSyntheticPointData
{
public:
    FunctionData(double(*y)(double)):
        QwtSyntheticPointData(100),
        d_y(y)
    {
    }

    virtual double y(double x) const
    {
        return d_y(x);
    }

private:
    double(*d_y)(double);
};


Plot::Plot(QWidget *parent) :
    QwtPlot(parent)
{
    initCanvas();
}
Plot::~Plot()
{

}

void Plot::initCanvas()
{
    //右键拖拽
    (new QwtPlotPanner(this->canvas()))->setMouseButton(Qt::RightButton);

    //y轴在放大的时候,坐标不变化
    (new QwtPlotMagnifier(this->canvas()))->setAxisEnabled(QwtPlot::yLeft,false);


    //一个选择器,十字线,以xBottom和yLeft坐标
    picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
                               QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
                               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)));

    setTitle("sin(x) and cos(x)");

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


    QwtPlotGrid *grid = new QwtPlotGrid;//网格
    grid->enableXMin(true);
    grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));//大格子
    grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));//大格子里的小格子
    grid->attach(this);




    initAxes();
    initCurves();
}


void Plot::initAxes()
{


    setAxisTitle(QwtPlot::yLeft, QObject::trUtf8("y 轴"));
    setAxisScale(QwtPlot::yLeft, -1.0, 1.0);

    setAxisTitle(QwtPlot::xBottom,QObject::trUtf8("x 轴"));
    setAxisScale(QwtPlot::xBottom,-5.0,5.0);
}

void Plot::initCurves()
{
    sinCurve = new QwtPlotCurve("y = sin(2x)");
    //切换渲染提示 启用抗锯齿
    sinCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
    sinCurve->setLegendAttribute(QwtPlotCurve::LegendShowLine);
    sinCurve->setPen(QPen(Qt::yellow));
    sinCurve->attach(this);


    cosCurve = new QwtPlotCurve("y = cos(x)");
    cosCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
    //这个会改变曲线在QwtLegend显示的样式,可以是线,矩形,符号,
    cosCurve->setLegendAttribute(QwtPlotCurve::LegendNoAttribute);
    cosCurve->setPen(QPen(Qt::red));
    cosCurve->attach(this);

    sinCurve->setData(new FunctionData(_sin2));
    cosCurve->setData(new FunctionData(::cos));

}




  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值