QGraphicsView + QChart with explicit plotArea: Cannot set background brush
-
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.
-
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.
@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 fromQGraphicsView
's background color at that position.
AFAIKQChart
(when based on Graphics View Framework, not the new QML/Quick Item basedQGraphs
) has its own background. Could be the case that the internalQChart
background and the default white background of your standardQGraphicsView
collide.
But I could be wrong and you should check the source code yourself :) -
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.