Results 1 to 10 of 10

Thread: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

  1. #1
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Qt products
    Qt Jambi
    Platforms
    Unix/X11

    Default How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    I want to progressively decrease the opacity of a QPushButton over a time of 2 seconds to complete transparency. For that I used the QPropertyAnimation class and used the property "windowOpacity" of the button to achieve the effect. But that worked only for a standalone QPushButton. When I assigned a parent to the button, the effect disappeared. Is there any way of achieving the same effect for child buttons ?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    This is the side effect of child widgets not being windows in Qt.
    You can only do the opacity on the parent.

    edit: however, this doesn't mean it is impossible.
    Use a style specific for your button.
    Last edited by tbscope; 17th October 2010 at 18:31.

  3. The following user says thank you to tbscope for this useful post:

    daudiam (17th October 2010)

  4. #3
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Qt products
    Qt Jambi
    Platforms
    Unix/X11

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Thanks. But how do we specify transparency levels in buttons ? I mean, any functions, any property which I can set.

  5. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Read about QPalette.

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

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Let's try to go for a quick solution. Subclass QPushButton, add a qreal "opacity" property to the button subclass (so that you can animate its value) and reimplement the paint event. In the event implementation there are two things that might work. First you might try setting up a painter and calling QPainter::setOpacity() before calling the base class implementation or if this doesn't work, call the base class implementation first and then use QPainter::setCompositionMode() to setup a composition mode that will allow you to paint the alpha channel to what is already painted using an opacity of your choice. Which composition mode to choose is left as an exercise to the reader.
    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.


  7. #6
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Following up with the suggestions from wysota (thanks wysota), I have a similar requirement when using a QLabel with a pixmap. The following code segment is my
    attempt at following wysota's instructions for using a subclass in combination with a
    QPropertyAnimation. I can tell from breakpoints to the setOpacity API, that the API is being called properly, but the rendering within the paintEvent never displays the QLabel pixmap with any change to the transparency. The pixmap I am using with my QLabel is a PNG with alpha content. For those of you interested in attempting a similar quest, this might be a good starting point. It would be good to the community to get this
    to work as it is most useful.

    Qt Code:
    1. m_propAnimationFadeOverscrollTop = new QPropertyAnimation(ui->overscrollTop, "windowOpacity");
    2. m_propAnimationFadeOverscrollTop->setDuration(5000);
    3. m_propAnimationFadeOverscrollTop->setStartValue(0.0);
    4. m_propAnimationFadeOverscrollTop->setEndValue(1.0);
    5. m_propAnimationFadeOverscrollTop->start();
    6.  
    7.  
    8. class LabelAnimate : public QLabel
    9. {
    10. Q_OBJECT
    11. Q_PROPERTY(double windowOpacity READ windowOpacity WRITE setWindowOpacity DESIGNABLE isWindow)
    12.  
    13. public:
    14. LabelAnimate(QWidget *parent = 0): QLabel(parent),
    15. m_alpha(0),
    16. opacity(0.0)
    17. {
    18. // Start with the image completely transparent
    19. UpdateAlphaChannel(m_alpha);
    20. }
    21.  
    22. public:
    23. qreal windowOpacity() const
    24. {
    25. return opacity;
    26. }
    27.  
    28. void setWindowOpacity(qreal opacity)
    29. {
    30. opacity = qBound(qreal(0.0), opacity, qreal(1.0));
    31. setAttribute(Qt::WA_WState_WindowOpacitySet);
    32. }
    33.  
    34. void fadeMore()
    35. {
    36. if (m_alpha > 0)
    37. {
    38. UpdateAlphaChannel(--m_alpha);
    39. }
    40. }
    41.  
    42. void fadeLess()
    43. {
    44. if (m_alpha < 255)
    45. {
    46. UpdateAlphaChannel(++m_alpha);
    47. }
    48. }
    49.  
    50. int getAlpha() { return m_alpha; }
    51. void setAlpha(int alpha) { UpdateAlphaChannel(alpha); }
    52.  
    53. protected:
    54. void paintEvent(QPaintEvent *event)
    55. {
    56. // From wysota: setting up a painter and calling QPainter::setOpacity() before calling the base class implementation
    57. // or if this doesn't work, call the base class implementation first and then use QPainter::setCompositionMode() to
    58. // setup a composition mode that will allow you to paint the alpha channel to what is already painted using an opacity of
    59. // your choice
    60. #ifdef METHOD1
    61. p.begin(this);
    62. p.setOpacity(opacity);
    63. p.end();
    64.  
    65. // Call base class
    66. QLabel::paintEvent(event);
    67. #else
    68. // Call base class
    69. QLabel::paintEvent(event);
    70.  
    71. p.begin(this);
    72. p.setCompositionMode(QPainter::CompositionMode_Clear);
    73. //::CompositionMode_DestinationIn);
    74. p.end();
    75. #endif
    76. }
    77.  
    78. QPixmap &setAlpha(QPixmap &px, int& val)
    79. {
    80. QPixmap alpha = px;
    81. QPainter p(&alpha);
    82. p.fillRect(alpha.rect(), QColor(val, val, val));
    83. p.end();
    84. px.setAlphaChannel(alpha);
    85. return px;
    86. }
    87.  
    88. void UpdateAlphaChannel(int alpha)
    89. {
    90. const QPixmap *px = this->pixmap();
    91.  
    92. // Check for NULL before converting to reference
    93. if (px != 0)
    94. {
    95. // Convert pointer to reference
    96. QPixmap pm(*px);
    97.  
    98. // Change alpha using reference argument
    99. setAlpha(pm, alpha);
    100. }
    101. }
    102.  
    103. private:
    104. int m_alpha; // Current alpha level
    105. qreal opacity; // opacity
    106. };
    To copy to clipboard, switch view to plain text mode 


    Added after 1 28 minutes:


    Further observation, I am not getting repaints for the pixmap as changes are made via the QPropertyAnimation to setOpacity despite my calls to update() and refresh()

    Any ideas?

    Qt Code:
    1. void setWindowOpacity(qreal opacity)
    2. {
    3. qDebug() << "LabelAnimate::setWindowOpacity:" << opacity;
    4.  
    5. opacity = qBound(qreal(0.0), opacity, qreal(1.0));
    6. setAttribute(Qt::WA_WState_WindowOpacitySet);
    7.  
    8. // Force repaint
    9. this->update();
    10. this->repaint();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by wysota; 18th January 2011 at 22:04. Reason: missing [code] tags

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

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    If you close the painter right after you change its attributes, I doubt your changes will have any effect.

    Anyway, here is a quick solution using some of the additions to Qt 4.6+.
    Qt Code:
    1. QLabel *label = new QLabel;
    2. someLayout->addWidget(label);
    3. label->setText("abcdefgh");
    4. QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(label);
    5. label->setGraphicsEffect(effect);
    6. QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
    7. anim->setStartValue(0.01);
    8. anim->setEndValue(1.0);
    9. anim->setDuration(5000);
    10. anim->start();
    To copy to clipboard, switch view to plain text mode 
    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.


  9. The following 3 users say thank you to wysota for this useful post:

    bob2oneil (19th January 2011), lamp (19th April 2011), smmalmansoori (2nd January 2012)

  10. #8
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Just got done implementing a fade in/fade out on a QLabel with Pixmap using wysota's direction and the QGraphicsAnimation framework, so thanks again to wysota.

    My application is targeted towards embedded Linux, and therefore performance is a premium. Since I am now using portions of the QT graphics framework (and because of other requirements I have such as animated
    sceen transitions) it would seem that I have moved into the graphics based world of Qt. Does this implementation decision imply any particular build option(s) for the Qt library for embedded Linux to support the graphics
    engine? Are there any particular URLs that discuss optimizing Qt via build options for embedded Linux and performance tuning?

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

    Default Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    What's more important is optimizing your own application. There is not much you can do by recompiling Qt with different options.
    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.


  12. #10
    Join Date
    Nov 2011
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How to animate the transparency of a child QPushButton using QPropertyAnimation ?

    Thanks for this post - this is what I needed as well! Much simpler than some of the more esoteric solutions I've seen about subclassing this and that and overriding the paint method, etc. I am fairly new to Qt so perhaps these methods were what was necessary prior to Qt 4.6??

    jgoluch



    Quote Originally Posted by wysota View Post
    If you close the painter right after you change its attributes, I doubt your changes will have any effect.

    Anyway, here is a quick solution using some of the additions to Qt 4.6+.
    Qt Code:
    1. QLabel *label = new QLabel;
    2. someLayout->addWidget(label);
    3. label->setText("abcdefgh");
    4. QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(label);
    5. label->setGraphicsEffect(effect);
    6. QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
    7. anim->setStartValue(0.01);
    8. anim->setEndValue(1.0);
    9. anim->setDuration(5000);
    10. anim->start();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPropertyAnimation and animation speed
    By themk in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2010, 00:05
  2. QPropertyAnimation and QStackedLayout:functionality.
    By savaliya_ambani in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 24th September 2010, 08:33
  3. Using QPropertyAnimation with QGraphicsPixmapItem
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2010, 09:47
  4. Replies: 1
    Last Post: 8th January 2010, 12:21
  5. Replies: 2
    Last Post: 11th November 2009, 08:03

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.