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. Understanding QPalette

Understanding QPalette

Scheduled Pinned Locked Moved Solved General and Desktop
kdecolorsqpallete
7 Posts 3 Posters 2.8k Views
  • 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.
  • T Offline
    T Offline
    tim-hilt
    wrote on 8 Jan 2023, 00:09 last edited by
    #1

    I found this static method in a KDE-related project:

     void KColorScheme::adjustBackground(QPalette &palette, BackgroundRole newRole, QPalette::ColorRole color,
                                         ColorSet set, KSharedConfigPtr config)
     {
         palette.setBrush(QPalette::Active,   color, KColorScheme(QPalette::Active,   set, config).background(newRole));
         palette.setBrush(QPalette::Inactive, color, KColorScheme(QPalette::Inactive, set, config).background(newRole));
         palette.setBrush(QPalette::Disabled, color, KColorScheme(QPalette::Disabled, set, config).background(newRole));
     }
    

    however, I can't really make sense of it! The method is supposed to adjust a background-color of an existing color-scheme. I know the values of newRole, set and config for my use-case, but what are palette and color supposed to be?

    I assumed, that I would have to pass a QColor and some scope-information on which color to change, but not a whole color-palette.

    I tried to get support in KDE-forums first, but didn't have any sucess. Is this something, that's implementation specific to KDE? If so, I will keep researching it in their forums and chat-rooms.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 8 Jan 2023, 10:51 last edited by Axel Spoerl 1 Aug 2023, 11:22
      #4

      Do I understand correctly that you want to modify a specific brush of a given palette by replacing its color with a given QRgb value?
      In that case I would not use that KDE method at all and set the color directly to the brush, using QPalette::setColor().

      A QPaletteis a set of brushes, each of which has a unique combination of QPalette::ColorGroup (i.e. active, inactive, disabled) and QPalette::ColorRole(i.e. base, button, text, window text, etc.). A QBrushdefines a color and the pattern with which is is drawn. That can be a pre-defined pattern or a texture defined by a pixmap.

      What the KDE method does in essence is: It takes a colorRolein the parameter color (which one could be mislead to interpret as a color). It also takes the current KDE color set and the configuration. It then reads the active, inactive and disabled background brushes from the KDE configuration and sets them in the palette to all possible color groups of the color role passed as an argument.

      If the target is to change only the color of the given palette's brushes, I'd code it directly or write a specific method for this task. Could be something like

       void myClass::adjustWhatever(QPalette &palette, QPalette::ColorRole colorRole, const QColor &targetColor)
       {
           palette.setColor(QPalette::Active, colorRole, targetColor);
           palette.setColor(QPalette::Inactive, colorRole, targetColor);
           palette.setColor(QPalette::Disabled, colorRole, targetColor);
       }
      

      As for the RGB value, it comes in handy that QColorhas both a QRgband an RGBA constructor. So you can call the method like

      myClass.adjustWhatever(myPalette, myColorRole, QColor(r, g, b, a)); // passing rgb and a as int values
      

      or

      myClass.adjustWhatever(myPalette, myColorRole, myQRgb); // passing a QRgb directly
      

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 8 Jan 2023, 02:11 last edited by Chris Kawa 1 Aug 2023, 02:14
        #2

        Different widgets use different roles for their backgrounds. For example a text input might use QPalette::Base and a dialog might use QPalette::Window. The color parameter controls which role you want to modify. the default is QPalette::Base. This KDE method is a bit weird, since nothing stops you from passing a text or any other role here, but I guess it's just relying on the user to play nice.

        As for palette parameter that's the palette you're modifying. You can't really get a reference to a palette used by given widget directly, only a copy, so a common pattern for changing palette is get->modify->set, e.g. to change the Base color for entire application would look something like this:

        QPalette p = qApp->palette();
        KColorScheme::adjustBackground(p, ...
        qApp->setPalette(p);
        
        T 1 Reply Last reply 8 Jan 2023, 10:46
        1
        • C Chris Kawa
          8 Jan 2023, 02:11

          Different widgets use different roles for their backgrounds. For example a text input might use QPalette::Base and a dialog might use QPalette::Window. The color parameter controls which role you want to modify. the default is QPalette::Base. This KDE method is a bit weird, since nothing stops you from passing a text or any other role here, but I guess it's just relying on the user to play nice.

          As for palette parameter that's the palette you're modifying. You can't really get a reference to a palette used by given widget directly, only a copy, so a common pattern for changing palette is get->modify->set, e.g. to change the Base color for entire application would look something like this:

          QPalette p = qApp->palette();
          KColorScheme::adjustBackground(p, ...
          qApp->setPalette(p);
          
          T Offline
          T Offline
          tim-hilt
          wrote on 8 Jan 2023, 10:46 last edited by
          #3

          @Chris-Kawa thanks for the answer! I understood that the QPallet has to exist before calling this function. No idea where this might come from though. I’ll keep searching!

          As for the second parameter „color“: How would I pass a QRgb-value to the shown function? This is what I would ultimately like to do.

          C 1 Reply Last reply 8 Jan 2023, 11:21
          0
          • A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote on 8 Jan 2023, 10:51 last edited by Axel Spoerl 1 Aug 2023, 11:22
            #4

            Do I understand correctly that you want to modify a specific brush of a given palette by replacing its color with a given QRgb value?
            In that case I would not use that KDE method at all and set the color directly to the brush, using QPalette::setColor().

            A QPaletteis a set of brushes, each of which has a unique combination of QPalette::ColorGroup (i.e. active, inactive, disabled) and QPalette::ColorRole(i.e. base, button, text, window text, etc.). A QBrushdefines a color and the pattern with which is is drawn. That can be a pre-defined pattern or a texture defined by a pixmap.

            What the KDE method does in essence is: It takes a colorRolein the parameter color (which one could be mislead to interpret as a color). It also takes the current KDE color set and the configuration. It then reads the active, inactive and disabled background brushes from the KDE configuration and sets them in the palette to all possible color groups of the color role passed as an argument.

            If the target is to change only the color of the given palette's brushes, I'd code it directly or write a specific method for this task. Could be something like

             void myClass::adjustWhatever(QPalette &palette, QPalette::ColorRole colorRole, const QColor &targetColor)
             {
                 palette.setColor(QPalette::Active, colorRole, targetColor);
                 palette.setColor(QPalette::Inactive, colorRole, targetColor);
                 palette.setColor(QPalette::Disabled, colorRole, targetColor);
             }
            

            As for the RGB value, it comes in handy that QColorhas both a QRgband an RGBA constructor. So you can call the method like

            myClass.adjustWhatever(myPalette, myColorRole, QColor(r, g, b, a)); // passing rgb and a as int values
            

            or

            myClass.adjustWhatever(myPalette, myColorRole, myQRgb); // passing a QRgb directly
            

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            3
            • T tim-hilt
              8 Jan 2023, 10:46

              @Chris-Kawa thanks for the answer! I understood that the QPallet has to exist before calling this function. No idea where this might come from though. I’ll keep searching!

              As for the second parameter „color“: How would I pass a QRgb-value to the shown function? This is what I would ultimately like to do.

              C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 8 Jan 2023, 11:21 last edited by
              #5

              @tim-hilt said:

              No idea where this might come from though. I’ll keep searching!

              There's an application wide palette that you get like I showed in the example and you can locally override palette for each widget, calling QWidget::palette().

              How would I pass a QRgb-value to the shown function?

              That's not what this function is for. It creates a KColorScheme from the set and config and gets a color from that. If you want to set a particular color do what Axel said, set it directly e.g.

              p.setColor(QPalette::Base, QColor(r,g,b));
              
              1 Reply Last reply
              3
              • T Offline
                T Offline
                tim-hilt
                wrote on 8 Jan 2023, 13:13 last edited by
                #6

                @Axel-Spoerl @Chris-Kawa thank you to both of you for the answers. I now understand, that the method doesn't even do what I thought it would do!

                I still don't know how I could get the correct QPalette, but now I have a much better understanding of what these functions actually do and how that differs from what I need.

                Thanks again to both of you.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Axel Spoerl
                  Moderators
                  wrote on 8 Jan 2023, 13:21 last edited by
                  #7

                  @tim-hilt Can you describe verbally which palette should be changed? The application palette? A specific widget’s palette? Something else? We’ll find it out!

                  Software Engineer
                  The Qt Company, Oslo

                  1 Reply Last reply
                  0

                  3/7

                  8 Jan 2023, 10:46

                  • Login

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