Question about widget's backgroundRole / foregroundRole
-
Hi,
I try to set a background color to some of my widgets (like QLineEdits) and I don't understand the sense of QPalette::ColorRole.
I know each QWidget has a backgroundRole (for designing its background with QColor or QBrush) and also a foregroundRole (e.g. to set a text color).
I also know (please correct me if I'm wrong) that these Roles can be set for three widget states (active, inactive, disabled), so I can give a background/text color like blue to a widget in disabled state and red to the same widget in active state.
All these settings are stored in the widgets palette (QPallete::ColorGroup/QPallete::ColorRole).Well, sounds all logically. What I don't understand now is, why can QWidgets like e.g. QLineEdit have a function setBackgroundRole(QPalette::ColorRole)??
The only colorRoles for a QWidget's background that would make sense in my eyes are: QPalette::Window (means that this widget has the same background color as its window, right??) or QPalette::Base (background color for edits like QLineEdit, right??) or QPalette::Button (background color for buttons).What would happen now if I do something like:
@QLineEdit::setBackgroundRole(QPalette::WindowText);@ ?? This doesn't make any sense in my eyes, because it's a color role for QWidget's foreground.I think I don't understand this stuff. When I do something like that:
@QLineEdit::setBackgroundRole(QPalette::Window);@ Then the QLineEdit should have the same background color as the window (gray), but it is still white...Am I missing something?
Thank you in anticipation!
-
Hi,
You could think of it as a way to synchronize the background color to another element of you UI.
Anyway, the current style is free to choose what it want to use. You can either modify the palette or use a style sheet if you don't get what you want.
Hope it helps
-
Hi and thank you for your reply.
Could you explain what you mean with "synchronize"?
What is the sense of different background roles?
Documentation says, QPalette::Window is used for background settings (background color) and QPalette::WindowText for foreground settings (text color).
So why are there any other roles like QPalette::Base or QPalette::Button and so on?My guess was, that every QWidget is ranged in one of those background roles, like every widget that can hold editable text (Edits, Lists/Views) uses the background role QPalette::Base and QPalette::Base uses by default a white background color. QPalette::Window would then use a gray background color (that would explain why windows have gray background by default).
But I think my guess is definitely wrong and I'm far away from the truth..
I tried something like this:
@QPalette palette;
palette.setColor(QPalette::Window, QColor(255,0,0));
QLineEdit* edit = new QLineEdit;
edit->setPalette(palette);@ But that doesn't work. Well, the following works:
@QPalette palette;
palette.setColor(QPalette::Base, QColor(255,0,0));
QLineEdit* edit = new QLineEdit;
edit->setPalette(palette);@ That would underline my guess that QLineEdit uses QPalette::Base. so I tried the following:
@QPalette palette;
palette.setColor(QPalette::Window, QColor(255,0,0));
QLineEdit* edit = new QLineEdit;
edit->setBackgroundRole(QPalette::Window);
edit->setPalette(palette);@ But this doesn't work!I think I didn't get it, so If anyone could explain that stuff to me, I would really appreciate that :-)
-
I meant that whatever the color of the reference you are using, you'd ensure that you'll get the same.
AFAIK, it done so you can implement your own QStyle and colorize your widgets the way you like it e.g. Qt Creator has it's own style. But again, the styles are free to ignore the colors you might be setting on the palette.