#include "qwt.h"
#include "qwt_plot.h"
#include "qwt_scale_draw.h"
#include "qwt_plot_layout.h"
#include "qwt_plot_canvas.h"
#include "qwt_plot_curve.h"
#include "QTextCodec"
#include "qwt_plot_grid.h"
#include "qwt_symbol.h"
#include <QVBoxLayout>
#define TIMER_INTERVAL 2
class TimeScaleDrawR: public QwtScaleDraw
{
public:
TimeScaleDrawR(const QDateTime &base):
baseTime(base)
{
}
virtual QwtText label(double v) const
{
QDateTime upTime = baseTime.addSecs((int)v);
return upTime.toString("hh:mm:ss");
}
private:
QDateTime baseTime;
};
qwt::qwt(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
// QVBoxLayout* vLayout = new QVBoxLayout(this);
//
m_plot = new QwtPlot(QwtText("实时曲线图"),this);
m_plot->setCanvasBackground(QBrush(Qt::white));
//vLayout->addWidget(m_plot);
m_plot->setGeometry(0,0,600,400);
test(1);
}
qwt::~qwt()
{
}
int qwt::test( int a )
{
m_timeAreaWidth = 60;
m_plot->setAxisScale(QwtPlot::yLeft, 0, 100);
m_plot->setAxisTitle(QwtPlot::yLeft, "左边");
m_baseTime = QDateTime::currentDateTime();
m_plot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDrawR(m_baseTime));
m_plot->setAxisScale(QwtPlot::xBottom, 0, m_timeAreaWidth);
m_plot->setAxisTitle(QwtPlot::xBottom,"底下");
m_plot->setAxisLabelRotation(QwtPlot::xBottom, -50.0);
m_plot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft|Qt::AlignBottom);
m_plot->plotLayout()->setAlignCanvasToScales(true);
m_plot->canvas()->setBorderRadius(10);
//m_plot->setGeometry(QRect(20, 70, 1050, 570));
//m_plot->setCanvasBackground(QBrush(Qt:lightGray));
QwtPlotGrid* grid = new QwtPlotGrid();
grid->enableXMin(true);
grid->enableYMin(true);
grid->setMajPen(QPen(Qt::darkGray,0,Qt::DotLine));
grid->setMinPen(QPen(Qt::lightGray,0,Qt::DotLine));
grid->attach(m_plot);
//resize(2110, 1660);
for ( int i = 0; i < m_timeAreaWidth; i++ )
m_timeData[i] =-i;
/*m_timerId = */startTimer(TIMER_INTERVAL*1000); // 1 second
m_curve = new QwtPlotCurve("测试");
m_curve->setPen(QColor(Qt::blue));
m_curve->setRenderHint(QwtPlotItem::RenderAntialiased);
m_curve->attach(m_plot);
///////////////////////
m_curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
QPen(Qt::blue), QSize(5, 5) ) );
m_plot->replot();
return 0;
}
void qwt::timerEvent( QTimerEvent *e )
{
qint64 timeVal = (QDateTime::currentDateTime().toMSecsSinceEpoch()-m_baseTime.toMSecsSinceEpoch())/1000;
QPointF pt;
if(m_vp.size() >= m_timeAreaWidth)
{
m_vp.pop_front();
}
pt.setX(timeVal);
pt.setY(rand()%100);
m_vp.append(pt);
qint64 k = timeVal;
for ( int j = 0; j < m_timeAreaWidth; j++ )
{
m_timeData[j] = k--;
}
m_plot->setAxisScale(QwtPlot::xBottom, m_timeData[m_timeAreaWidth - 1 ], m_timeData[0]);
m_curve->setSamples(m_vp);
m_plot->replot();
}
qwt画实时曲线的小demo
5星 · 超过95%的资源 需积分: 34 53 浏览量
2015-04-20
09:58:02
上传
评论 8
收藏 334KB RAR 举报
翅膀又硬了
- 粉丝: 108
- 资源: 11
- 1
- 2
- 3
- 4
前往页