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

来源:互联网 发布:关键词价格查询源码 编辑:程序博客网 时间:2024/06/05 05:37
首先,我们新建一个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表示的,而不是向参考实例一样是直接放在其他类的内部。所以这里我们需要在类的头文件中添加关键性语句:

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

#include "mainwindow.h"#include <QApplication>#include"plotlines.h"int main(int argc, char *argv[]){QApplication a(argc, argv);//MainWindow w;//这里的主窗口我们没有使用,当然也可以在主窗口中显示曲线//w.show();PlotLines line;line.show();return a.exec();}
PlotLines.h文件如下:
#ifndef PLOTLINES_H#define PLOTLINES_H#define QWT_DLL#include<qwt_plot.h>#include <qwt_plot_layout.h>#include <qwt_plot_canvas.h>#include <qwt_plot_renderer.h>#include <qwt_plot_grid.h>#include <qwt_plot_histogram.h>#include <qwt_plot_curve.h>#include <qwt_plot_zoomer.h>#include <qwt_plot_panner.h>#include <qwt_plot_magnifier.h>#include <qwt_legend.h>#include <qwt_legend_label.h>#include <qwt_column_symbol.h>#include <qwt_series_data.h>#include <qpen.h>#include <qwt_symbol.h>#include <qwt_picker_machine.h>class PlotLines : public QwtPlot{Q_OBJECTpublic:explicit PlotLines(QWidget *parent = 0);private Q_SLOTS:void showItem(const QVariant &itemInfo, bool on);//点击图例,显示相应的曲线};#endif // PLOTLINES_H
PlotLines.cpp文件如下:
#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 &, bool, int ) ),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 );}

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

1、初始界面如下:

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

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

1、坐标轴的绘制

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

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

0 0