[Interest] QGraphicsOpacityEffect animation on QPushButton not working in some cases

Calogero Mauceri mauceri at actgate.com
Mon Sep 3 14:47:50 CEST 2012


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.

Attached is the full example code.

Here is how I create the button


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)
{
Q_UNUSED(e);

if( animation->state() == QAbstractAnimation::Running )
animation->pause();
animation->setDirection(QAbstractAnimation::Forward);

animation->start();
}

void PushButtonAnimated::leaveEvent(QEvent *e)
{
Q_UNUSED(e);

if( animation->state() == QAbstractAnimation::Running )
animation->pause();
animation->setDirection(QAbstractAnimation::Backward);

animation->start();
}

and this is how I create the dialog

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();
}

Does anyone have any hint on how to have both buttons animation working?

Thanks in advance for your help,
Calogero

-- 
Calogero Mauceri
Software Engineer

Applied Coherent Technology Corporation (ACT)
www.actgate.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: testbutton.zip
Type: application/octet-stream
Size: 4143 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120903/e9261449/attachment.obj>


More information about the Interest mailing list