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:

    @
    static const qreal START_OPACITY = 0.3;

    // PushButtonAnimated code
    PushButtonAnimated::PushButtonAnimated(QWidget * parent)
    : QPushButton(parent)
    {
    QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
    effect->setOpacity(START_OPACITY);
    setGraphicsEffect(effect);

    animation = new QPropertyAnimation(effect, "opacity");
    animation->setDuration(500);
    animation->setStartValue(START_OPACITY);
    animation->setEndValue(1.0);
    }

    void PushButtonAnimated::enterEvent(QEvent *e)
    {
    if( animation->state() == QAbstractAnimation::Running )
    animation->pause();
    animation->setDirection(QAbstractAnimation::Forward);

    animation->start();
    }

    void PushButtonAnimated::leaveEvent(QEvent *e)
    {
    if( animation->state() == QAbstractAnimation::Running )
    animation->pause();
    animation->setDirection(QAbstractAnimation::Backward);

    animation->start();
    }@

    @
    // QGLWidget, has an animated button
    // just draws a black background
    MyGLWidget::MyGLWidget(QWidget *parent)
    : QGLWidget(parent)
    {
    PushButtonAnimated *pb = new PushButtonAnimated(this);
    pb->setIcon( QIcon("icon.png") );
    }

    void MyGLWidget::initializeGL()
    {
    // Set up the rendering context, define display lists etc.:
    qglClearColor( Qt::black );
    // Setting the Hidden Surface Removal process
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    }

    void MyGLWidget::resizeGL(int w, int h)
    {
    // setup viewport, projection etc.:
    glViewport(0, 0, (GLint)w, (GLint)h);
    }

    void MyGLWidget::paintGL()
    {
    // draw the scene:
    // ...
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }@

    @// this is the main
    int main( int argc, char *argv[] )
    {
    QApplication appMain(argc, argv);

    // build interface
    QDialog *dg = new QDialog();

    PushButtonAnimated *pb = new PushButtonAnimated(dg);
    pb->setIcon( QIcon(":/test/icon.png") );
    QSpacerItem *spacer = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);

    QGridLayout *layout = new QGridLayout(dg);
    layout->addItem(spacer, 1, 0);
    layout->addWidget(pb, 1, 1);

    bool showOgl = false;
    // bool showOgl = true;
    if( showOgl )
    {
    // IF THIS CODE IS ENABLED THEN
    // THE BUTTON ANIMATION IS NO MORE WORKING
    MyGLWidget *glWidget = new MyGLWidget(dg);
    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    glWidget->setSizePolicy(sizePolicy);
    layout->addWidget(glWidget, 0, 0);
    }

    dg->show();
    dg->resize(500, 500);

    return appMain.exec();
    }@

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

    Thanks in advance for your help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.