Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Custom DelayButton background not changing

    General and Desktop
    qproxystyle qpushbutton
    3
    6
    531
    Loading More Posts
    • 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
      MiklYarochkin last edited by MiklYarochkin

      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.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @MiklYarochkin last edited by

        @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

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

        1 Reply Last reply Reply Quote 0
        • M
          MiklYarochkin last edited by MiklYarochkin

          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.Hilk 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators @MiklYarochkin last edited by

            @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

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

            1 Reply Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion last edited by

              @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 Reply Quote 0
              • M
                MiklYarochkin last edited by

                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 Reply Quote 1
                • First post
                  Last post