在《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文件如下所示:
- #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_OBJECT
- public:
- 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 );
- }
2、點擊右上角的圖例后:
原文:http://blog.csdn.net/tengweitw/article/details/41911035
轉自:https://www.cnblogs.com/xiaomm/p/6326334.html