Setting a plain background color for a QGraphicsView widget

CakeMaster picture CakeMaster · Aug 24, 2010 · Viewed 18.2k times · Source

I'm trying to display a little square of solid color.

I've created a QGraphicsView widget myGraphicsView and call:

ui->myGraphicsView->setBackgroundBrush(QBrush(Qt::red, Qt::SolidPattern));

But it stays white. What am I doing wrong?

Answer

user362638 picture user362638 · Aug 24, 2010

You need to set the QGraphicsScene to the QGraphicsView before anything is drawn:

ui->myGraphicsView->setScene(myScene);