1

In my QGraphicsScene, I would like to set the background brush to the default widget background - but I can't get it.

Kinda like, for my QGraphicsView,

setBackgroundRole(QPalette::Window);

or

setBackgroundBrush(palette().background().color());

(but setting this I see nothing happening)... I also see nothing happening if I set the view color to a bright red).

So I thought I must set color directly on the QGraphicsScene.

For the QGraphicsScene I am trying all sort of combinations like

setBackgroundBrush(QPalette::color(QPalette::Background));

Nothing will even build, seems I require an object (? a widget ?) - but my scene may not have a widget parent... and all I want is a default palette, I thought there would be a generic way to get that color without having a widget ?

On the scene, this will work...

setBackgroundBrush(Qt::red);

No clue why the view won't show color (even if I set on the view, red brush and on the scene transparent).

Thalia
  • 13,637
  • 22
  • 96
  • 190

2 Answers2

3

You may retrieve the QApplication's current style using the static method style(). From there, you may access the QStyle's standard palette using standardPalette(). Use QPalette's brush method to get a brush for a given ColorRole. Putting it all together you get...

QApplication::style()->standardPalette().brush(QPalette::Background)

This may not be the color you are expecting. Check out the documentation on http://doc.qt.io/qt-4.8/qpalette.html, and try different ColorRole values until you find what you're looking for.

walkingTarget
  • 408
  • 5
  • 17
2

Create a temporary widget instance just to access its palette and get the background color:

QColor bgColor = QWidget().palette().background().color();

But I think you should set the background color in the QGraphicsView widget. You can do that by changing its stylesheet. Something like:

QColor bg = ui->graphicsView->palette().background().color();
ui->graphicsView->setStyleSheet(QString("background-color:") + bg.name(QColor::HexArgb));

Setting a transparent background also works.

HeyYO
  • 1,989
  • 17
  • 24
  • I have tried to set the background color before, in the view, but I see nothing happening... – Thalia Nov 12 '15 at 20:44
  • What do you see exactly? Does it stay the default color - white? – HeyYO Nov 12 '15 at 20:55
  • yes it stays white no matter what i change it to - I updated the question to explain this odd behavior. Even if I set the scene as transparent. The scene background brush works - but having to create a useless object, and include qwidget for it, to acquire a color, seems a lot of overhead – Thalia Nov 12 '15 at 20:59
  • I just got an idea to try `setBackgroundBrush(views().at(0)->palette().background().color());` but the scene is created before being added to the view... – Thalia Nov 12 '15 at 21:02
  • Ok I cheated - i made the caller set the scene brush. It makes no sense that the system colors are not available... but... oh well. – Thalia Nov 12 '15 at 21:07