[Qt-interest] Best way to implement "onShow/onHide" animation when GraphicsItem gets added/removed from scene?

Anders Bakken anders.bakken at myriadgroup.com
Mon Jul 26 20:17:54 CEST 2010


On Thu, Jul 22, 2010 at 11:47:39PM +0100, Carl Snellman wrote:
> Hey,
>
> I'm trying to implement an animation to a QGraphicsItem derivate, and
> I'd like the animation to scale the item from 0 to 1 when the item is
> added, and then when removed from scene, it would shrink away (scale
> 1->0). I got the "onShow" animation working fine, but "onHide" (or
> onRemoval) causes me headaches. I tried the following:
> >>>>>>>>>>>>>>>>>>>>>>>>
> QVariant MyObject::itemChange(GraphicsItemChange change, const
> QVariant &value) {
>      if (change == QGraphicsItem::ItemVisibleChange && scene()) {
>          bool visible = value.toBool();
>          QPropertyAnimation * sizeAnimation = new
> QPropertyAnimation(this, "scale");
>          sizeAnimation->setDuration(700);
>          sizeAnimation->setEasingCurve(QEasingCurve::OutBounce);
>          if(visible) {
>              qDebug() << "MyObject::itemChange onShow";
>              setScale(0);
>              sizeAnimation->setEndValue(1); // grow to normal size
>          } else {
>             qDebug() << "MyObject::itemChange onHide";
>             sizeAnimation->setEndValue(0); // shrink away
>          }
>          sizeAnimation->setLoopCount(1);
>          sizeAnimation->start();
>      }
>      return QGraphicsItem::itemChange(change, value);
>  }
> <<<<<<<<<<<<<<<<<<<<<<<<
> As said, onShow anim is okay, but the ItemVisibleChange for hide never
> happens if the object is removed from scene. So I tried the following:
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>
> QVariant MyObject::itemChange(GraphicsItemChange change, const
> QVariant &value) {
>     if (change == QGraphicsItem::ItemSceneChange) {
>         QPropertyAnimation * sizeAnimation = new
> QPropertyAnimation(this, "scale");
>         sizeAnimation->setDuration(700);
>         sizeAnimation->setEasingCurve(QEasingCurve::OutBounce);
>         if( ! value.isNull()) {
>             qDebug() << "MyObject::itemChange onSceneAdd";
>             setScale(0);
>             sizeAnimation->setEndValue(1); // grow to normal size
>         } else {
>            qDebug() << "MyObject::itemChange onSceneRemove";
>            sizeAnimation->setEndValue(0); // shrink away
>         }
>         sizeAnimation->setLoopCount(1);
>         sizeAnimation->start();
>     }
>  }
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> Again, onShow works fine, but when the item is removed from scene, it
> gets the event with value non-null !??!?!
> So looks like that won't work either as an approach.
>
> Would anyone have any ideas how to implement such functionality? There
> is also one more complication: I'd like to have the object
> self-destruct when the animation is finished... Otherwise I'd need to
> react on the anim finished() signal somewhere and delete the object
> then. Any ideas how to implement self-destruct?
>
> Carl

Why don't you just call your own function when removing items from the
scene? AFAIK QGraphics.* will never remove an item from a scene for you
(unless maybe if you're adding it to another scene).

regards

--
Anders Bakken

This message, including attachments, is intended solely for the addressee indicated in this message and is strictly confidential or otherwise privileged. If you are not the intended recipient (or responsible for delivery of the message to such person) : - (1) please immediately (i) notify the sender by reply email and (ii) delete this message and attachments, - (2) any use, copy or dissemination of this transmission is strictly prohibited. If you or your employer does not consent to Internet email messages of this kind, please advise Myriad Group AG by reply e-mail immediately. Opinions, conclusions and other information expressed in this message are not given or endorsed by Myriad Group AG unless otherwise indicated by an authorized representative independent of this message.




More information about the Qt-interest-old mailing list