Aurélien Gâteau

Qt stylesheets and QPalette

written on Tuesday, January 15, 2008

Whenever I wanted to use standard colors from QPalette within my Qt stylesheet definitions, I used to write code like this:

QColor color = palette().highlight().color(); QString css = QString("QLabel { color: %1; }").arg(; setStyleSheet(css);

Today I discovered an hidden feature of Qt stylesheets (either that, or I missed it in the doc): you can reference QPalette colors from within Qt stylesheet definitions! The above code can be rewritten like this:

QString css = "QLabel { color: palette(highlight); }"; setStyleSheet(css);

Isn't it great?

Update: Just found the documentation for this feature.

This post was tagged css, kde, qt and tips