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. How to force a style sheet recalculation
Forum Updated to NodeBB v4.3 + New Features

How to force a style sheet recalculation

Scheduled Pinned Locked Moved General and Desktop
21 Posts 8 Posters 27.6k Views 1 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.
  • S Offline
    S Offline
    SteveKing
    wrote on last edited by
    #3

    Hi,

    The problem is that if the style for the button is contained in the app wide style sheet, it doesn't get updated when a property of the button is changed unless you change the style sheet of the whole app, or apply the whole app style to the button.

    I think the only solution is to have a separate style sheet for the button and use the setStyleSheet each time the property is changed...

    Regards,
    Steve

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #4

      yes its a perfectly valid scenario that you set a separate "small" style sheet for your button based on some application logic.
      e.g., you could set 3 different colors to your button based on three different scenarios in your application ... then you just set three different stylesheets to your button ... depending on the logic flow

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #5

        Well, I don't think the scenario is that valid to be honest. Steve wanted to use a property in the style sheet, and that is a valid approach. Forcing splitting up of the style, and even setting different styles depending on properties, defeats the purpose IMHO. There should be a valid way to trigger a redraw of a widget if a property affecting the style has been changed.

        SteveKing: does your property emit a signal on change? That would be a minimum requirement to have the style respond to the change, I'd think. I am not at all sure it actually will if there is such a signal, but IMHO it should.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SteveKing
          wrote on last edited by
          #6

          Andre: I think the property emits a signal, but I'll have a look at that and see if adding the signal makes any difference. I guess it wouldn't.

          As for the different style sheets, this isn't an ideal solution but it's the best I can hope for in the short time I have available...

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #7

            from the Qt Docs for Stylesheet syntax:

            bq.
            Warning: If the value of the Qt property changes after the style sheet has been set, it might be necessary to force a style sheet recomputation. One way to achieve this is to unset the style sheet and set it again.

            Setting entire app style sheet is going to be costly and so you can setStyleSheet for specific widget. Why would Qt provide setStyleSheet at a widget level if that was not an optimal way to do things?

            Also, see the "Property Selector" section in the stylesheet syntax doc for further info on how to set styles based on Q_PROPERTY.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SteveKing
              wrote on last edited by
              #8

              Hi Chetankjain,

              I have read that part of the manual, and I understand that "One way to achieve this" is to set the style sheet (emphasis on the one way). I guess what I was asking is, is there another way that's less expensive...

              From my web search and from the replies here it would seem that a better line in the docs would be "The ONLY way to do this..."

              Anyway, I'm happy enough with the multiple style sheet solution so I guess it's problem solved.

              Regards,
              Steve

              1 Reply Last reply
              0
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                Guest
                wrote on last edited by
                #9

                Hi Steve,
                That is "One way" to my knowledge .. and even I'm not sure if that is the ONLY way :)
                Request if any Troll can clarify better ...
                br
                chetan

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  ogoffart
                  wrote on last edited by
                  #10

                  IIRC, calling buton->setStyleSheet("/* /"); should be enough to clear the caches for this widget and force a recomputation of the style.
                  (Notice "/
                  */" and not "" in order to avoid an empty stylesheet that would be ignored)

                  W 1 Reply Last reply
                  1
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on last edited by
                    #11

                    Hi Olivier, setStyleSheet line doesn't compile. It expects a string and if I convert it to regular quotes, the stylesheet is cleared.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      ogoffart
                      wrote on last edited by
                      #12

                      "/* /" is a comment inside Quotes, that is, a string containing a comment. QString("/ */")

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #13

                        [quote author="SteveKing" date="1287412881"]Andre: I think the property emits a signal, but I'll have a look at that and see if adding the signal makes any difference. I guess it wouldn't.[/quote]
                        I fear so too, but you'd say that in an ideal world, it should. Also, I think it would be perfectly possible to do. If the rendering of a widget depends on a property, then a change of that property should, IMHO, cause a trigger to see if a re-render is needed. Just like hovering, getting focus etc. do. As long as there is a signal for the property change, this should be possible. Having to re-set the style sheet is a hackish workaround, IMHO.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #14

                          I created a bugreport suggesting that property changes should trigger an update of the styling automatically: "http://bugreports.qt.nokia.com/browse/QTBUG-14601":http://bugreports.qt.nokia.com/browse/QTBUG-14601

                          [edit: fixed link / chetankjain]

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            genjix
                            wrote on last edited by
                            #15

                            wait, so there's no way to do style changes for hover/selecting a button? There should be :p

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              DenisKormalev
                              wrote on last edited by
                              #16

                              genjix, for hovering or clicking on a button there are pseudo-states in stylesheets.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #17

                                Just for reference:
                                "http://developer.qt.nokia.com/faq/answer/how_can_my_stylesheet_account_for_custom_properties":http://developer.qt.nokia.com/faq/answer/how_can_my_stylesheet_account_for_custom_properties.

                                Here it is indicated that using a unpolish()/polish() pair of calls would be the fastest way to force the update.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #18

                                  When first being classified as "a sensible idea" and asking for more info, the JIRA ticket has now been closed as "Out of scope" because Qt is "more focussed on QML than in widgets" now.

                                  I read in that: f*ck everybody who invested hundreds or thousands of developer hours into widget based applications; everybody who is not immediately jumping onto the immature Quick bandwagon is screwed.

                                  I am really disapointed by this. While I see Quick as a very interesting technology for creating flashy touch UI's, conventional widgets have a huge invested base and do have their advantages. At least it is proven technology. I would have expected to see them live on side by side for quite a while, as there are use cases for both stacks. It seems I was wrong. :-(

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    karolyi
                                    wrote on last edited by
                                    #19

                                    Coming here after googling, just a quick reply:

                                    meanwhile, QStyle.polish() seems to apply the recomputed style on the change widget.

                                    ODБOïO 1 Reply Last reply
                                    0
                                    • K karolyi

                                      Coming here after googling, just a quick reply:

                                      meanwhile, QStyle.polish() seems to apply the recomputed style on the change widget.

                                      ODБOïO Offline
                                      ODБOïO Offline
                                      ODБOï
                                      wrote on last edited by
                                      #20

                                      @karolyi said in How to force a style sheet recalculation:

                                      quick reply

                                      this topic is 8 years old

                                      1 Reply Last reply
                                      0
                                      • O ogoffart

                                        IIRC, calling buton->setStyleSheet("/* /"); should be enough to clear the caches for this widget and force a recomputation of the style.
                                        (Notice "/
                                        */" and not "" in order to avoid an empty stylesheet that would be ignored)

                                        W Offline
                                        W Offline
                                        WolfgangGriech
                                        wrote last edited by
                                        #21

                                        @ogoffart you're my hero, I had discovered a weird effect on Qt6.9.1 (and later also on some older Qt6 versions), sending an empty or invalid string to setStyleSheet() didn't always clear the previously set stylesheet, but your proposed solution perfectly works, always! Thanks man!

                                        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