Qt World Summit: Submit your Presentation

Help setting background-color of QPushButton with QPropertyAnimation

  • This is my first post on the Qt forum. I'm sorry if I have posted in the wrong category.

    I would like to have my custom button (from QPushButton) fade into another background color when it is hovered over. I've gotten the hovering part down, and I've gotten the animation (fade) part down, but I haven't figured out how to set the background color with QPropertyAnimation. From what I've seen, it requires QVariant to be used, but QVariant does not provide a background-color type. Ideally I would be using stylesheets for this, but the lack of @keyframes in qss combined with the fact that you cannot use stylesheets with QPropertyAnimation would probably make it very hard to do so. Any help?
    Current code (only sets font color):

    void HoverButton::enterEvent(QEvent *)
      QGraphicsColorizeEffect *effect= new QGraphicsColorizeEffect(this);
      QPropertyAnimation *animation = new QPropertyAnimation(effect,"color");

    If you need any other information, I would be happy to provide it. I'm open to other strategies as well, as long as the result is reasonably similar.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The first issue to fixe: you have t
    a memory leak. Each time you enter that method you create a new animation that is never deleted.

    The second is that you are recreating "effect" again and again for no benefits.

    IIRC, you will have to paint the button yourself as the platform style may ignore these kind of modifications in order to keep the interface consistant.

    This thread might help you get going.

    Hope it helps

  • @SGaist Right. I sort of realized these memory leaks after I had posted, but they can be fixed by declaring effect and animation in the class constructor (I think). Another thing; is the platform theme different from the actual style of the program? I have the application set to Fusion (qApp->setStyle(QStyleFactory::create("Fusion"));, as iirc Fusion ships with Qt) because I am doing all of the "skinning" via. stylesheet, so the only thing the style has to do is have consistent properties across devices. Does Fusion not achieve this?

  • Lifetime Qt Champion

    The stylesheet have their own internal style that does not care at all about the platform style be it the native one or fusion.

Log in to reply