我正在尝试使用qwtplot绘制速度 – 时间图形.
我的X数据是QTime值,Y数据是相应的速度值.我找不到任何关于使用QTime绘制图的示例.任何人都可以简单地解释如何绘制QTime与Y数据?如果可能的话,我还想学习如何缩放QTime轴.
解决方法:
对于未来的读者,我已经找到了一个解决方案,感谢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();
}