Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use case and difference between QPalette and Qt Style Sheet?
Forum Updated to NodeBB v4.3 + New Features

Use case and difference between QPalette and Qt Style Sheet?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 4.0k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      CJhaC 1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        For border color, please see
        https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton

        CJhaC 1 Reply Last reply
        1
        • SGaistS SGaist

          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.

          CJhaC Offline
          CJhaC Offline
          CJha
          wrote on last edited by
          #4

          @SGaist Thanks! If QStyle is free to ignore then what is the point of using it? It will end up producing inconsistent results.

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            For border color, please see
            https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton

            CJhaC Offline
            CJhaC Offline
            CJha
            wrote on last edited by
            #5

            @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?

            mrjjM 1 Reply Last reply
            0
            • CJhaC CJha

              @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?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              alt text

              1 Reply Last reply
              1
              • M Offline
                M Offline
                MajidKamali
                wrote on last edited by MajidKamali
                #7

                If you want to change many things related to look and feel of widgets, you must inherit from QStyle and define controls' looks accordingly. read QStyle 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.

                1 Reply Last reply
                1
                • RokeJulianLockhartR Offline
                  RokeJulianLockhartR Offline
                  RokeJulianLockhart
                  wrote on last edited by RokeJulianLockhart
                  #8

                  If any of you are familiar with KDE Plasma, would this be equivalent to its notion of "colour schemes" (QPalettes) versus "application styles" (QStyles)?

                  Screenshot_20250219_161017.png Screenshot_20250219_161014.png

                  I ask solely because I'm familiar with these concepts, so it would help me understand.

                  When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                  SGaistS 1 Reply Last reply
                  0
                  • RokeJulianLockhartR RokeJulianLockhart

                    If any of you are familiar with KDE Plasma, would this be equivalent to its notion of "colour schemes" (QPalettes) versus "application styles" (QStyles)?

                    Screenshot_20250219_161017.png Screenshot_20250219_161014.png

                    I ask solely because I'm familiar with these concepts, so it would help me understand.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    RokeJulianLockhartR 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      @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.

                      RokeJulianLockhartR Offline
                      RokeJulianLockhartR Offline
                      RokeJulianLockhart
                      wrote on last edited by RokeJulianLockhart
                      #10

                      KColorScheme is a superset of QPalette

                      @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 on QPalette 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.

                      When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved