前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Qt编程】基于QWT的曲线绘制及图例显示操作

【Qt编程】基于QWT的曲线绘制及图例显示操作

作者头像
bear_fish
发布2018-09-20 12:30:09
5.7K0
发布2018-09-20 12:30:09
举报

http://blog.csdn.net/tengweitw/article/details/41911035

    在《QWT在QtCreator中的安装与使用》一文中,我们完成了QWT的安装,这篇文章我们讲讲基础曲线的绘制功能。

首先,我们新建一个Qt应用程序,然后一路默认即可。这时,你会发现总共有:mainwindow.h,mainwindow.cpp,main.cpp,mainwindow.ui四个文件。

然后,选中项目,添加新文件,添加一个c++类,我们假设命名为PlotLines,基类选择QwtPlot,选择继承自QWidget。

接着,在pro文件中添加

                                         INCLUDEPATH +=D:\Qt\Qt5.3.0\5.3\msvc2010_opengl\include\QWT                                          LIBS+= -lqwtd 注意,我这里是将绘制曲线单独用一个类PlotLines表示的,而不是向参考实例一样是直接放在其他类的内部。所以这里我们需要在类的头文件中添加关键性语句:     #define QWT_DLL

最后,在主文件main.cpp中添加我们类的头文件,并在函数中生成该类的实例并显示,修改后的main.cpp文件如下所示:

[cpp] view plaincopy

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include"plotlines.h"
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7. //    MainWindow w;//这里的主窗口我们没有使用,当然也可以在主窗口中显示曲线
  8. //    w.show();
  9.     PlotLines line;  
  10.     line.show();  
  11. return a.exec();  
  12. }  
代码语言:javascript
复制
PlotLines.h文件如下:

[cpp] view plaincopy

  1. #ifndef PLOTLINES_H
  2. #define PLOTLINES_H
  3. #define QWT_DLL
  4. #include<qwt_plot.h>
  5. #include <qwt_plot_layout.h>
  6. #include <qwt_plot_canvas.h>
  7. #include <qwt_plot_renderer.h>
  8. #include <qwt_plot_grid.h>
  9. #include <qwt_plot_histogram.h>
  10. #include <qwt_plot_curve.h>
  11. #include <qwt_plot_zoomer.h>
  12. #include <qwt_plot_panner.h>
  13. #include <qwt_plot_magnifier.h>
  14. #include <qwt_legend.h>
  15. #include <qwt_legend_label.h>
  16. #include <qwt_column_symbol.h>
  17. #include <qwt_series_data.h>
  18. #include <qpen.h>
  19. #include <qwt_symbol.h>
  20. #include <qwt_picker_machine.h>
  21. class PlotLines : public QwtPlot  
  22. {  
  23.     Q_OBJECT  
  24. public:  
  25. explicit PlotLines(QWidget *parent = 0);  
  26. private Q_SLOTS:  
  27. void showItem(const QVariant &itemInfo, bool on);//点击图例,显示相应的曲线
  28. };  
  29. #endif // PLOTLINES_H
代码语言:javascript
复制
PlotLines.cpp文件如下:

[cpp] view plaincopy

  1. #include "plotlines.h"
  2. PlotLines::PlotLines(QWidget *parent) :  
  3.     QwtPlot(parent)  
  4. {  
  5.     setTitle("图的标题");  
  6. //---------设置画布---------//
  7.     QwtPlotCanvas *canvas=new QwtPlotCanvas();  
  8.     canvas->setPalette(Qt::white);  
  9.     canvas->setBorderRadius(10);  
  10.     setCanvas( canvas );  
  11.     plotLayout()->setAlignCanvasToScales( true );  
  12. //-----------设置x,y坐标和范围--------------//
  13.     setAxisTitle( QwtPlot::yLeft, "ylabel" );  
  14.     setAxisTitle( QwtPlot::xBottom, "xlabel" );  
  15.     setAxisScale(QwtPlot::yLeft,0.0,10.0);  
  16.     setAxisScale(QwtPlot::xBottom,0.0,10.0);  
  17. //----------------设置栅格线-------------------//
  18.     QwtPlotGrid *grid = new QwtPlotGrid;  
  19.     grid->enableX( true );//设置网格线
  20.     grid->enableY( true );  
  21.     grid->setMajorPen( Qt::black, 0, Qt::DotLine );  
  22.     grid->attach( this );  
  23. //-----------------开始画图----------------------//
  24.     QwtPlotCurve *curve=new QwtPlotCurve("curve");  
  25. // curve->setTitle( "信道"+QString( "%1 " ).arg( i+1));
  26.     curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
  27.     curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
  28.     QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,  
  29.     QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
  30.     curve->setSymbol( symbol );//添加样本点形状
  31.     QPolygonF points1, points2;//输入节点数据QPointF(x,y)
  32.     points1<<QPointF(1,1)<<QPointF(2,2)<<QPointF(3,3)<<QPointF(4,4)<<QPointF(5,5)<<QPointF(6,6)<<QPointF(7,7);  
  33.     points2<<QPointF(1,2)<<QPointF(2,3)<<QPointF(3,4)<<QPointF(4,5)<<QPointF(5,6)<<QPointF(6,7)<<QPointF(7,8);  
  34.     curve->setSamples(points1);  
  35.     curve->attach( this );  
  36.     curve->setLegendAttribute(curve->LegendShowLine);//显示图例的标志,这里显示线的颜色。
  37. //曲线2的形状采用默认,即不单独设置画笔的颜色、样本点的显示
  38.     QwtPlotCurve *curve2=new QwtPlotCurve("curve2");  
  39.     curve2->setSamples(points2);  
  40.     curve2->attach( this );  
  41.     curve2->setLegendAttribute(curve->LegendShowLine);  
  42. //--------------设置图例可以被点击来确定是否显示曲线-----------------------//
  43.     QwtLegend *legend = new QwtLegend;  
  44.     legend->setDefaultItemMode( QwtLegendData::Checkable );//图例可被点击
  45.     insertLegend( legend, QwtPlot::RightLegend );  
  46.     connect( legend, SIGNAL( checked( const QVariant &, boolint ) ),  
  47.         SLOT( showItem( const QVariant &, bool ) ) );//点击图例操作
  48.     QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状,注意改变参数
  49. //  qDebug()<<items;
  50. for ( int i = 0; i < items.size(); i++ )  
  51.     {  
  52. if ( i == 0 )  
  53.         {  
  54. const QVariant itemInfo = itemToInfo( items[i] );  
  55.             QwtLegendLabel *legendLabel =  
  56.                 qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );  
  57. if ( legendLabel )  
  58.                 legendLabel->setChecked( true );//
  59.             items[i]->setVisible( true );  
  60.         }  
  61. else
  62.         {  
  63.             items[i]->setVisible( false );  
  64.         }  
  65.     }  
  66. this->resize(600,400);  
  67. this->replot();  
  68.     setAutoReplot( true );//设置自动重画,相当于更新
  69. }  
  70. //点击图例,显示相应的曲线
  71. void PlotLines::showItem(const QVariant &itemInfo, bool on)  
  72. {  
  73.     QwtPlotItem *plotItem = infoToItem( itemInfo );  
  74. if ( plotItem )  
  75.         plotItem->setVisible( on );  
  76. }  
代码语言:javascript
复制

其他的文件没有作任何改变,在此就不列出来了。显示结果如下图:

1、初始界面如下:

2、点击右上角的图例后:

代码语言:javascript
复制

本文所创建的PlotLines类,完成的功能如下:

1、坐标轴的绘制

2、根据数据点绘制相应的曲线

3、右上角的图例可以点击,并显示或隐藏对应曲线

原文:http://blog.csdn.net/tengweitw/article/details/41911035

作者:nineheadedbird

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年06月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档