Qt World Summit: Submit your Presentation

Custom DelayButton background not changing

  • 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));
             QProxyStyle::drawControl(element, &buttonOption, painter, widget);
         QProxyStyle::drawControl(element, option, painter, widget);

    Can you help me please to find mistake?

  • Moderators

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

    how do you call this function?

    can you show that?

  • 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


    I found this:
    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.

  • Moderators

    @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!

  • Lifetime Qt Champion

    @MiklYarochkin said in Custom DelayButton background not changing:


    You can see here what drawControl really does

    (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 :)

  • 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

    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"

Log in to reply