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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
全部代码
#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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116