Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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));
    
             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?


  • 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

    ...
    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.


  • 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:

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



  • 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"


Log in to reply