• 【Qt编程】基于QWT的曲线绘制及图例显示操作——有样点的实现功能


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

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

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

         接着,在pro文件中添加

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

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

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

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

    本文所创建的PlotLines类,完成的功能如下:
    1、坐标轴的绘制
    2、根据数据点绘制相应的曲线
    3、右上角的图例可以点击,并显示或隐藏对应曲线
     
     

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

    转自:https://www.cnblogs.com/xiaomm/p/6326334.html

  • 相关阅读:
    NABC的特点分析
    梦断代码读后感(三)
    大道至简-“(我) 能不能学会写程序”
    课堂练习-找水王续
    找1
    课堂练习-找水王
    课堂练习-电梯调度
    课堂练习——计算法能够计算出读者购买一批书的最低价格。
    团队项目—二手书店特色
    梦断代码阅读笔记三
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/10929126.html
Copyright © 2020-2023  润新知