Results 1 to 4 of 4

Thread: How to place animated GIF on QGraphicsScene?

  1. #1
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default How to place animated GIF on QGraphicsScene?

    I have QGraphicsScene with QGraphicsItem-s. Now I need add animated images to some items. I created aniGIFs but don't see how to "attach" them to items. There is a way to show animated GIFs via QLabel with QMovie inside. But QLabel requires QWidget at least to define where to draw QLabel. I do not have any QWidget inside scene. QWidget and QGraphicsItem are independent drawing essences. They cannot mix. I tried inherit from both classes but got lots of errors even after MOC compiler.

    How can I solve this? How animated GIF can be better placed on QGraphicsScene? Can this work if I create QWidget inside paint() event of my QGraphicsItem? Or there is any other lightweight way?

  2. #2
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default (SOLVED) Re: How to place animated GIF on QGraphicsScene?

    QMovie should be initialized with animated GIF, then QLabel::setMovie( QMovie ), QGraphicsProxyWidget must be initialized with parent QGraphicsItem then QGraphicsProxyWidget::setWidget( Qlabel). QGraphicsProxyWidget::setPos(X,Y) needed to set position of movie on QGraphicsItem. Finally call of QLabel::setAttribute(Qt::WA_NoSystemBackground) needed to allow transparent parts of GIF be transparent. All works.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: (SOLVED) Re: How to place animated GIF on QGraphicsScene?

    An alternative could be to use QMovie together with a QGraphicsPixmapItem and updating the item whenever the movie indicates a frame change.

    Cheers,
    _

  4. #4
    Join Date
    Apr 2011
    Posts
    31
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to place animated GIF on QGraphicsScene?

    But some problem persits.

    I use QMovie on QLabel and QGraphicsProxyWidget to show animated GIF over QGraphicsItem placed on QGraphicsScene. Almost all works fine except one – at first start I see not entire GIF but only small left part of it. After I drag QGraphicsItem over scene (QDrag is used for it) – then QMovie fully appears. Exactly QDrag carries just pointer to QGraphicsItem object instead of serialized object.

    All my actions I show here:
    Qt Code:
    1. QLabel* aniLabel = new QLabel();
    2. QMovie* aniMovie = new QMovie( ":/new/prefix1/anim-1-color.gif", QByteArray(), this );
    3. QGraphicsProxyWidget* aniWidget = new QGraphicsProxyWidget( this );
    4. aniLabel->setAttribute( Qt::WA_NoSystemBackground );
    5. aniLabel->setMovie( aniMovie );
    6. aniWidget->setWidget( aniLabel );
    To copy to clipboard, switch view to plain text mode 
    these actions I perform in constructor of item class derived from QGraphicsItem. To setup position of QGraphicsProxyWidget on QGraphicsItem and show animation I use following code:

    Qt Code:
    1. aniWidget->setPos( pic.width() - aniLabel->size().width(),
    2. pic.height() - aniLabel->size().height() );
    3. aniLabel->show();
    4. aniMovie->start();
    To copy to clipboard, switch view to plain text mode 
    it is placed in paint(…) event of item class derived from QGraphicsItem.

    My question is – are there some necessary actions to be done with QMovie before show it with QLavel? Or may be this is a problem with QLabel itself?


    Added after 12 minutes:


    I SOLVED!! There is a bug in QMovie implementation. After first setting animated GIF it accepts size of only first frame. To properly initialize image properties from all frames QMovie must be started and stopped right now.

    I did

    Qt Code:
    1. aniMovie->start();
    2. aniMovie->stop();
    To copy to clipboard, switch view to plain text mode 
    right before

    Qt Code:
    1. aniLabel->setMovie( aniMovie );
    To copy to clipboard, switch view to plain text mode 

    and animation now appears from first start.

    Can anybody tell me - how QMovie works in Qt 5.x? Is start/stop needed to display animated GIF properly?
    Last edited by Gourmet; 5th February 2015 at 17:40.

Similar Threads

  1. best way for a own animated loader?
    By janton in forum Newbie
    Replies: 2
    Last Post: 28th February 2012, 19:52
  2. Replies: 3
    Last Post: 21st April 2009, 05:25
  3. Pb with animated gif on Mac os
    By mourad in forum Qt Programming
    Replies: 0
    Last Post: 17th April 2008, 13:38
  4. How to place contextmenu in QGraphicsScene?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 16:04
  5. animated gif's
    By :db:sStrong in forum Newbie
    Replies: 2
    Last Post: 31st May 2006, 15:22

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
  •  
Qt is a trademark of The Qt Company.