Palettes

From qtnode

Jump to: navigation, search

A widget's color information is stored in a QPalette object. This object holds the different colors for all of the widget's different situations and types. To change a particular widget's palette (colors), you first need to get a copy of the current QPalette in use:

QPalette p = MyWidget->palette();

Now you can associate a color with a particular situation (ColorRole) as desired. For example, if MyWidget is a QPushButton, and you would like the color of that button to be red, you would set the ColorRole to QPalette::Button, and the QColor to Qt::red as follows:

p.setColor(QPalette::Button, Qt::red);

You can also specify when this color is active by passing a ColorGroup as the first argument. For example, if you want the button to be red for active windows and blue for inactive windows, you can instead set the color as follows:

p.setColor(QPalette::Active, QPalette::Button, Qt::red);
p.setColor(QPalette::Inactive, QPalette::Button, Qt::blue);

Then, once you have your palette adjusted the way you prefer, you simply reassign it back to the widget:

MyWidget->setPalette(p);

Note: Under Win32, Qt will default to the WinXP Theme/Style. This style enforces some strange limitations to customizing colors, because of the "rules of Windows". If you are having trouble with a color being displayed, even though it is set in code, this is most likely the culprit. To give yourself the best chance of using custom colors, I would suggest using the "plastique" Theme/Style, which is modeled after KDE. Let us say you have instantiated your QApplication on the stack as MyApplication. To set the "plastique" Theme/Style, you will then immediately call this statement to use the "plastique" style:

MyApplication.setStyle("plastique");

Reference: See QPalette(), QColor(), and QWidget() in Qt Assistant.

Note for Qt 4.1 and up

A common operation when dealing with palettes is to set a custom background color for a widget. In Qt 4.1, setting the background role of the palette has no effect unless setAutoFillBackground(true) is also called, as the background role QPalette::Window is transparent by default.

// change the palette and set a blue background color
QPalette pal = MyWidget->palette();
pal.setBrush( QPalette::Window, QBrush(QColor(128, 128, 255)) );
MyWidget->setPalette(pal);

// without this the background is transparent...
MyWidget->setAutoFillBackground(true);

Using Background Images

Using a custom image as a background for a widget or window is a simple task:

QPalette p = w.palette();
p.setBrush(QPalette::Window, QPixmap(QString("filename")));
w.setPalette(p);

In Qt 4.0.x, Qt::WA_ContentsPropagated must also be set to allow the background image to show through child widgets. This behavior is default in Qt 4.1.x and higher. Note that this is mutually exclusive with the previous option to force a widget's background to be non-transparent, though you can put an image with alpha transparency on a child widget and they'll blend.

Personal tools