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. Custom DelayButton background not changing
Forum Updated to NodeBB v4.3 + New Features

Custom DelayButton background not changing

Scheduled Pinned Locked Moved Solved General and Desktop
qproxystyleqpushbutton
6 Posts 3 Posters 1.2k 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.
  • M Offline
    M Offline
    MiklYarochkin
    wrote on 9 Jun 2020, 09:58 last edited by MiklYarochkin 6 Sept 2020, 10:00
    #1

    Hello,

    I need to make a PushButton with delayed activation: press during 1 sec-> checked.

    To indicate activation progress to the user, I am trying to use QProxyStyle: for example, change color from lighter to darker every 100ms.

    But it is not working even in a simple form:

    ...
    ui.DelayButtonDemo->setStyle(new DelayButtonStyle);
    ...
    
    class DelayButtonStyle : public QProxyStyle
    {
     void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
     {
         QPushButton* currentDelayButton = (QPushButton*)widget;
    
         if (currentDelayButton->isDown() == true)
         {
             QStyleOptionButton buttonOption(*qstyleoption_cast<const QStyleOptionButton*>(option));
    
             buttonOption.palette.setColor(QPalette::Button,
                 QColor(Qt::red));
    
             QProxyStyle::drawControl(element, &buttonOption, painter, widget);
    
             return;
         }
    
         QProxyStyle::drawControl(element, option, painter, widget);
     }
    }
    

    Can you help me please to find mistake?

    J 1 Reply Last reply 9 Jun 2020, 10:00
    0
    • M MiklYarochkin
      9 Jun 2020, 09:58

      Hello,

      I need to make a PushButton with delayed activation: press during 1 sec-> checked.

      To indicate activation progress to the user, I am trying to use QProxyStyle: for example, change color from lighter to darker every 100ms.

      But it is not working even in a simple form:

      ...
      ui.DelayButtonDemo->setStyle(new DelayButtonStyle);
      ...
      
      class DelayButtonStyle : public QProxyStyle
      {
       void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
       {
           QPushButton* currentDelayButton = (QPushButton*)widget;
      
           if (currentDelayButton->isDown() == true)
           {
               QStyleOptionButton buttonOption(*qstyleoption_cast<const QStyleOptionButton*>(option));
      
               buttonOption.palette.setColor(QPalette::Button,
                   QColor(Qt::red));
      
               QProxyStyle::drawControl(element, &buttonOption, painter, widget);
      
               return;
           }
      
           QProxyStyle::drawControl(element, option, painter, widget);
       }
      }
      

      Can you help me please to find mistake?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 9 Jun 2020, 10:00 last edited by
      #2

      @MiklYarochkin in such cases, it's most likely a blocked event loop

      how do you call this function?

      can you show that?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MiklYarochkin
        wrote on 9 Jun 2020, 10:08 last edited by MiklYarochkin 6 Sept 2020, 10:10
        #3

        Hello,

        Thank you for your fast reaction.

        I updated my code snippet.

        Don't think the loop is blocked.
        This function is called correctly.
        As an experiment, I was trying to play with text color, and I see a change of color:
        QPalette::Button -> QPalette::ButtonText

        ...
        buttonOption.palette.setColor(QPalette::ButtonText,
                    QColor(Qt::red));
        ...
        

        I found this:
        https://doc.qt.io/qt-5/stylesheet-reference.html
        Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color. For example,

        Could it be a reason?

        But I don't know how to set it in drawControl
        If I set border using setStyleSheet, it stops working completely.

        J 1 Reply Last reply 9 Jun 2020, 10:40
        0
        • M MiklYarochkin
          9 Jun 2020, 10:08

          Hello,

          Thank you for your fast reaction.

          I updated my code snippet.

          Don't think the loop is blocked.
          This function is called correctly.
          As an experiment, I was trying to play with text color, and I see a change of color:
          QPalette::Button -> QPalette::ButtonText

          ...
          buttonOption.palette.setColor(QPalette::ButtonText,
                      QColor(Qt::red));
          ...
          

          I found this:
          https://doc.qt.io/qt-5/stylesheet-reference.html
          Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color. For example,

          Could it be a reason?

          But I don't know how to set it in drawControl
          If I set border using setStyleSheet, it stops working completely.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 9 Jun 2020, 10:40 last edited by
          #4

          @MiklYarochkin I see, this, is sadly out of my scope of experience 😔
          Hopefully someone else can help you here!

          Could it be a reason?

          Sounds like it could very well be the reason!


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 9 Jun 2020, 16:42 last edited by
            #5

            @MiklYarochkin said in Custom DelayButton background not changing:

            QStyleOptionButton

            Hi
            You can see here what drawControl really does

            https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#1321
            (and in fusion style/for your platform)

            I did try to copy the code and add brushes etc in my delegate but i was not able to change the background but
            you might have better luck :)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MiklYarochkin
              wrote on 10 Jun 2020, 06:49 last edited by
              #6

              I finally made it:
              in order to operate color, I have to overrule standard behavior

              • set full set of styles using setStyleSheet
              • handle events
              • manage flow

              And I gave up to use it at the end:

              1. a lot of custom code with a doubtful outcome
              2. ugly design
              3. not clear flow

              Solution:
              I use the standard functionality of the standard button. Better explain to the user what "it works and feels the same as all other buttons in Windows"

              1 Reply Last reply
              1

              6/6

              10 Jun 2020, 06:49

              • Login

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