Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple Qt question(s) (QObject parenting model, QLinearGradient, plotArea( ) )
Forum Updated to NodeBB v4.3 + New Features

Simple Qt question(s) (QObject parenting model, QLinearGradient, plotArea( ) )

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dean95
    wrote on last edited by Dean95
    #1

    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 in

    QLinearGradient 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

    jsulmJ 1 Reply Last reply
    0
    • D Dean95

      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 in

      QLinearGradient 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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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.

        D Offline
        D Offline
        Dean95
        wrote on last edited by Dean95
        #3

        @jsulm Thanks, but i still do not understand the following piece of code

        chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);
        
        mrjjM 1 Reply Last reply
        0
        • D Dean95

          @jsulm Thanks, but i still do not understand the following piece of code

          chart->scroll(chart->plotArea().width() / CHART_X_RANGE_MAX, 0);
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Dean95

          • 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 scroll

          so 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; )

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved