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. Override value in stylesheet for specific QWidget
Forum Update on Tuesday, May 27th 2025

Override value in stylesheet for specific QWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 3.1k Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by Mark81
    #1

    From here I learnt how to use css selectors to override the current style in my stylesheet. Now I have a different scenario when this method is quite hard to use.

    At run-time I create a lot of QWidgets (for this example let's say they are all QLabel) and make a network connection to a remote machine. Whenever I receive data from the machine I parse it and I get a list of colors to be assigned to some of those hundreds of QLabel. This happen every 100 ms.

    I think it's quite hard to change the stylesheet for each one every time... is there a simpler way to override background and foreground (text) color for a QLabel with a stylesheet is already applied, without changing it?

    The best would be use QPalette but unfortunately the stylesheet has the precedence.
    Any other idea?

    JonBJ 1 Reply Last reply
    0
    • M Mark81

      From here I learnt how to use css selectors to override the current style in my stylesheet. Now I have a different scenario when this method is quite hard to use.

      At run-time I create a lot of QWidgets (for this example let's say they are all QLabel) and make a network connection to a remote machine. Whenever I receive data from the machine I parse it and I get a list of colors to be assigned to some of those hundreds of QLabel. This happen every 100 ms.

      I think it's quite hard to change the stylesheet for each one every time... is there a simpler way to override background and foreground (text) color for a QLabel with a stylesheet is already applied, without changing it?

      The best would be use QPalette but unfortunately the stylesheet has the precedence.
      Any other idea?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Mark81
      What do you mean by "with a stylesheet is already applied, without changing it"? You have to change the stylesheet if you want to change the color without going via QPalette...

      I do not know whether 100ms is very frequent for stylesheet changes, but you can always try....

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mark81
        wrote on last edited by Mark81
        #3

        I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget) . I would go with QPalette, but it doesn't work when a stylesheet is applied as far as I know.

        From your answer I understand this is the only way to do it? It's a huge limitation for the Qt framework so much oriented to the user experience and appearance!

        JonBJ 1 Reply Last reply
        0
        • M Mark81

          I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget) . I would go with QPalette, but it doesn't work when a stylesheet is applied as far as I know.

          From your answer I understand this is the only way to do it? It's a huge limitation for the Qt framework so much oriented to the user experience and appearance!

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Mark81

          I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget)

          I still don't get what you are trying to do here/have a problem with. I don't use "resources", but I presume that once you have set the stylesheet from resource initially that will have actually set the string returned by QWidget::stylesheet() (http://doc.qt.io/qt-5/qwidget.html#styleSheet-prop)? So to change something you call QWidget::stylesheet(), make your change, and then set that whole string back via QWidget::setStylesheet() (I do this to alter individual styles). That means you should not have to re-fetch the resource. (Or maybe you mean your resource is only a stylesheet for the whole application, nothing for individual widgets? Do you understand that to achieve what you want we are talking about calling an individual widget's setStylesheet(), not for the whole application?)

          Don't know what you mean by "huge limitation". You have to do something to make the change, and since QPalette is overridden by an explicit stylesheet you need to do it the stylesheet way?

          P.S.
          I could tell you how you could achieve this via "dynamic properties" on your widget (which I use for various reasons). However, since changing a dynamic property still requires you to call QWidget::setStylesheet() to make the dynamic property update actually be reflected properly on the widget, I don't see the point....

          M 1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            Why not make a custom widget that draw some text using a color for bg and one for text color and simply
            set it when data comes and call update() on it ?
            It sounds like the usage of those labels are not part of the stylesheet concept for the user and
            in reality, the label plays the role of some data-driven Status widget.
            So i would just make a small custom Status widget and use that instead of labels since it
            would be only a paintEvent override and a method to set 2 colors to be used.

            M 1 Reply Last reply
            3
            • JonBJ JonB

              @Mark81

              I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget)

              I still don't get what you are trying to do here/have a problem with. I don't use "resources", but I presume that once you have set the stylesheet from resource initially that will have actually set the string returned by QWidget::stylesheet() (http://doc.qt.io/qt-5/qwidget.html#styleSheet-prop)? So to change something you call QWidget::stylesheet(), make your change, and then set that whole string back via QWidget::setStylesheet() (I do this to alter individual styles). That means you should not have to re-fetch the resource. (Or maybe you mean your resource is only a stylesheet for the whole application, nothing for individual widgets? Do you understand that to achieve what you want we are talking about calling an individual widget's setStylesheet(), not for the whole application?)

              Don't know what you mean by "huge limitation". You have to do something to make the change, and since QPalette is overridden by an explicit stylesheet you need to do it the stylesheet way?

              P.S.
              I could tell you how you could achieve this via "dynamic properties" on your widget (which I use for various reasons). However, since changing a dynamic property still requires you to call QWidget::setStylesheet() to make the dynamic property update actually be reflected properly on the widget, I don't see the point....

              M Offline
              M Offline
              Mark81
              wrote on last edited by Mark81
              #6

              @JonB said in Override value in stylesheet for specific QWidget:

              Do you understand that to achieve what you want we are talking about calling an individual widget's setStylesheet(), not for the whole application?)

              You got the point. I didn't understand that! I thought I had to mess with the quite complex stylesheet of the whole application. Now it makes more sense :-)

              I was afraid that setting an individual stylesheet that defines only the background color would "reset" all the other application-level styles. This is why I didn't understand it before. But if it overrides just what I'm going to change it's perfect!

              1 Reply Last reply
              1
              • mrjjM mrjj

                Hi
                Why not make a custom widget that draw some text using a color for bg and one for text color and simply
                set it when data comes and call update() on it ?
                It sounds like the usage of those labels are not part of the stylesheet concept for the user and
                in reality, the label plays the role of some data-driven Status widget.
                So i would just make a small custom Status widget and use that instead of labels since it
                would be only a paintEvent override and a method to set 2 colors to be used.

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                @mrjj Yes and no :-) Yes because the content of the widget is actually not related to the stylesheet (text and colors) but the style of the widget itself matters. For this reason I'm trying to use an already styled widgets (like QLabel or QLineEdit) in order to keep them like the other controls on the form.

                Now that I understand what @JonB said (I apologize for the misunderstanding) I'm going to try the more obvious way using individual stylesheets.

                1 Reply Last reply
                1

                • Login

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