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. Stylesheet recomputing after property change
Qt 6.11 is out! See what's new in the release blog

Stylesheet recomputing after property change

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 8.5k 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.
  • S Offline
    S Offline
    SilvioP.
    wrote on last edited by
    #1

    I need to change the Style of a QPushButton at execution time. So, what I try to do is using a property.
    I set a property for a QPushButton object of my Custom widget then i would try to recompute the stylesheet but I need to not use setStylesheet call.
    I tried bot unpolish/polish and setStyle.
    None of them seems to work. Yhe only way I can obtain what I want is by using setStyleSheet call.
    Here are the examples of what I did:

    MyClass:Myclass()
    {
         QString strStyleSheet = styleSheet();
         strStyleSheet = "QPushButton#myPushButton[mode=\"medalrm\"]{border-image: 
         url(:/Images/buttonYellow.png) stretch stretch;}";
    }
    
    MyClass:function1()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->style()->unpolish(ui->myPushButton);
       ui->myPushButton->style()->polish(ui->myPushButton);
       ui->myPushButton->update();
    }
    
    MyClass:function2()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->setStyle(QApplication::style());
    }
    
    MyClass:function3()
    {
       ui->myPushButton->setProperty("mode", "medalrm");
       ui->myPushButton->setStyleSheet(styleSheet());
    }
    

    Only function3 works properly.
    Any ideas?

    Thank you

    raven-worxR 1 Reply Last reply
    0
    • S SilvioP.

      I need to change the Style of a QPushButton at execution time. So, what I try to do is using a property.
      I set a property for a QPushButton object of my Custom widget then i would try to recompute the stylesheet but I need to not use setStylesheet call.
      I tried bot unpolish/polish and setStyle.
      None of them seems to work. Yhe only way I can obtain what I want is by using setStyleSheet call.
      Here are the examples of what I did:

      MyClass:Myclass()
      {
           QString strStyleSheet = styleSheet();
           strStyleSheet = "QPushButton#myPushButton[mode=\"medalrm\"]{border-image: 
           url(:/Images/buttonYellow.png) stretch stretch;}";
      }
      
      MyClass:function1()
      {
         ui->myPushButton->setProperty("mode", "medalrm");
         ui->myPushButton->style()->unpolish(ui->myPushButton);
         ui->myPushButton->style()->polish(ui->myPushButton);
         ui->myPushButton->update();
      }
      
      MyClass:function2()
      {
         ui->myPushButton->setProperty("mode", "medalrm");
         ui->myPushButton->setStyle(QApplication::style());
      }
      
      MyClass:function3()
      {
         ui->myPushButton->setProperty("mode", "medalrm");
         ui->myPushButton->setStyleSheet(styleSheet());
      }
      

      Only function3 works properly.
      Any ideas?

      Thank you

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @SilvioP.
      do the following:

      widget->style()->unpolish(widget);
      widget->style()->polish(widget);
      QEvent event(QEvent::StyleChange);
      QApplication::sendEvent(widget, &event);
      widget->update();
      widget->updateGeometry();
      

      widget->style()->polish(widget) alone only works as long as the style doesn't change the widget's box model.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      DiracsbracketD 1 Reply Last reply
      4
      • S Offline
        S Offline
        SilvioP.
        wrote on last edited by SilvioP.
        #3

        Thank you for the reply raven.
        Actually the property shoud not affect the widget's box model.
        In any case I tried but still nothing change.

        I'm using Qt 4.8.6

        mrjjM raven-worxR 2 Replies Last reply
        0
        • S SilvioP.

          Thank you for the reply raven.
          Actually the property shoud not affect the widget's box model.
          In any case I tried but still nothing change.

          I'm using Qt 4.8.6

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

          @SilvioP.
          Hi
          Can you test same code in 4.8 ?
          (works in 5.8)
          alt text

          https://www.dropbox.com/s/l7ire2gwa7fy08e/mybutton.zip?dl=0

          1 Reply Last reply
          1
          • S SilvioP.

            Thank you for the reply raven.
            Actually the property shoud not affect the widget's box model.
            In any case I tried but still nothing change.

            I'm using Qt 4.8.6

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @SilvioP. said in Stylesheet recomputing after property change:

            In any case I tried but still nothing change.

            thats what actually happens when you call setStyleSheet().
            I assume you have only posted fragments of your code?! I think you might have an issue somewhere else?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

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

              Thx mrjj I tried your code. It works fine. Actually it works even if I use the normal unpolish/polish/update. I don't know what is wrong with my project. I tried even by using an image; same result: everything is as I espect.
              I don't know what can be wrong with my project.

              mrjjM 1 Reply Last reply
              0
              • S SilvioP.

                Thx mrjj I tried your code. It works fine. Actually it works even if I use the normal unpolish/polish/update. I don't know what is wrong with my project. I tried even by using an image; same result: everything is as I espect.
                I don't know what can be wrong with my project.

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

                @SilvioP.
                Can you trigger the style sheet in Designer?
                Maybe it cant load image ?
                And did you call setObjectName on Myclass so its called myPushButton?

                1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @SilvioP.
                  do the following:

                  widget->style()->unpolish(widget);
                  widget->style()->polish(widget);
                  QEvent event(QEvent::StyleChange);
                  QApplication::sendEvent(widget, &event);
                  widget->update();
                  widget->updateGeometry();
                  

                  widget->style()->polish(widget) alone only works as long as the style doesn't change the widget's box model.

                  DiracsbracketD Offline
                  DiracsbracketD Offline
                  Diracsbracket
                  wrote on last edited by Diracsbracket
                  #8

                  @raven-worx said in Stylesheet recomputing after property change:

                  QEvent event(QEvent::StyleChange);
                  QApplication::sendEvent(widget, &event);
                  widget->update();

                  Thanks! It helped me out too. My dynamic property styling on my QTreeView did not seem to be applied when the property change occurred without user interaction by clicking or key navigation (or even a simple mouse move nudge). In my case, the transition from PLAY to STOP state when a file finishes playing does not require user interaction, so the formatting for the STOP state was not applied. Your solution solved this! Thanks.

                  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