Results 1 to 4 of 4

Thread: QPushButton Icon Animation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2014
    Posts
    20
    Thanks
    2

    Red face QPushButton Icon Animation

    Hello,

    is there anyone who could give me some hints how can i do the rotate animation of the icon in pushbutton?
    like waiting, etc.

    i search some mostly they are implemented with qml.
    what i would like to have is however a qpushbutton.

    thank you

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton Icon Animation

    You can inherit the QPushButton and override the paintEvent ,,,
    You will also need a timer or better use QVariantAnimation to update the drawing on your widget .
    Hope you get the idea

  3. #3
    Join Date
    Jun 2014
    Posts
    20
    Thanks
    2

    Default Re: QPushButton Icon Animation

    Quote Originally Posted by aamer4yu View Post
    You can inherit the QPushButton and override the paintEvent ,,,
    You will also need a timer or better use QVariantAnimation to update the drawing on your widget .
    Hope you get the idea
    Hello, thanks for ur suggestion.

    i have tried the idea and it works. The only drawback ive found is that the icon looks blured during animation.
    i post my test code here, hope anyone could point it out where is the problem.

    Qt Code:
    1. void LineEditButton::setRotationEnable(bool enable)
    2. {
    3. if (enable) {
    4. m_animator = new QPropertyAnimation(this);
    5. m_animator->setTargetObject(this);
    6. m_animator->setStartValue(0);
    7. m_animator->setEndValue(360);
    8. m_animator->setDuration(2000);
    9.  
    10. m_animator->start();
    11. connect(m_animator, SIGNAL(valueChanged(const QVariant&)), SLOT(update()));
    12. connect(m_animator, SIGNAL(finished()), m_animator, SLOT(start()));
    13. }
    14. else {
    15. m_animator->stop();
    16. delete m_animator;
    17. m_animator = NULL;
    18. }
    19. }
    20.  
    21. void LineEditButton::paintEvent(QPaintEvent* event)
    22. {
    23. Q_UNUSED(event);
    24.  
    25. initStyleOption(&option);
    26.  
    27. static QIcon s_emptyIcon;
    28.  
    29. // draw rest of button
    30. QStylePainter p(this);
    31. const QPixmap& pixmap = option.icon.pixmap(16, 16, isEnabled() ? QIcon::Disabled : QIcon::Normal);
    32. option.icon = s_emptyIcon;
    33. p.drawControl(QStyle::CE_PushButton, option);
    34. p.save();
    35.  
    36. // rotate
    37. QRect pixmapRect = style()->itemPixmapRect(rect(), Qt::AlignCenter, pixmap);
    38. p.translate(pixmapRect.center());
    39. p.rotate(m_animator ? m_animator->currentValue().toInt() : 0);
    40. p.drawPixmap(QRect(-8, -8, 16, 16), pixmap);
    41. p.restore();
    42. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushButton Icon Animation

    If you start rotating an icon which is 16x16 then you will be getting artifacts which you describe as "blur". I suggest to minimize the number of rotations. A human eye sees the animation as fluent at about 16 frames per second. So instead of animating 360 frames during 2 seconds which is larger than your monitor can show anyway, animate 30 frames during those 2 seconds. For each frame the icon will be rotated 12 degrees further.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 3rd September 2012, 12:11
  2. Animation effect of gif in QPushButton
    By santosh.kumar in forum Qt Programming
    Replies: 6
    Last Post: 3rd March 2011, 11:14
  3. QPushButton - Only show the Icon
    By graciano in forum Newbie
    Replies: 9
    Last Post: 19th September 2009, 21:15
  4. QPushButton Icon Image
    By QbelcorT in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 14:26
  5. QPushButton fade effect without animation
    By Kostanev in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2008, 09:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.