Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
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.
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.
For border color, please see
@SGaist Thanks! If QStyle is free to ignore then what is the point of using it? It will end up producing inconsistent results.
@mrjj Thanks! Can Qt Style Sheet be overridden? I am trying to get the border colour but it's not showing up. I am only making the change through Qt Designer, am I supposed to make these changes in CPP file for it to show up?
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.
MajidKamali last edited by MajidKamali
If you want to change many things related to look and feel of widgets, you must inherit from
QStyleand define controls' looks accordingly. read
QStyledocumentation for details.
But if you just want to change some colors,
QPaletteis 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.