ui界面
核心代码
void Dialog::slot_timer()
{
qreal xOffset=0.f;
qreal dIncrease=10;
for(int i=0;i<10;i++)
{
m_x+=dIncrease;
xOffset+=dIncrease;
m_splineSerise->append(m_x,qrand()%10);
}
if(m_splineSerise->count()>c_MaxSize)
{
m_splineSerise->removePoints(0,m_splineSerise->count()*0.5-c_MaxSize);
}
qreal xUnit =m_chart->plotArea().width()/(m_axisX->max()-m_axisX->min());
qreal xScroll =xOffset*xUnit;
m_chart->scroll(xScroll,0);
}
全部代码
#include "dialog.h"
#include "ui_dialog.h"
const quint32 c_MaxSize=1000;
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
,ui(new Ui::Dialog)
,m_x(0.f)
,m_splineSerise(NULL)
{
ui->setupUi(this);
m_splineSerise=new QSplineSeries(this);
m_x=1000;
m_splineSerise->append(m_x,1);
m_chart=new QChart();
m_chart->addSeries(m_splineSerise);
m_axisX = new QValueAxis();
m_axisX->setRange(0,c_MaxSize);
m_axisX->setTitleText(QStringLiteral("X"));
m_axisX->setLabelFormat("%g");
m_axisX->setTickCount(5);
m_axisY= new QValueAxis();
m_axisY->setRange(-10,10);
m_axisY->setTitleText(QStringLiteral("Y"));
m_chart->setAxisX(m_axisX,m_splineSerise);
m_chart->setAxisY(m_axisY,m_splineSerise);
m_chart->legend()->hide();
m_chart->setTheme(QtCharts::QChart::ChartThemeBlueCerulean);
m_chart->setTitle(QString("图表1"));
m_chart->setGeometry(0,0,500,300);
m_pScene =new QGraphicsScene(this);
ui->graphicsView->setScene(m_pScene);
m_pScene->addItem(m_chart);
ui->graphicsView->setRenderHint(QPainter::Antialiasing,true);
m_timer=new QTimer(this);
m_timer->setInterval(100);
connect(m_timer,&QTimer::timeout,this,&Dialog::slot_timer);
m_timer->start();
}
Dialog::~Dialog()
{
m_chart->removeAllSeries();
delete ui;
}
qreal Dialog::getData(qreal x)
{
return qSin(x*2*M_PI)*7;
}
void Dialog::slot_timer()
{
qreal xOffset=0.f;
qreal dIncrease=10;
for(int i=0;i<10;i++)
{
m_x+=dIncrease;
xOffset+=dIncrease;
m_splineSerise->append(m_x,qrand()%10);
}
if(m_splineSerise->count()>c_MaxSize)
{
m_splineSerise->removePoints(0,m_splineSerise->count()*0.5-c_MaxSize);
}
qreal xUnit =m_chart->plotArea().width()/(m_axisX->max()-m_axisX->min());
qreal xScroll =xOffset*xUnit;
m_chart->scroll(xScroll,0);
}