Simple Qt question(s) (QObject parenting model, QLinearGradient, plotArea( ) )
-
Hello, I'm beginner at Qt and currently learning from Mastering Qt5 book and I got stuck. There is a piece of code and I don't understand some things.
#include "MemoryWidget.h" #include "SysInfo.h" // Getting system informations #include <QtCharts/QAreaSeries> #include <QPen> #include <QLinearGradient> using namespace QtCharts; const int CHART_X_RANGE_COUNT = 50; const int CHART_X_RANGE_MAX = CHART_X_RANGE_COUNT - 1; const int COLOR_DARK_BLUE = 0x209fdf; const int COLOR_LIGHT_BLUE = 0xbfdfef; const int PEN_WIDTH = 3; MemoryWidget::MemoryWidget(QWidget *parent) : SysInfoWidget(parent), mSeries(new QLineSeries(this)), mPointPositionX(0) { QPen pen(COLOR_DARK_BLUE); pen.setWidth(PEN_WIDTH); QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1)); gradient.setColorAt(1.0, COLOR_DARK_BLUE); gradient.setColorAt(0.0, COLOR_LIGHT_BLUE); gradient.setCoordinateMode(QGradient::ObjectBoundingMode); QAreaSeries *areaSeries = new QAreaSeries(mSeries); areaSeries->setPen(pen); areaSeries->setBrush(gradient); QChart *chart = chartView().chart(); chart->addSeries(areaSeries); chart->setTitle("Memory used"); chart->createDefaultAxes(); chart->axisX()->setRange(0, CHART_X_RANGE_MAX); chart->axisX()->setVisible(false); chart->axisY()->setRange(0, 100); } void MemoryWidget::updateSeries() { double memoryUsed = SysInfo::instance().memoryUsed(); mSeries->append(mPointPositionX++, memoryUsed); if (mSeries->count() > CHART_X_RANGE_COUNT) { QChart *chart = chartView().chart(); chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0); mSeries->remove(0); } }
I don't understand the whole QLinearGradient piece of code.
What are the QPointF values inQLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
and in the following code, who will be the parent:
QAreaSeries *areaSeries = new QAreaSeries(mSeries);
And finally, in MemoryWidget::updateSeries() method, why is
chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);
the solution if i want scroll the x axis by one point.
Anyway, the book says:
"The syntax chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0) will then scroll to the latest point on the X axis and nothing on Y. The chart->scroll(dx, dy) expects coordinates expressed in our series coordinates. That is the reason why we have to retrieve the char->plotArea() divided by CHART_X_RANGE_MAX , the X axis unit."Thank you, and sorry for my bad english
-
@Dean95 said in Simple Qt question(s) (QObject parenting model, QLinearGradient, plotArea( ) ):
QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
Take a look at documentation: http://doc.qt.io/qt-5/qlineargradient.html#QLinearGradient-1 and http://doc.qt.io/qt-5/qlineargradient.html
QAreaSeries *areaSeries = new QAreaSeries(mSeries);
mSeries will be the parent as it is passed as parent.
-
- chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);
Which part of it
It calls
void QChart::scroll(qreal dx, qreal dy)
that makes it scrollso it calculates the scroll amount
say plotArea().width() is 1000
it will scroll 1000/49
( from
const int CHART_X_RANGE_COUNT = 50;
const int CHART_X_RANGE_MAX = CHART_X_RANGE_COUNT - 1; )