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. QGraphicsView + QChart with explicit plotArea: Cannot set background brush

QGraphicsView + QChart with explicit plotArea: Cannot set background brush

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 101 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.
  • U Offline
    U Offline
    utz80
    wrote on 7 Apr 2025, 09:58 last edited by
    #1

    As per title, I am trying to set the background color of a QChart with an explicit plotArea that is displayed in a QGraphicsView.

    #include <QApplication>
    #include <QMainWindow>
    #include <QWidget>
    #include <QDebug>
    #include <QChart>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QLineSeries>
    #include <QRectF>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QMainWindow w = QMainWindow();
    
        auto gv = new QGraphicsView(&w);
        auto gs = new QGraphicsScene(gv);
        gv->setScene(gs);
    
        auto c = new QChart();
        auto s = new QLineSeries();
    
        for (int i = 0; i < 20; ++i) {
          s->append(i, i);
        }
    
        c->addSeries(s);
    
        c->setPlotArea(QRectF(0, 0, 500, 200));
    
        c->setBackgroundBrush(Qt::green);
        c->setBackgroundVisible(true);
    
        qDebug() << "bgbrush: " << c->backgroundBrush();
    
        gs->addItem(c);
    
        w.show();
        w.setCentralWidget(gv);
    
        return a.exec();
    }
    

    the chart's background brush is not set to green. Rather, the output is

    bgbrush:  QBrush(QColor(ARGB 1, 0, 1, 0),SolidPattern)
    

    Not setting a plotArea will color the chart background as expected.

    I know I can set the plotArea background brush instead. That would be sufficient for me, except it does not work in my actual application code either for reasons I haven't figured out yet (it does work in the above example code). So in the meantime, I would like to understand why setting the chart background isn't working.

    Fwiw, this is in Qt 6.8.

    P 1 Reply Last reply 7 Apr 2025, 10:26
    0
    • U utz80
      7 Apr 2025, 09:58

      As per title, I am trying to set the background color of a QChart with an explicit plotArea that is displayed in a QGraphicsView.

      #include <QApplication>
      #include <QMainWindow>
      #include <QWidget>
      #include <QDebug>
      #include <QChart>
      #include <QGraphicsView>
      #include <QGraphicsScene>
      #include <QLineSeries>
      #include <QRectF>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QMainWindow w = QMainWindow();
      
          auto gv = new QGraphicsView(&w);
          auto gs = new QGraphicsScene(gv);
          gv->setScene(gs);
      
          auto c = new QChart();
          auto s = new QLineSeries();
      
          for (int i = 0; i < 20; ++i) {
            s->append(i, i);
          }
      
          c->addSeries(s);
      
          c->setPlotArea(QRectF(0, 0, 500, 200));
      
          c->setBackgroundBrush(Qt::green);
          c->setBackgroundVisible(true);
      
          qDebug() << "bgbrush: " << c->backgroundBrush();
      
          gs->addItem(c);
      
          w.show();
          w.setCentralWidget(gv);
      
          return a.exec();
      }
      

      the chart's background brush is not set to green. Rather, the output is

      bgbrush:  QBrush(QColor(ARGB 1, 0, 1, 0),SolidPattern)
      

      Not setting a plotArea will color the chart background as expected.

      I know I can set the plotArea background brush instead. That would be sufficient for me, except it does not work in my actual application code either for reasons I haven't figured out yet (it does work in the above example code). So in the meantime, I would like to understand why setting the chart background isn't working.

      Fwiw, this is in Qt 6.8.

      P Offline
      P Offline
      Pl45m4
      wrote on 7 Apr 2025, 10:26 last edited by Pl45m4 4 Jul 2025, 10:27
      #2

      @utz80 said in QGraphicsView + QChart with explicit plotArea: Cannot set background brush:

      I would like to understand why setting the chart background isn't working.

      I have not looked at the source code (maybe you want to do this) but my best guess is that if that is intended to work like this, that the background is not taken into account when painting/rendering a plot in a QGraphicsScene... so only foreground data is respected while the background color comes from QGraphicsView's background color at that position.
      AFAIK QChart (when based on Graphics View Framework, not the new QML/Quick Item based QGraphs) has its own background. Could be the case that the internal QChart background and the default white background of your standard QGraphicsView collide.
      But I could be wrong and you should check the source code yourself :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utz80
        wrote on 7 Apr 2025, 12:11 last edited by
        #3

        If I don't set an explicit plotArea, setting the QChart background will work as intended, so I don't think it's colliding with QGraphicsView's background per se. Btw in my application I'm also setting the QGraphicsView background explicitly, but the problem occurs either way.

        1 Reply Last reply
        0

        3/3

        7 Apr 2025, 12:11

        • Login

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