【Qt編程】基於QWT的曲線繪制及圖例顯示操作——有樣點的實現功能


  在《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文件如下所示:

 

  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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM