跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  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

已排程 已置頂 已鎖定 已移動 General and Desktop
24 貼文 11 Posters 28.8k 瀏覽 3 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • A 離線
    A 離線
    andre
    寫於 最後由 編輯
    #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 條回覆 最後回覆
    0
    • S 離線
      S 離線
      SteveKing
      寫於 最後由 編輯
      #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 條回覆 最後回覆
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        訪客
        寫於 最後由 編輯
        #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 條回覆 最後回覆
        0
        • S 離線
          S 離線
          SteveKing
          寫於 最後由 編輯
          #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 條回覆 最後回覆
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            訪客
            寫於 最後由 編輯
            #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 條回覆 最後回覆
            0
            • O 離線
              O 離線
              ogoffart
              寫於 最後由 編輯
              #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 條回覆 最後回覆
              2
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                訪客
                寫於 最後由 編輯
                #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 條回覆 最後回覆
                0
                • O 離線
                  O 離線
                  ogoffart
                  寫於 最後由 編輯
                  #12

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

                  1 條回覆 最後回覆
                  0
                  • A 離線
                    A 離線
                    andre
                    寫於 最後由 編輯
                    #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 條回覆 最後回覆
                    0
                    • A 離線
                      A 離線
                      andre
                      寫於 最後由 編輯
                      #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 條回覆 最後回覆
                      0
                      • G 離線
                        G 離線
                        genjix
                        寫於 最後由 編輯
                        #15

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

                        1 條回覆 最後回覆
                        0
                        • D 離線
                          D 離線
                          DenisKormalev
                          寫於 最後由 編輯
                          #16

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

                          1 條回覆 最後回覆
                          0
                          • A 離線
                            A 離線
                            andre
                            寫於 最後由 編輯
                            #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 條回覆 最後回覆
                            0
                            • A 離線
                              A 離線
                              andre
                              寫於 最後由 編輯
                              #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 條回覆 最後回覆
                              0
                              • K 離線
                                K 離線
                                karolyi
                                寫於 最後由 編輯
                                #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 條回覆 最後回覆
                                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 離線
                                  ODБOïO 離線
                                  ODБOï
                                  寫於 最後由 編輯
                                  #20

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

                                  quick reply

                                  this topic is 8 years old

                                  1 條回覆 最後回覆
                                  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 離線
                                    W 離線
                                    WolfgangGriech
                                    寫於 最後由 編輯
                                    #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 條回覆 最後回覆
                                    1
                                    • L 離線
                                      L 離線
                                      larsmaxfield
                                      編寫 最後由 larsmaxfield 編輯
                                      #22

                                      The solution from @ogoffart works for me, too.

                                      setStyleSheet() in >6.9.1 doesn't work for me to refresh the stylesheet. I use widget.setStyleSheet(widget.styleSheet()), but that isn't reliable. @WolfgangGriech also describes this.

                                      Perhaps this should be reported as bug? It's been reported and fixed for 6.10.

                                      JonBJ 1 條回覆 最後回覆
                                      0
                                      • L larsmaxfield

                                        The solution from @ogoffart works for me, too.

                                        setStyleSheet() in >6.9.1 doesn't work for me to refresh the stylesheet. I use widget.setStyleSheet(widget.styleSheet()), but that isn't reliable. @WolfgangGriech also describes this.

                                        Perhaps this should be reported as bug? It's been reported and fixed for 6.10.

                                        JonBJ 離線
                                        JonBJ 離線
                                        JonB
                                        編寫 最後由 編輯
                                        #23

                                        @larsmaxfield
                                        FWIW, (I believe) I recall in some Qt 5 version years ago in order to get it reliably to count as a change I had to/ended up doing:

                                        temp = widget.styleSheet();
                                        widget.setStyleSheet("");
                                        widget.setStyleSheet(temp);
                                        
                                        Pl45m4P 1 條回覆 最後回覆
                                        0
                                        • JonBJ JonB

                                          @larsmaxfield
                                          FWIW, (I believe) I recall in some Qt 5 version years ago in order to get it reliably to count as a change I had to/ended up doing:

                                          temp = widget.styleSheet();
                                          widget.setStyleSheet("");
                                          widget.setStyleSheet(temp);
                                          
                                          Pl45m4P 離線
                                          Pl45m4P 離線
                                          Pl45m4
                                          編寫 最後由 編輯
                                          #24

                                          @JonB

                                          Maybe even

                                          • https://doc.qt.io/qt-6/qwidget.html#ensurePolished
                                          • https://doc.qt.io/qt-6/qstyle.html#polish

                                          That might force the style(sheet) to update too.
                                          But usually the widget should do this when a new style(sheet) is applied.


                                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                          ~E. W. Dijkstra

                                          1 條回覆 最後回覆
                                          1

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved