PDA

View Full Version : How to change only the background color of widget to white?



Leutzig
28th November 2015, 02:08
Hi,

I'm making a color picker program for an RBG-LED and I found a color wheel online that I've been using. I decided later on to use a Tab Widget as my main widget, but I'm having some trouble with the background color of the color wheel. Since the tab widget's default background color is white and a "normal" widget's (which I've used as parent for the color wheel) default background color is grey, the color wheel ends up with a visible grey square around it (see attached picture).

I tried writing this in my main, but it makes the whole widget white including the color wheel.
colorWheelWidget->setStyleSheet("background-color:white;");

The color wheel is written all in code, which I'm not very familiar with, so I wonder if there is a way to change the color wheel's default color to white inside the colorwheel cpp-file?

I found the color wheel here:
https://forum.qt.io/topic/19261/colorwheel

//Leutzig

d_stranz
28th November 2015, 19:14
In looking at the code, it appears that the outer square is filled with a brush created from the style's "Window" QPalette::ColorRole. So when you create your instance of the ColorWheel, retrieve the palette, change the brush for the Normal ColorGroup and Window ColorRole to white, then set the palette back on the widget:



ColorWheel * myWheel = new ColorWheel( this );
QPalette wheelPalette = myWheel->palette();
wheelPalette.setBrush( QPalette::Normal, QPalette::Window, QBrush( Qt::white ) );
myWheel->setPalette( wheelPalette );


This code sets the background color only for the active (normal) mode. The color for the inactive and disabled modes remain whatever they are. If you want the background color as white regardless of mode, then use the setBrush() method that takes only the ColorRole and QBrush as arguments.

Leutzig
28th November 2015, 20:28
Thank you so much d_stranz for this and the explanation, this did the trick!