學習這個東西方向對了,就對了一半
今天我開始學習基於qt庫的一個開源的繪製2維的統計圖的庫--qwt。
我們畫東西首先要有一個容器,不然都是徒勞,今天我們就介紹這個類--QwtPlot
它繼承自QFrame和QwtPlotDict,QFrame提供一個QWidget的架構,QwtPlotDict為QwtPlot管理在其中的plot items,就是繪製的項。在QwtPlot上我們可以繪製無限多個的plot items,這些plot items可以是曲線,標記,格子以及繼承自QwtPlotItem的子類。一個QwtPlot可以有四個軸,每個plot item串連到x和y軸上。在軸上的比例變換可以使用QwtScaleDiv,對於plot items比例可以使用QwtScaleEngine來計算,在每個軸上,QwtScaleEngine可以被單獨設定。
在QwtPlot中有兩個枚舉類型。
Axis,軸,5個值,一個QwtPlot除了x和y,還有top和bottom軸,第五個是axisCnt,軸數,枚舉從0開始,第五個為4,說明一共四個軸。另一個是LegendPosition,圖例的位置。
它有五個值,分別指定插入一個圖例仔什麼位置,四個都是和x和y軸的位置有關,最後一個是特殊的,它允許不在這個Plot中,就是外部的。
這是今天寫的一個小例子
/**************************************************author:周翔*e-mail:604487178@qq.com*blog:http://blog.csdn.net/zhx6044***************************************************/#ifndef PLOT_H#define PLOT_H#include <qwt_plot.h>#include <qwt_plot_curve.h>#include <qwt_plot_picker.h>#include <qwt_plot_panner.h>#include <qwt_plot_magnifier.h>#include <qwt_legend.h>#include <qwt_plot_grid.h>#include <qwt_picker_machine.h>class Plot : public QwtPlot{ Q_OBJECTpublic: explicit Plot(QWidget *parent = 0); ~Plot();signals: public slots:private: QwtPlotCurve *sinCurve; QwtPlotCurve *cosCurve; QwtPlotPicker *picker; void initCanvas(); void initAxes(); void initCurves(); };#endif // PLOT_H/**************************************************author:周翔*e-mail:604487178@qq.com*blog:http://blog.csdn.net/zhx6044***************************************************/#include "plot.h"double _sin2(double x){ return ::sin(2 * x);}/** * @brief The FunctionData class 將一個函數封裝一下,就像一個函數對象一樣,但是又不一樣,沒有基於運行符()重載 * 而是基於多態,我們只需要繼承它,實現相關的虛函數即可,它內部有自己的調用規則 */class FunctionData: public QwtSyntheticPointData{public: FunctionData(double(*y)(double)): QwtSyntheticPointData(100), d_y(y) { } virtual double y(double x) const { return d_y(x); }private: double(*d_y)(double);};Plot::Plot(QWidget *parent) : QwtPlot(parent){ initCanvas();}Plot::~Plot(){}void Plot::initCanvas(){ //右鍵拖拽 (new QwtPlotPanner(this->canvas()))->setMouseButton(Qt::RightButton); //y軸在放大的時候,座標不變化 (new QwtPlotMagnifier(this->canvas()))->setAxisEnabled(QwtPlot::yLeft,false); //一個選取器,十字線,以xBottom和yLeft座標 picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn, this->canvas()); picker->setStateMachine(new QwtPickerDragPointMachine());//拖拽點起作用 picker->setRubberBandPen(QPen(QColor(Qt::white))); picker->setTrackerPen(QColor(Qt::yellow)); setAutoFillBackground(true); this->canvas()->setPalette(QPalette (QColor(Qt::darkCyan))); setTitle("sin(x) and cos(x)"); //這個會根據畫板中的圖在RightLegend顯示一個圖例 insertLegend(new QwtLegend(),QwtPlot::RightLegend); QwtPlotGrid *grid = new QwtPlotGrid;//網格 grid->enableXMin(true); grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));//大格子 grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));//大格子裡的小格子 grid->attach(this); initAxes(); initCurves();}void Plot::initAxes(){ setAxisTitle(QwtPlot::yLeft, QObject::trUtf8("y 軸")); setAxisScale(QwtPlot::yLeft, -1.0, 1.0); setAxisTitle(QwtPlot::xBottom,QObject::trUtf8("x 軸")); setAxisScale(QwtPlot::xBottom,-5.0,5.0);}void Plot::initCurves(){ sinCurve = new QwtPlotCurve("y = sin(2x)"); //切換渲染提示 啟用消除鋸齒 sinCurve->setRenderHint(QwtPlotItem::RenderAntialiased); sinCurve->setLegendAttribute(QwtPlotCurve::LegendShowLine); sinCurve->setPen(QPen(Qt::yellow)); sinCurve->attach(this); cosCurve = new QwtPlotCurve("y = cos(x)"); cosCurve->setRenderHint(QwtPlotItem::RenderAntialiased); //這個會改變曲線在QwtLegend顯示的樣式,可以是線,矩形,符號, cosCurve->setLegendAttribute(QwtPlotCurve::LegendNoAttribute); cosCurve->setPen(QPen(Qt::red)); cosCurve->attach(this); sinCurve->setData(new FunctionData(_sin2)); cosCurve->setData(new FunctionData(::cos));}