使用qwt作曲线图

来源:互联网 发布:周杰伦代言的游戏 知乎 编辑:程序博客网 时间:2024/04/29 02:48

创建自己的QwtPlot

(1)     增加一个类比如curvePlotWidget,继承自QwtPlot

(2)代码示例

[cpp] view plaincopy
  1. curvePlotWidget::curvePlotWidget(QWidget *parent)  
  2.  : QwtPlot(parent)  
  3. {  
  4.  ui.setupUi(this);  
  5.   
  6.    
  7.   
  8. //设置一些窗口熟悉  
  9.  setFrameStyle(QFrame::NoFrame);  
  10.  setLineWidth(0);  
  11.  setCanvasLineWidth(2);  
  12.   
  13.  plotLayout()->setAlignCanvasToScales(true);  
  14.   
  15. //增加网格  
  16.   
  17.  QwtPlotGrid *grid = new QwtPlotGrid;  
  18.  grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));  
  19.  grid->attach(this);  
  20.   
  21. //设置画布背景  
  22.   
  23.  setCanvasBackground(QColor(29, 100, 141)); // nice blue  
  24.   
  25. //设置X与Y坐标范围  
  26.  setAxisScale(xBottom, 1, 75);  
  27.  setAxisScale(yLeft, -1, 1);  
  28.   
  29. //  
  30.   
  31.   
  32. //新建一个曲线对象  
  33.  QwtPlotCurve *pCurve=new QwtPlotCurve("curve1");  
  34.   
  35.    
  36.   
  37. //输入数据  
  38.   
  39.  QVector< double > xData;  
  40.   
  41.  QVector<double> yData;  
  42.   
  43.  for(int i=0;i<75;++i)  
  44.   xData.push_back(i+1);  
  45.   
  46.  yData<<1<<0.048634<<0.655211<<0.320122<<0.130912<<0.182503<<0.163217<<0.167857<<0.169706<<0.15244<<0.17136<<0.184516<<0.183185<<0.16788<<0.150819<<0.154223<<0.149134<<0.126398<<0.090325<<-0.017047<<0.184973<<0.113727<<0.072852<<0.054324<<0.04943<<0.036473<<0.042876<<0.048972<<0.04963<<0.052114<<0.056796<<0.060517<<0.07844<<0.066472<<0.079221<<0.06061<<-0.018855<<0.457584<<0.104125<<0.282665<<0.066127<<0.064099<<0.065944<<0.013025<<0.054401<<0.027663<<0.038911<<0.03153<<0.040123<<0.038832<<0.03919<<0.048258<<0.050396<<0.063897<<0.062202<<0.067778<<0.074743<<0.063545<<0.066624<<0.09162<<-0.022548<<0.037526<<0.04687<<0.04425<<0.046449<<0.038345<<0.051492<<0.033624<<0.030668<<0.075395<<-0.016367<<-0.039846<<0.021928<<0<<0;  
  47.   
  48.   
  49.  pCurve->setSamples(xData,yData);  
  50.   
  51.  pCurve->attach(this);  
  52.   
  53. //设置曲线颜色  
  54.  QPen pen;  
  55.  pen.setColor(QColor(255,0,0));  
  56.   
  57.  pCurve->setPen(pen);  
  58.    
  59.  //QwtPlotCurve::PaintAttribute  
  60.   
  61. //抗锯齿  
  62.   
  63. pCurve->setRenderHint(QwtPlotItem::RenderAntialiased,true);  
  64.   
  65.    
  66.   
  67. //增加缩放功能  
  68.   
  69. QwtPlotZoomer  *pZoomer= new QwtPlotZoomer(canvas());  
  70.   
  71. pZoomer->setRubberBandPen(QPen(Qt::red));  
  72.   
  73. //重绘  
  74.   
  75. replot();  
  76. }  

写下这些代码,就可以实现曲线绘制以及显示还有缩放功能,qwt还是挺强大的,所得界面如下图:

 

原创粉丝点击