16,203
社区成员
发帖
与我相关
我的任务
分享
#ifndef QWT_H
#define QWT_H
#include <QtGui/QWidget>
#include <ActiveQt/QAxBindable>
#include <QDateTime>
#include <QVector>
#include <QtGui/QMainWindow>
#include "ui_qwt.h"
class QwtPlot;
class QwtPlotCurve;
class qwt : public QMainWindow
{
Q_OBJECT
public:
qwt(QWidget *parent = 0, Qt::WFlags flags = 0);
~qwt();
QwtPlot* m_plot;
QwtPlotCurve* m_curve;
QDateTime m_baseTime;
QVector<QPointF>m_vp;
int m_timeAreaWidth;
void timerEvent(QTimerEvent *e);
double m_timeData[3600];
public slots:
int test(int a);
private:
Ui::qwtClass ui;
};
#endif // QWT_H
#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();
}