Results 1 to 1 of 1

Thread: QGraphicsOpacityEffect animation on QPushButton not working in some cases

  1. #1
    Join Date
    Mar 2006
    Posts
    56
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsOpacityEffect animation on QPushButton not working in some cases

    Hi,

    I have a button whose opacity changes when the mouse moves on top of the it.
    I’m using a QGraphicsOpacityEffect to change the button opacity and I’m animating the opacity using a QPropertyAnimation.

    The animation works greatly unless I add a QGLWidget in the same window where the button is.

    I wrote a very simple application for debugging. It is a dialog with a QGLWidget and two opacity animated buttons, one inside the QGLWidget and one outside it.
    If the QGlWidget is not created then the button animation is working very well. When I create the QGLWidget and add it to the dialog the button animation stops working.
    The QGLWidget does nothing, it just draws a black background.

    Here is the code:

    Qt Code:
    1. static const qreal START_OPACITY = 0.3;
    2.  
    3.  
    4. // PushButtonAnimated code
    5. PushButtonAnimated::PushButtonAnimated(QWidget * parent)
    6. : QPushButton(parent)
    7. {
    8. QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
    9. effect->setOpacity(START_OPACITY);
    10. setGraphicsEffect(effect);
    11.  
    12. animation = new QPropertyAnimation(effect, "opacity");
    13. animation->setDuration(500);
    14. animation->setStartValue(START_OPACITY);
    15. animation->setEndValue(1.0);
    16. }
    17.  
    18. void PushButtonAnimated::enterEvent(QEvent *e)
    19. {
    20. if( animation->state() == QAbstractAnimation::Running )
    21. animation->pause();
    22. animation->setDirection(QAbstractAnimation::Forward);
    23.  
    24. animation->start();
    25. }
    26.  
    27. void PushButtonAnimated::leaveEvent(QEvent *e)
    28. {
    29. if( animation->state() == QAbstractAnimation::Running )
    30. animation->pause();
    31. animation->setDirection(QAbstractAnimation::Backward);
    32.  
    33. animation->start();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // QGLWidget, has an animated button
    2. // just draws a black background
    3. MyGLWidget::MyGLWidget(QWidget *parent)
    4. : QGLWidget(parent)
    5. {
    6. PushButtonAnimated *pb = new PushButtonAnimated(this);
    7. pb->setIcon( QIcon("icon.png") );
    8. }
    9.  
    10. void MyGLWidget::initializeGL()
    11. {
    12. // Set up the rendering context, define display lists etc.:
    13. qglClearColor( Qt::black );
    14. // Setting the Hidden Surface Removal process
    15. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    16. }
    17.  
    18. void MyGLWidget::resizeGL(int w, int h)
    19. {
    20. // setup viewport, projection etc.:
    21. glViewport(0, 0, (GLint)w, (GLint)h);
    22. }
    23.  
    24. void MyGLWidget::paintGL()
    25. {
    26. // draw the scene:
    27. // ...
    28. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // this is the main
    2. int main( int argc, char *argv[] )
    3. {
    4. QApplication appMain(argc, argv);
    5.  
    6. // build interface
    7. QDialog *dg = new QDialog();
    8.  
    9. PushButtonAnimated *pb = new PushButtonAnimated(dg);
    10. pb->setIcon( QIcon(":/test/icon.png") );
    11. QSpacerItem *spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
    12.  
    13.  
    14. QGridLayout *layout = new QGridLayout(dg);
    15. layout->addItem(spacer, 1, 0);
    16. layout->addWidget(pb, 1, 1);
    17.  
    18. bool showOgl = false;
    19. // bool showOgl = true;
    20. if( showOgl )
    21. {
    22. // IF THIS CODE IS ENABLED THEN
    23. // THE BUTTON ANIMATION IS NO MORE WORKING
    24. MyGLWidget *glWidget = new MyGLWidget(dg);
    25. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    26. glWidget->setSizePolicy(sizePolicy);
    27. layout->addWidget(glWidget, 0, 0);
    28. }
    29.  
    30. dg->show();
    31. dg->resize(500, 500);
    32.  
    33. return appMain.exec()
    34. }
    To copy to clipboard, switch view to plain text mode 

    Do you have any hint on how to have both buttons animation working?

    Thanks in advance for your help

    P.S.
    attached is the full example code
    Attached Files Attached Files

Similar Threads

  1. Animation effect of gif in QPushButton
    By santosh.kumar in forum Qt Programming
    Replies: 6
    Last Post: 3rd March 2011, 11:14
  2. Replies: 3
    Last Post: 6th February 2010, 22:28
  3. Animation not working on transition between states
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 2nd January 2010, 15:01
  4. QPushButton fade effect without animation
    By Kostanev in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2008, 09:46
  5. QProcess not working in some cases
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2006, 09:58

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.