在QwtPlot中使用QTime作为X asis

Using QTime as X asis in QwtPlot

本文关键字:作为 asis QTime QwtPlot      更新时间:2023-10-16

我正在尝试使用qwtplot绘制速度-时间图形。

我的X数据是QTime值,Y数据是相应的速度值。我找不到任何关于用QTime绘制绘图的例子。有人能简单地解释一下如何绘制QTime与Y的数据吗?如果可能的话,我也想学习如何缩放QTime轴。

感谢HeyYO,我为未来的读者找到了解决方案。我分享一个非常简单的例子:

#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();
}