QwtPlot解析

QwtPlot类是一个二维绘图部件,继承自QFrame 和 QwtPlotDict。不过严格的说来,它只是一个视图窗口,真正的绘制设备是它的中心部件 QwtPlotCanvas类。在QwtPlot的画布上可以显示不限数量的图元项(plot items)。这些图元项可以是曲线(QwtPlotCurve),标签(QwtPlotMarker),网格(QwtPlotGrid),或者其它任意的从QwtPlotItem派生出来的子类。 一个QwtPlot有四条坐标抽,每一个项都依附于X轴或者Y轴。每一个轴的刻度可以通过set (QwtScaleDiv)或者根据绘制的图元通过算法(QwtScaleEngine)单独配置。

       QwtPlotDict类是一个Item的字典,用于管理添加到QwtPlot上的所有图元项。QwtPlotDict 按照Z值的递增顺序组织items. 如果autoDelete()设置为可用,所有依附的items会在QwtPlotDict的析构函数中被删除。

       QwtPlotCanvas类:QwtPlot的画布【Canvas of a QwtPlot】,继承自QFrame。

      在QwtPlot上所有图元(QwtPlotItem)的绘制都是源于QwtPlotCanvas类的paintEvent(). paintEvent()调用QwtPlotCanvas::drawCanvas(), QwtPlotCanvas::drawCanvas() 调用 QwtPlot::drawCanvas( QPainter *painter ), QwtPlot::drawCanvas( QPainter *painter )会调用QwtPlot::drawItems(), QwtPlot::drawItems()会调用所有图元的纯虚函数QwtPlotItem::draw() = 0; 绘制图元自己。

      因此,从上面的绘制流程来看,QwtPlot只是一个呈现图元的视口,相当于View; 而QwtPlotCanvas 才是真正的画布(即绘制设备),它是QwtPlot的一个成员,或者说中心部件,用于展现所有的图元,相当于QGraphicsView::setViewport( QWidget * widget ); 【从QAbstractScrollArea继承而来】中的widget;QwtPlotDict是一个非常重要的类,它被QwtPlot继承,用于协助QwtPlot管理所有的图元项,感觉相当于Scene; QwtPlotItem 即为图元,相当于Item。

     如何添加一个图元?
     Qwt中添加一个图元的方式不同于Qt的GraphicsView框架。QwtPlot本身并不具备添加图元的操作,一个图元自己有权力决定被添加到那个QwtPlot,图元通过以下两个接口实现这项功能。
    void attach( QwtPlot *plot );
    void detach();

看看这两个接口的具体实现:

[cpp] view plain copy
  1. /*! 
  2.   \brief Attach the item to a plot. 
  3.  
  4.   This method will attach a QwtPlotItem to the QwtPlot argument. It will first 
  5.   detach the QwtPlotItem from any plot from a previous call to attach (if 
  6.   necessary). If a NULL argument is passed, it will detach from any QwtPlot it 
  7.   was attached to. 
  8.  
  9.   \param plot Plot widget 
  10.   \sa detach() 
  11. */  
  12. void QwtPlotItem::attach( QwtPlot *plot )  
  13. {  
  14.     if ( plot == d_data->plot )  
  15.         return;  
  16.   
  17.     // remove the item from the previous plot  
  18.   
  19.     if ( d_data->plot )  
  20.     {  
  21.         if ( d_data->plot->legend() )  
  22.             d_data->plot->legend()->remove( this );  
  23.   
  24.         d_data->plot->attachItem( thisfalse );  
  25.   
  26.         if ( d_data->plot->autoReplot() )  
  27.             d_data->plot->update();  
  28.     }  
  29.   
  30.     d_data->plot = plot;  
  31.   
  32.     if ( d_data->plot )  
  33.     {  
  34.         // insert the item into the current plot  
  35.   
  36.         d_data->plot->attachItem( thistrue );  
  37.         itemChanged(); // 这个函数里面实现了QwtPlot的更新和图元对应示例图QwtLegendItem的更新  
  38.     }  
  39. }  
  40.   
  41. /*! 
  42.    \brief This method detaches a QwtPlotItem from any  
  43.           QwtPlot it has been associated with. 
  44.  
  45.    detach() is equivalent to calling attach( NULL ) 
  46.    \sa attach() 
  47. */  
  48. void QwtPlotItem::detach()  
  49. {  
  50.     attach( NULL );  
  51. }  

再看看itemChanged()的实现代码,有助于我们理解QwtPlotItem 和 QwtLegend 是如何实现绑定的。

[cpp] view plain copy
  1. /*! 
  2.    Update the legend and call QwtPlot::autoRefresh for the 
  3.    parent plot. 
  4.  
  5.    \sa updateLegend() 
  6. */  
  7. void QwtPlotItem::itemChanged()  
  8. {  
  9.     if ( d_data->plot )  
  10.     {  
  11.         if ( d_data->plot->legend() )  
  12.             updateLegend( d_data->plot->legend() );   
  13.   
  14.         d_data->plot->autoRefresh();  
  15.     }  
  16. }  

上面的updateLegend()是在QwtLegendItemManager中定义的纯虚函数:

[cpp] view plain copy
  1. virtual void QwtLegendItemManager::updateLegend( QwtLegend *legend ) const = 0;  

在QwtPlotItem及其派生类中会根据需要重新实现updateLegend( QwtLegend *legend )函数,实现代码中会为QwtPlotItem在QwtLegend中创建(通过legendItem())对应的示例图示。

而legendItem()是在QwtLegendItemManager中定义的纯虚函数:

[cpp] view plain copy
  1. virtual QWidget *legendItem() const = 0; 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
转自http://blog.csdn.net/junzhepan/article/details/8507249
QwtPlot是用来绘制二维图像的widget。在它的画板上可以无限制的显示绘画组件。绘画组件可以是曲线(QwtPlotCurve)、标记(QwtPlotMarker)、网格(QwtPlotGrid)、或者其它从QwtPlotItem继承的组件。
 

QwtPlot拥有4个axes(轴线)

yLeft 
Y axis left of the canvas.
yRight  Y axis right of the canvas.
xBottom  X axis below the canvas.
xTop  X axis above the canvas.
 

常用函数接口

setAxisTitle 设置轴标题
enableAxis 主要是显示xTop,yRight坐标轴
setAxisMaxMajor 设置某个某个坐标轴扩大比例尺的最大间隔数目
setAxisMaxMinor 设置某个某个坐标轴缩小比例尺的最大间隔数目
setAxisScale 禁用自动缩放比例尺,为某个坐标轴指定一个修改的比例尺
insertLegend 添加图例(标注)
 

常用组件

QwtPlotCurve 曲线
QwtPlotMarker 标记
QwtPlotGrid 网格
QwtPlotHistogram 直方图
other 从QwtPlotItem继承的组件
 
QwtPlotItem plot能显示的类,如果想要实现自己绘画图形,要继承此类实现rtti和draw接口
QwtPlotPanner 平移器    (用鼠标左键平移)
QwtPlotMagnifier  放大器    (用鼠标滚轮缩放)
QwtPlotCanvas 画布
QwtScaleMap 比例图---可以提供一个逻辑区域到实际区域的坐标转换
QwtScaleWidget 比例窗口
QwtScaleDiv 比例布局
QwtLegent 标注
QwtPlotLayout 布局管理器
QwtScaleDraw 自画坐标轴
 
 

QwtPlotCure简介

 
常见接口
setPen 设置画笔
setData 设置曲线的数据
setStyle 设置曲线形式,点、直线、虚线等等
setCurveAttribute 设置曲线属性,一般设置Fitted
attch 把曲线附加到QwlPlot上
 
下面看一个小例子,结果如下:
  QwtPlot解析_第1张图片
 
 
源代码:

 

[cpp] view plain copy print ?
  1. #include <QtGui/QApplication>   
  2. #include <Qt/qmath.h>   
  3. #include <QVector>   
  4. #include <qwt_plot.h>   
  5. #include <qwt_plot_curve.h>   
  6. #include <qwt_plot_magnifier.h>   
  7. #include <qwt_plot_panner.h>   
  8. #include <qwt_legend.h>   
  9.   
  10. int main(int argc, char *argv[])  
  11. {  
  12.     QApplication a(argc, argv);  
  13.   
  14.     QwtPlot plot(QwtText("CppQwtExample1"));  
  15.     plot.resize(640,400);  
  16.     //设置坐标轴的名称   
  17.     plot.setAxisTitle(QwtPlot::xBottom, "x->");  
  18.     plot.setAxisTitle(QwtPlot::yLeft, "y->");  
  19.     //设置坐标轴的范围   
  20.     plot.setAxisScale(QwtPlot::xBottom, 0.0, 2.0 * M_PI);  
  21.     plot.setAxisScale(QwtPlot::yLeft, -1.0, 1.0);  
  22.     //设置右边标注   
  23.     plot.insertLegend(new QwtLegend(), QwtPlot::RightLegend);  
  24.   
  25.     //使用滚轮放大/缩小   
  26.     (voidnew QwtPlotMagnifier( plot.canvas() );  
  27.   
  28.     //使用鼠标左键平移   
  29.     (voidnew QwtPlotPanner( plot.canvas() );  
  30.   
  31.     //计算曲线数据   
  32.     QVector<double> xs;  
  33.     QVector<double> ys;  
  34.     for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0))  
  35.     {  
  36.         xs.append(x);  
  37.         ys.append(qSin(x));  
  38.     }  
  39.     //构造曲线数据   
  40.     QwtPointArrayData * const data = new QwtPointArrayData(xs, ys);  
  41.     QwtPlotCurve curve("Sine");  
  42.     curve.setData(data);//设置数据   
  43.     curve.setStyle(QwtPlotCurve::Lines);//直线形式   
  44.     curve.setCurveAttribute(QwtPlotCurve::Fitted, true);//是曲线更光滑  
  45.     curve.setPen(QPen(Qt::blue));//设置画笔   
  46.   
  47.     curve.attach(&plot);//把曲线附加到plot上   
  48.   
  49.     plot.show();  
  50.   
  51.     return a.exec();  

你可能感兴趣的:(QwtPlot,QwtPlotItem,QwtPlotCurve,QwtPlotCanvas,QwtPlotDict)