欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Qt 编程] 基于 QWT 的曲线绘制和图例显示操作基于 QWT 的曲线绘制和图例显示操作

最编程 2024-06-01 21:18:01
...
  • #include "plotlines.h"
  • PlotLines::PlotLines(QWidget *parent) :  
  •     QwtPlot(parent)  
  • {  
  •     setTitle("图的标题");  
  • //---------设置画布---------//
  •     QwtPlotCanvas *canvas=new QwtPlotCanvas();  
  •     canvas->setPalette(Qt::white);  
  •     canvas->setBorderRadius(10);  
  •     setCanvas( canvas );  
  •     plotLayout()->setAlignCanvasToScales( true );  
  • //-----------设置x,y坐标和范围--------------//
  •     setAxisTitle( QwtPlot::yLeft, "ylabel" );  
  •     setAxisTitle( QwtPlot::xBottom, "xlabel" );  
  •     setAxisScale(QwtPlot::yLeft,0.0,10.0);  
  •     setAxisScale(QwtPlot::xBottom,0.0,10.0);  
  • //----------------设置栅格线-------------------//
  •     QwtPlotGrid *grid = new QwtPlotGrid;  
  •     grid->enableX( true );//设置网格线
  •     grid->enableY( true );  
  •     grid->setMajorPen( Qt::black, 0, Qt::DotLine );  
  •     grid->attach( this );  
  • //-----------------开始画图----------------------//
  •     QwtPlotCurve *curve=new QwtPlotCurve("curve");  
  • // curve->setTitle( "信道"+QString( "%1 " ).arg( i+1));
  •     curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
  •     curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
  •     QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,  
  •     QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
  •     curve->setSymbol( symbol );//添加样本点形状
  •     QPolygonF points1, points2;//输入节点数据QPointF(x,y)
  •     points1<<QPointF(1,1)<<QPointF(2,2)<<QPointF(3,3)<<QPointF(4,4)<<QPointF(5,5)<<QPointF(6,6)<<QPointF(7,7);  
  •     points2<<QPointF(1,2)<<QPointF(2,3)<<QPointF(3,4)<<QPointF(4,5)<<QPointF(5,6)<<QPointF(6,7)<<QPointF(7,8);  
  •     curve->setSamples(points1);  
  •     curve->attach( this );  
  •     curve->setLegendAttribute(curve->LegendShowLine);//显示图例的标志,这里显示线的颜色。
  • //曲线2的形状采用默认,即不单独设置画笔的颜色、样本点的显示
  •     QwtPlotCurve *curve2=new QwtPlotCurve("curve2");  
  •     curve2->setSamples(points2);  
  •     curve2->attach( this );  
  •     curve2->setLegendAttribute(curve->LegendShowLine);  
  • //--------------设置图例可以被点击来确定是否显示曲线-----------------------//
  •     QwtLegend *legend = new QwtLegend;  
  •     legend->setDefaultItemMode( QwtLegendData::Checkable );//图例可被点击
  •     insertLegend( legend, QwtPlot::RightLegend );  
  •     connect( legend, SIGNAL( checked( const QVariant &, boolint ) ),  
  •         SLOT( showItem( const QVariant &, bool ) ) );//点击图例操作
  •     QwtPlotItemList items = itemList( QwtPlotItem::Rtti_PlotCurve );//获取画了多少条曲线,如果为获取其他形状,注意改变参数
  • //  qDebug()<<items;
  • for ( int i = 0; i < items.size(); i++ )  
  •     {  
  • if ( i == 0 )  
  •         {  
  • const QVariant itemInfo = itemToInfo( items[i] );  
  •             QwtLegendLabel *legendLabel =  
  •                 qobject_cast<QwtLegendLabel *>( legend->legendWidget( itemInfo ) );  
  • if ( legendLabel )  
  •                 legendLabel->setChecked( true );//
  •             items[i]->setVisible( true );  
  •         }  
  • else
  •         {  
  •             items[i]->setVisible( false );  
  •         }  
  •     }  
  • this->resize(600,400);  
  • this->replot();  
  •     setAutoReplot( true );//设置自动重画,相当于更新
  • }  
  • //点击图例,显示相应的曲线
  • void PlotLines::showItem(const QVariant &itemInfo, bool on)  
  • {  
  •     QwtPlotItem *plotItem = infoToItem( itemInfo );  
  • if ( plotItem )  
  •         plotItem->setVisible( on );  
  • }  
  • 推荐阅读