Use case and difference between QPalette and Qt Style Sheet?
-
Hi! I am new to Qt. This might sound like a stupid question, but can someone please explain to me what is the difference between QPalette and Qt Style Sheets and when am I supposed to use either of these? I am trying to change the look of QPushButton (both background and borders) using either in Qt Designer but I am unable to get it done. Here is what happens:
QPalette: Can change background colour of windows and text colour on push buttons but not the background colour of push buttons.
Qt Style Sheet: Can change background colour of push buttons but not the border colour.
I tried searching for this on the internet but didn't find much info, this page on Qt Wiki is too small to properly explain the difference and use case for both QPalette and Qt Style Sheet. And, this question on Qt Forum is old and there is just one line explanation.
-
Hi,
QPalette is used by QStyle to get information on what color to use to render controls. QStyle is free to ignore these hints in order to stay in ligne with the OS style implements.
Qt stylesheets offer a larger set of customization but in return won't use the original style implemented for the OS.
-
Hi
For border color, please see
https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton -
-
@CJha
Hi The point of QPalette is to allow tweaking some of the colors
that the platform allows to change.
You can apply StyleSheet in Designer or code.
UI files are translated to c++ code so Its the same.Stylesheets are cascading so it will also affect the children of a Widget. Make sure
to read the section about selectors so you target only what you want. -
If you want to change many things related to look and feel of widgets, you must inherit from
QStyle
and define controls' looks accordingly. readQStyle
documentation for details.
But if you just want to change some colors,QPalette
is the best way. actually stylesheets are the last option, although you can customize many things. BUT stylesheets are bad when you want to change application palette on the fly (e.g. user can choose from a set of predefined palettes), because stylesheet always override palette, so when user changes application palette, widgets which have stylesheet, do not change according to selected palette and remain with old colors, so inconsistency occurs in look and feel of application. -
-
@RokeJulianLockhart I think so. If I am not mistaking, KColorScheme is a superset of QPalette while the application styles are likely akin to QStyle.
However, styling Plasma can be done through other means such as Plasma Theme which is a set of SVG files. You might have better answers on KDE's forum/reddit. -
RokeJulianLockhartreplied to SGaist 19 days ago last edited by RokeJulianLockhart 3 May 2025, 14:15
KColorScheme
is a superset ofQPalette
@SGaist, it indeed is, per
api.kde.org/frameworks/kcolorscheme/html/classKColorScheme.html
:KColorScheme
currently provides access to the system color palette that the user has selected (in the future, it is expected to do more). It greatly expands onQPalette
by providing five distinct "sets" with several color choices each, covering background, foreground, and decoration colors.Thanks.
Styling Plasma can be done through other means such as Plasma Theme which is a set of SVG files.
Yeah, it's a bit of a mess:
thelibre.news/the-future-of-kde-styling-and-design
.