2015-11-10 17 views
3

我正在嘗試使用qwtplot繪製速度 - 時間圖形。在QwtPlot中使用QTime作爲X asis

我的X數據是QTime值,Y數據是相應的速度值。我無法找到任何使用QTime繪圖的例子。任何人都可以簡單地解釋如何繪製QTime與Y數據?如果可能的話,我也想學習如何縮放QTime軸。

+1

Qt有'QwtDateScaleEngine'類來創建基於時間的比例。查看「stockchart」示例。如果足夠放大,您會發現它也能夠顯示時間值。 – HeyYO

+0

謝謝你的提示,我帶着一個解決方案來使用它。 –

回答

4

對於未來的讀者,我找到了解決方案,感謝HeyyYO。我分享這個非常簡單的例子:

#include "QApplication" 
#include<qwt_plot_layout.h> 
#include<qwt_plot_curve.h> 
#include<qwt_scale_draw.h> 
#include<qwt_scale_widget.h> 
#include<qwt_legend.h> 
class TimeScaleDraw:public QwtScaleDraw 
{ 
public: 
    TimeScaleDraw(const QTime & base) 
     :baseTime(base) 
    { 
    } 
virtual QwtText label(double v)const 
{ 
    QTime upTime = baseTime.addSecs((int)v); 
    return upTime.toString(); 
} 
private: 
    QTime baseTime; 
}; 

int main(int argc,char * argv[]) 
{ 
    QApplication a(argc,argv); 

    QwtPlot * myPlot = new QwtPlot(NULL); 

    myPlot->setAxisScaleDraw(QwtPlot::xBottom,new TimeScaleDraw(QTime::currentTime())); 
    myPlot->setAxisTitle(QwtPlot::xBottom,"Time"); 
    myPlot->setAxisLabelRotation(QwtPlot::xBottom,-50.0); 
    myPlot->setAxisLabelAlignment(QwtPlot::xBottom,Qt::AlignLeft|Qt::AlignBottom); 

    myPlot->setAxisTitle(QwtPlot::yLeft,"Speed"); 
    QwtPlotCurve * cur = new QwtPlotCurve("Speed"); 
    QwtPointSeriesData * data = new QwtPointSeriesData; 
    QVector<QPointF>* samples=new QVector<QPointF>; 
    for (int i=0;i<60;i++) 
    { 
     samples->push_back(QPointF(i,i*i)); 
    } 
    data->setSamples(*samples); 
    cur->setData(data); 
    cur->attach(myPlot); 
    myPlot->show(); 
    return a.exec(); 
}