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. Help setting background-color of QPushButton with QPropertyAnimation
Forum Update on Monday, May 27th 2025

Help setting background-color of QPushButton with QPropertyAnimation

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++
4 Posts 2 Posters 669 Views
  • 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.
  • E Offline
    E Offline
    Ewan Green
    wrote on last edited by
    #1

    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);
      this->setGraphicsEffect(effect);
      
      QPropertyAnimation *animation = new QPropertyAnimation(effect,"color");
      animation->setStartValue(QColor(qRgb(255,255,255)));
      animation->setEndValue(QColor(qRgb(221,221,237)));
      animation->setDuration(400);
      animation->setLoopCount(1);
      animation->start();
    }
    

    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.

    Ewan Green

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      1
      • SGaistS SGaist

        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

        E Offline
        E Offline
        Ewan Green
        wrote on last edited by
        #3

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

        Ewan Green

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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