在《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类,完成的功能以下:
一、坐标轴的绘制
二、根据数据点绘制相应的曲线
三、右上角的图例能够点击,并显示或隐藏对应曲线