Qwt使用之QwtPlot

来源:互联网 发布:工资制作软件 编辑:程序博客网 时间:2024/05/17 10:41
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继承的组件
 
QwtPlotItemplot能显示的类,如果想要实现自己绘画图形,要继承此类实现rtti和draw接口QwtPlotPanner平移器    (用鼠标左键平移)QwtPlotMagnifier 放大器    (用鼠标滚轮缩放)QwtPlotCanvas画布QwtScaleMap比例图---可以提供一个逻辑区域到实际区域的坐标转换QwtScaleWidget比例窗口QwtScaleDiv比例布局QwtLegent标注QwtPlotLayout布局管理器QwtScaleDraw自画坐标轴
 
 

QwtPlotCure简介

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

 

[cpp] view plaincopy
  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();  
0 0