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

  在《QWT在QtCreator中的安裝與使用》一文中,咱們完成了QWT的安裝,這篇文章咱們講講基礎曲線的繪製功能。html

     首先,咱們新建一個Qt應用程序,而後一路默認便可。這時,你會發現總共有:mainwindow.h,mainwindow.cpp,main.cpp,mainwindow.ui四個文件。c++

     而後,選中項目,添加新文件,添加一個c++類,咱們假設命名爲PlotLines,基類選擇QwtPlot,選擇繼承自QWidget。canvas

     接着,在pro文件中添加函數

                                         INCLUDEPATH +=D:\Qt\Qt5.3.0\5.3\msvc2010_opengl\include\QWT
                                         LIBS+= -lqwtd
      注意我這裏是將繪製曲線單獨用一個類PlotLines表示的,而不是向參考實例同樣是直接放在其餘類的內部。因此這裏咱們須要在類的頭文件中添加關鍵性語句:
    #define QWT_DLL
ui

      最後,在主文件main.cpp中添加咱們類的頭文件,並在函數中生成該類的實例並顯示,修改後的main.cpp文件以下所示:this

 

  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. }  
其餘的文件沒有做任何改變,在此就不列出來了。顯示結果以下圖:
一、初始界面以下:

 

二、點擊右上角的圖例後:spa

 

 

本文所建立的PlotLines類,完成的功能以下:
一、座標軸的繪製
二、根據數據點繪製相應的曲線
三、右上角的圖例能夠點擊,並顯示或隱藏對應曲線
 
 

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

轉自:https://www.cnblogs.com/xiaomm/p/6326334.htmlhtm

相關文章
相關標籤/搜索