Qt – How to animate widget transparency in Qt4

qtqt4

I need to animate widget transparency.
It seems to me, that I should use QPropertyAnimation. But how could i define widget translucent? Should I use something like this?

Best Answer

There are two sub-answers to your question, from what I've played with at least:

  1. If you want to set animate the opacity of a QWidget toplevel window, you can use QPropertyAnimation from 0 to 1 on the "windowOpacity" value. You might want to check the exact syntax since I'm using PyQt.
  2. If you want to animate the opacity of a button or of a text label, this property won't work and you need to use QGraphicsWidgets. They inherit QGraphicsItem and so, they have opacity. Unfortunately, this means using proxy widgets QGrapgicsProxyWidget to keep normal widgets in sync with their graphics widget counterparts since QGraphicsWidget doesn't inherit QWidget. To find out more, check out the Animation Framework Examples, in particular the State Machine for how to use proxy widgets.

For windowOpacity animations:

QPropertyAnimation animate = new QPropertyAnimation(this, "windowOpacity", this);

animate.setDuration(100); animate.setStartValue(1); animate.setEndValue(0); animate.start();

This will work only on systems that support some kind of Composite extension.

Hope it helps!