How to get widget background QColor

Nils Werner picture Nils Werner · Mar 20, 2013 · Viewed 14.4k times · Source

I am trying to find out the background color of a QWidget or QGLWidget so that I can use it with qglClearColor() to make the OpenGL part appear natively within the widget (without for example a black background).

I figured I could fetch backgroundRole() of my widget but I am having problems converting it to a QColor. There is QPalette::color(QColorRole) but it isn't static and I have no idea how I would have to create an instance of QPalette to do the transformation.

Answer

Sir Digby Chicken Caesar picture Sir Digby Chicken Caesar · Mar 20, 2013

Use QWidget::palette().color(QWidget::backgroundRole())to receive the color of the background color role for that widget. Obviously, this should also work with any class that inherits QWidget.