Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Customize qtChart

Customize qtChart

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 250 Views
  • 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.
  • S Offline
    S Offline
    steph27
    wrote on 19 Nov 2021, 15:56 last edited by
    #1

    Hi,

    I would like to customize my QtChart to have the 3 colors (green, yellow, red) on the range slider at the background of the chart (instead of the shades) :

    Capture.JPG

    How can I do that in QML ?

    Regards
    Stéphane

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steph27
      wrote on 23 Nov 2021, 12:32 last edited by
      #2

      Ok I found it :
      Capture.JPG

      I call this function each time the QSliderRange change :

      void Controller::updateBackground(QQuickItem* item){
          if(item){
              if(QGraphicsScene *scene = item->findChild<QGraphicsScene *>()){
                  for(QGraphicsItem *it : scene->items()){
                      if(QtCharts::QChart *chart = dynamic_cast<QtCharts::QChart *>(it)){
                          // Customize plot area background
                          QImage background(item->width(), item->height(), QImage::Format_ARGB32);
                          if (background.isNull())
                              continue;
                          background.fill(QColor(Qt::white));
                          QPainter painter(&background);
      
                          int margin = (item->height()-chart->plotArea().height()) /2;
                          int hRed = (10- _pData->threshHigh)/10 * chart->plotArea().height();
                          int hGreen = _pData->threshLow/10 * chart->plotArea().height();
                          int hYellow = chart->plotArea().height() - hRed - hGreen;
      
                          // Red zone
                          painter.fillRect(0, 0, item->width(), margin + hRed, QBrush(QColor(255,50,50,128)));
                          // Yellow zone
                          painter.fillRect(0,margin+hRed, item->width(), hYellow, QBrush(QColor(235,220,50,128)));
                          // Green zone
                          painter.fillRect(0,margin+hRed+hYellow, item->width(), margin+hGreen, QBrush(QColor(100,150,100,128)));
      
                          chart->setPlotAreaBackgroundBrush(background);
                          chart->setPlotAreaBackgroundVisible(true);
                      }
                  }
              }
          }
      }
      
      
      

      By passing the ChartView as parameter :

      sThresh.first.onValueChanged:     controller.updateBackground(chart)
      sThresh.second.onValueChanged:    controller.updateBackground(chart)
      
      1 Reply Last reply
      1

      1/2

      19 Nov 2021, 15:56

      • Login

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