splinewidget.cpp
108 lines
| 2.8 KiB
| text/x-c
|
CppLexer
|
r401 | #include "splinewidget.h" | ||
#include "qchartview.h" | ||||
|
r419 | #include "qlineseries.h" | ||
|
r401 | #include <QGridLayout> | ||
|
r431 | #include <QPushButton> | ||
|
r434 | #include "qchartaxis.h" | ||
#include <qmath.h> | ||||
|
r401 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
SplineWidget::SplineWidget(QWidget *parent) | ||||
: QWidget(parent) | ||||
{ | ||||
|
r457 | // qsrand(time(NULL)); | ||
|
r434 | //! [1] | ||
|
r401 | //create QSplineSeries | ||
|
r450 | series = new QSplineSeries; | ||
|
r434 | //! [1] | ||
//! [2] | ||||
// customize the series presentation settings | ||||
QPen seriesPen(Qt::blue); | ||||
seriesPen.setWidth(3); | ||||
series->setPen(seriesPen); | ||||
//! [2] | ||||
//! [add points to series] | ||||
//add data points to the series | ||||
|
r431 | series->add(QPointF(150, 100)); | ||
series->add(QPointF(200, 130)); | ||||
series->add(QPointF(250, 120)); | ||||
series->add(QPointF(300, 140)); | ||||
|
r434 | series->add(QPointF(350, 160)); | ||
//! [add points to series] | ||||
// series->add(QPointF(400, 120)); | ||||
// series->add(QPointF(450, 150)); | ||||
// series->add(QPointF(500, 145)); | ||||
// series->add(QPointF(550, 170)); | ||||
// series->add(QPointF(600, 190)); | ||||
// series->add(QPointF(650, 210)); | ||||
// series->add(QPointF(700, 190)); | ||||
// series->add(QPointF(750, 180)); | ||||
// series->add(QPointF(800, 170)); | ||||
|
r401 | |||
|
r434 | //! [3] | ||
// create chart view | ||||
QChartView* chart = new QChartView; | ||||
chart->addSeries(series); | ||||
|
r401 | |||
|
r450 | chart->setChartTitle("Spline chart example"); | ||
// chart->setMinimumSize(800,600); | ||||
// chart->axisX()->setRange(0, 1500); | ||||
// chart->axisX()->setMax(1500); | ||||
// chart->axisY()->setRange(0, 400); | ||||
chart->axisX()->setMax(1500); | ||||
// chart-> | ||||
|
r401 | |||
chart->setMinimumSize(800,600); | ||||
|
r434 | //! [3] | ||
|
r431 | |||
|
r434 | //! [4] | ||
//add new data point button | ||||
|
r431 | QPushButton* addButton = new QPushButton("Add new point"); | ||
connect(addButton, SIGNAL(clicked()), this, SLOT(addNewPoint())); | ||||
|
r434 | // remove the last data point in the series | ||
QPushButton* removeButton = new QPushButton("Remove point"); | ||||
|
r431 | connect(removeButton, SIGNAL(clicked()), this, SLOT(removePoint())); | ||
|
r434 | //! [4] | ||
|
r431 | |||
|
r434 | //! [5] | ||
|
r431 | //butttons layout | ||
QVBoxLayout* buttonsLayout = new QVBoxLayout; | ||||
buttonsLayout->addWidget(addButton); | ||||
buttonsLayout->addWidget(removeButton); | ||||
buttonsLayout->addStretch(); | ||||
|
r401 | |||
QGridLayout* mainLayout = new QGridLayout; | ||||
|
r431 | mainLayout->addWidget(chart, 1, 0); | ||
mainLayout->addLayout(buttonsLayout, 1, 1); | ||||
|
r401 | setLayout(mainLayout); | ||
|
r434 | //! [5] | ||
|
r401 | } | ||
|
r434 | //! [add point] | ||
|
r431 | void SplineWidget::addNewPoint() | ||
{ | ||||
if (series->count() > 0) | ||||
|
r434 | series->add(QPointF(series->x(series->count() - 1) + 20 + qrand()%40, qAbs(series->y(series->count() - 1) - 50 + qrand()%100))); | ||
|
r431 | else | ||
series->add(QPointF(50, 50 + qrand()%50)); | ||||
} | ||||
|
r434 | //! [add point] | ||
|
r431 | |||
|
r434 | //! [remove point] | ||
|
r431 | void SplineWidget::removePoint() | ||
{ | ||||
if (series->count() > 0) | ||||
series->remove(QPointF(series->x(series->count() - 1), series->y(series->count() - 1))); | ||||
} | ||||
|
r434 | //! [remove point] | ||
|
r431 | |||
|
r401 | SplineWidget::~SplineWidget() | ||
{ | ||||
} | ||||