在《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_DLLui
最後,在主文件main.cpp中添加咱們類的頭文件,並在函數中生成該類的實例並顯示,修改後的main.cpp文件以下所示:this
- #include "mainwindow.h"
- #include <QApplication>
- #include"plotlines.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
-
- 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 );
-
-
- 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->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;
- 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);
-
-
- 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 );
-
- 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 );
- }
其餘的文件沒有做任何改變,在此就不列出來了。顯示結果以下圖:
一、初始界面以下:

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

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