Understanding QPalette
-
I found this static method in a KDE-related project:
void KColorScheme::adjustBackground(QPalette &palette, BackgroundRole newRole, QPalette::ColorRole color, ColorSet set, KSharedConfigPtr config) { palette.setBrush(QPalette::Active, color, KColorScheme(QPalette::Active, set, config).background(newRole)); palette.setBrush(QPalette::Inactive, color, KColorScheme(QPalette::Inactive, set, config).background(newRole)); palette.setBrush(QPalette::Disabled, color, KColorScheme(QPalette::Disabled, set, config).background(newRole)); }however, I can't really make sense of it! The method is supposed to adjust a background-color of an existing color-scheme. I know the values of
newRole,setandconfigfor my use-case, but what arepaletteandcolorsupposed to be?I assumed, that I would have to pass a
QColorand some scope-information on which color to change, but not a whole color-palette.I tried to get support in KDE-forums first, but didn't have any sucess. Is this something, that's implementation specific to KDE? If so, I will keep researching it in their forums and chat-rooms.
-
Do I understand correctly that you want to modify a specific brush of a given palette by replacing its color with a given QRgb value?
In that case I would not use that KDE method at all and set the color directly to the brush, usingQPalette::setColor().A
QPaletteis a set of brushes, each of which has a unique combination ofQPalette::ColorGroup(i.e. active, inactive, disabled) andQPalette::ColorRole(i.e. base, button, text, window text, etc.). AQBrushdefines a color and the pattern with which is is drawn. That can be a pre-defined pattern or a texture defined by a pixmap.What the KDE method does in essence is: It takes a
colorRolein the parametercolor(which one could be mislead to interpret as a color). It also takes the current KDE color set and the configuration. It then reads the active, inactive and disabled background brushes from the KDE configuration and sets them in the palette to all possible color groups of the color role passed as an argument.If the target is to change only the color of the given palette's brushes, I'd code it directly or write a specific method for this task. Could be something like
void myClass::adjustWhatever(QPalette &palette, QPalette::ColorRole colorRole, const QColor &targetColor) { palette.setColor(QPalette::Active, colorRole, targetColor); palette.setColor(QPalette::Inactive, colorRole, targetColor); palette.setColor(QPalette::Disabled, colorRole, targetColor); }As for the RGB value, it comes in handy that
QColorhas both aQRgband an RGBA constructor. So you can call the method likemyClass.adjustWhatever(myPalette, myColorRole, QColor(r, g, b, a)); // passing rgb and a as int valuesor
myClass.adjustWhatever(myPalette, myColorRole, myQRgb); // passing a QRgb directly -
Different widgets use different roles for their backgrounds. For example a text input might use
QPalette::Baseand a dialog might useQPalette::Window. Thecolorparameter controls which role you want to modify. the default isQPalette::Base. This KDE method is a bit weird, since nothing stops you from passing a text or any other role here, but I guess it's just relying on the user to play nice.As for palette parameter that's the palette you're modifying. You can't really get a reference to a palette used by given widget directly, only a copy, so a common pattern for changing palette is get->modify->set, e.g. to change the Base color for entire application would look something like this:
QPalette p = qApp->palette(); KColorScheme::adjustBackground(p, ... qApp->setPalette(p); -
Different widgets use different roles for their backgrounds. For example a text input might use
QPalette::Baseand a dialog might useQPalette::Window. Thecolorparameter controls which role you want to modify. the default isQPalette::Base. This KDE method is a bit weird, since nothing stops you from passing a text or any other role here, but I guess it's just relying on the user to play nice.As for palette parameter that's the palette you're modifying. You can't really get a reference to a palette used by given widget directly, only a copy, so a common pattern for changing palette is get->modify->set, e.g. to change the Base color for entire application would look something like this:
QPalette p = qApp->palette(); KColorScheme::adjustBackground(p, ... qApp->setPalette(p);@Chris-Kawa thanks for the answer! I understood that the QPallet has to exist before calling this function. No idea where this might come from though. I’ll keep searching!
As for the second parameter „color“: How would I pass a QRgb-value to the shown function? This is what I would ultimately like to do.
-
Do I understand correctly that you want to modify a specific brush of a given palette by replacing its color with a given QRgb value?
In that case I would not use that KDE method at all and set the color directly to the brush, usingQPalette::setColor().A
QPaletteis a set of brushes, each of which has a unique combination ofQPalette::ColorGroup(i.e. active, inactive, disabled) andQPalette::ColorRole(i.e. base, button, text, window text, etc.). AQBrushdefines a color and the pattern with which is is drawn. That can be a pre-defined pattern or a texture defined by a pixmap.What the KDE method does in essence is: It takes a
colorRolein the parametercolor(which one could be mislead to interpret as a color). It also takes the current KDE color set and the configuration. It then reads the active, inactive and disabled background brushes from the KDE configuration and sets them in the palette to all possible color groups of the color role passed as an argument.If the target is to change only the color of the given palette's brushes, I'd code it directly or write a specific method for this task. Could be something like
void myClass::adjustWhatever(QPalette &palette, QPalette::ColorRole colorRole, const QColor &targetColor) { palette.setColor(QPalette::Active, colorRole, targetColor); palette.setColor(QPalette::Inactive, colorRole, targetColor); palette.setColor(QPalette::Disabled, colorRole, targetColor); }As for the RGB value, it comes in handy that
QColorhas both aQRgband an RGBA constructor. So you can call the method likemyClass.adjustWhatever(myPalette, myColorRole, QColor(r, g, b, a)); // passing rgb and a as int valuesor
myClass.adjustWhatever(myPalette, myColorRole, myQRgb); // passing a QRgb directly -
@Chris-Kawa thanks for the answer! I understood that the QPallet has to exist before calling this function. No idea where this might come from though. I’ll keep searching!
As for the second parameter „color“: How would I pass a QRgb-value to the shown function? This is what I would ultimately like to do.
@tim-hilt said:
No idea where this might come from though. I’ll keep searching!
There's an application wide palette that you get like I showed in the example and you can locally override palette for each widget, calling
QWidget::palette().How would I pass a QRgb-value to the shown function?
That's not what this function is for. It creates a
KColorSchemefrom thesetandconfigand gets a color from that. If you want to set a particular color do what Axel said, set it directly e.g.p.setColor(QPalette::Base, QColor(r,g,b)); -
@Axel-Spoerl @Chris-Kawa thank you to both of you for the answers. I now understand, that the method doesn't even do what I thought it would do!
I still don't know how I could get the correct
QPalette, but now I have a much better understanding of what these functions actually do and how that differs from what I need.Thanks again to both of you.
-
@tim-hilt Can you describe verbally which palette should be changed? The application palette? A specific widget’s palette? Something else? We’ll find it out!