Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About QMovie : play gif animation only once
QtWS25 Last Chance

About QMovie : play gif animation only once

Scheduled Pinned Locked Moved Solved General and Desktop
qmoviegifanimation
17 Posts 3 Posters 13.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Petross404_Petros S
    wrote on 25 Feb 2018, 11:10 last edited by Petross404_Petros S
    #1

    I am having a QLabel that I want to display a gif animation before an image. I have trouble making the gif play only once :

    QMovie* movie = new QMovie(":/images/rolling.gif");
    m_ui->label->setMovie(movie);
    movie->start();
    connect(movie, &QMovie::finished, movie, &QMovie::stop);
    
    connect(movie, &QMovie::finished,
                this, [&](){
            QPixmap image(":/images/dice.png");
            m_ui->label->setPixmap(image);
            m_ui->label->show();
        });
    //The animation goes on forever
    

    I can also do this (kind of successfully*) with a timer :

    QTimer* timer = new QTimer(this);
    QMovie* movie = new QMovie(":/images/rolling.gif");
    m_ui->label->setMovie(movie);
    movie->start();
    connect(timer, &QTimer::timeout, movie, &QMovie::stop);
    connect(timer, &QTimer::timeout, this ,&QtDice::stopped_movie);
    timer->start(1000);
    connect(this, &QtDice::stopped_movie,
            this, [&](){
         QPixmap image(":/images/dice.png");
         m_ui->label->setPixmap(image);
         m_ui->label->show();
        });
    

    *Indeed the QLabel is updated with the image after the animation ends.

    Besides the fact that I want a cleaner (not timer based) solution, when I load other images to QLabel, they are shown for a brief moment and quickly images/dice.png is shown again. Apparently that's because of the whole signal thing that is connected there.

    Have I to disconnect QMovie's signals?

    EDIT : I forgot to mention that this code belongs to QtDice constructor.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Feb 2018, 18:42 last edited by
      #2

      Hi
      The actual GIF animation format controls this.
      It has a key for loop count.
      So you can just edit it and remove the looping.

      1 Reply Last reply
      3
      • P Offline
        P Offline
        Petross404_Petros S
        wrote on 25 Feb 2018, 21:02 last edited by
        #3

        The actual GIF animation format controls this.

        So there isn't something the API could do dynamically inside the application.

        Thank you very much .

        J 1 Reply Last reply 26 Feb 2018, 03:01
        0
        • P Petross404_Petros S
          25 Feb 2018, 21:02

          The actual GIF animation format controls this.

          So there isn't something the API could do dynamically inside the application.

          Thank you very much .

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 26 Feb 2018, 03:01 last edited by
          #4

          @Petross404_Petros-S said in About QMovie : play gif animation only once:

          So there isn't something the API could do dynamically inside the application.

          Not directly.

          But as a workaround, you could try to listen for the frameChanged() signal from QMovie, and stop it yourself when you reach the final frame.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          P 1 Reply Last reply 27 Feb 2018, 07:04
          4
          • J JKSH
            26 Feb 2018, 03:01

            @Petross404_Petros-S said in About QMovie : play gif animation only once:

            So there isn't something the API could do dynamically inside the application.

            Not directly.

            But as a workaround, you could try to listen for the frameChanged() signal from QMovie, and stop it yourself when you reach the final frame.

            P Offline
            P Offline
            Petross404_Petros S
            wrote on 27 Feb 2018, 07:04 last edited by
            #5

            @JKSH said in About QMovie : play gif animation only once:

            @Petross404_Petros-S said in About QMovie : play gif animation only once:

            So there isn't something the API could do dynamically inside the application.

            Not directly.

            But as a workaround, you could try to listen for the frameChanged() signal from QMovie, and stop it yourself when you reach the final frame.

            I guess I have to count the frames in order to stop it at the very last frame. I hope int QMovie::frameCount() const can do the job.

            I will post again when I try this.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Petross404_Petros S
              wrote on 27 Feb 2018, 18:27 last edited by Petross404_Petros S
              #6

              Ok frameChanged signal helped a lot :

              ....
              movie->start();
              ....
              connect(movie, &QMovie::frameChanged, this,
              	[movie]()
              	{
                       //For some reason == movie->frameCount() crashes, so... *
              		if(movie->currentFrameNumber() == (movie->frameCount() - 1))
              		{
              			movie->stop();
              			//Explicity emit finished signal so that label **
              			//can show the image instead of a frozen gif
              			//Also, double check that movie stopped before emiting
              			if (movie->state() == QMovie::NotRunning)
              			{
              				emit movie->finished();
              			}
              		}
              	}
              );
              
              //Once finished is emitted, update the label and
              //the status of m_button and action_Cast_the_dice
              //Also emit dice_stopped_rolling
              connect(movie, &QMovie::finished,
              	this, [&]()
              	{
              		m_ui->label->setPixmap(image);
              		m_ui->label->show();
              		m_ui->m_button->setEnabled(true);
              		m_ui->m_button->setFocus();
              		m_ui->action_Cast_the_dice->setEnabled(true);
              		emit dice_stopped_rolling();
              	}
              );
              //Too big lamdas, I have to write some slots for these...
              

              *frameCount returns 20 for the specific gif file, but for some reason I must stop at frame #19. That's an easy workaround to remember.

              ** What boggled my mind is that QMovie::stop() doesn't emit finished as I expected from the description in Detailed Description.

              ...When the movie is done, QMovie emits finished(). ...

              Again it's not a big deal to emit it my self. That's all for now, thank you very much for your help.

              PS : Should the documentation contain an example or be more specific about the whole looping thing for a QMovie?

              J 1 Reply Last reply 27 Feb 2018, 23:15
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 27 Feb 2018, 18:31 last edited by
                #7

                Hi
                Animated gifs have done that since the beginning of time (almost) but yeah
                would be nice if Docs mentioned that it does honor the loop count in the header, but provides no api to control it :)

                P 1 Reply Last reply 27 Feb 2018, 18:36
                1
                • M mrjj
                  27 Feb 2018, 18:31

                  Hi
                  Animated gifs have done that since the beginning of time (almost) but yeah
                  would be nice if Docs mentioned that it does honor the loop count in the header, but provides no api to control it :)

                  P Offline
                  P Offline
                  Petross404_Petros S
                  wrote on 27 Feb 2018, 18:36 last edited by
                  #8

                  @mrjj If someone wants to propose something for the Documentation, he/she must report at https://bugreports.qt.io/ or there is nothing that can be done?

                  M 1 Reply Last reply 27 Feb 2018, 18:38
                  0
                  • P Petross404_Petros S
                    27 Feb 2018, 18:36

                    @mrjj If someone wants to propose something for the Documentation, he/she must report at https://bugreports.qt.io/ or there is nothing that can be done?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 27 Feb 2018, 18:38 last edited by
                    #9

                    @Petross404_Petros-S
                    That would be the correct place as it also accepts feature requests and
                    documents updates.

                    P 1 Reply Last reply 27 Feb 2018, 18:48
                    1
                    • M mrjj
                      27 Feb 2018, 18:38

                      @Petross404_Petros-S
                      That would be the correct place as it also accepts feature requests and
                      documents updates.

                      P Offline
                      P Offline
                      Petross404_Petros S
                      wrote on 27 Feb 2018, 18:48 last edited by
                      #10

                      @mrjj Great, thanks!

                      M 1 Reply Last reply 27 Feb 2018, 18:50
                      0
                      • P Petross404_Petros S
                        27 Feb 2018, 18:48

                        @mrjj Great, thanks!

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 27 Feb 2018, 18:50 last edited by
                        #11

                        @Petross404_Petros-S
                        Btw, i assumed you need to use various GIFs you cant know before hand
                        as else i would just have suggested to open the GIF in online editor and set loop count to 1
                        and it would only play once.

                        P 1 Reply Last reply 27 Feb 2018, 19:06
                        0
                        • M mrjj
                          27 Feb 2018, 18:50

                          @Petross404_Petros-S
                          Btw, i assumed you need to use various GIFs you cant know before hand
                          as else i would just have suggested to open the GIF in online editor and set loop count to 1
                          and it would only play once.

                          P Offline
                          P Offline
                          Petross404_Petros S
                          wrote on 27 Feb 2018, 19:06 last edited by Petross404_Petros S
                          #12

                          @mrjj

                          i would just have suggested to open the GIF in online editor and set loop count to 1

                          I had done exactly that at https://ezgif.com/, before JKSH wrote the post about frameChanged :)

                          I find it a cleaner solution to control the loop with the Qt API (even with bloated -if that's the word- code), rather that duck the issue with external tools. It's just me, it might be better to do this without all that trouble.

                          M 1 Reply Last reply 27 Feb 2018, 19:10
                          0
                          • P Petross404_Petros S
                            27 Feb 2018, 19:06

                            @mrjj

                            i would just have suggested to open the GIF in online editor and set loop count to 1

                            I had done exactly that at https://ezgif.com/, before JKSH wrote the post about frameChanged :)

                            I find it a cleaner solution to control the loop with the Qt API (even with bloated -if that's the word- code), rather that duck the issue with external tools. It's just me, it might be better to do this without all that trouble.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 27 Feb 2018, 19:10 last edited by
                            #13

                            @Petross404_Petros-S
                            Hehe funny enough thats the site i would have suggested :)
                            Yeah fixing it with code is better than fixing one file.
                            Then if GIF changes it still works.

                            P 1 Reply Last reply 27 Feb 2018, 19:25
                            1
                            • M mrjj
                              27 Feb 2018, 19:10

                              @Petross404_Petros-S
                              Hehe funny enough thats the site i would have suggested :)
                              Yeah fixing it with code is better than fixing one file.
                              Then if GIF changes it still works.

                              P Offline
                              P Offline
                              Petross404_Petros S
                              wrote on 27 Feb 2018, 19:25 last edited by
                              #14

                              @mrjj You wrote exactly what I was thinking but couldn't express. Anyway, I am writing a bug report, I hope for the best.

                              1 Reply Last reply
                              1
                              • P Petross404_Petros S
                                27 Feb 2018, 18:27

                                Ok frameChanged signal helped a lot :

                                ....
                                movie->start();
                                ....
                                connect(movie, &QMovie::frameChanged, this,
                                	[movie]()
                                	{
                                         //For some reason == movie->frameCount() crashes, so... *
                                		if(movie->currentFrameNumber() == (movie->frameCount() - 1))
                                		{
                                			movie->stop();
                                			//Explicity emit finished signal so that label **
                                			//can show the image instead of a frozen gif
                                			//Also, double check that movie stopped before emiting
                                			if (movie->state() == QMovie::NotRunning)
                                			{
                                				emit movie->finished();
                                			}
                                		}
                                	}
                                );
                                
                                //Once finished is emitted, update the label and
                                //the status of m_button and action_Cast_the_dice
                                //Also emit dice_stopped_rolling
                                connect(movie, &QMovie::finished,
                                	this, [&]()
                                	{
                                		m_ui->label->setPixmap(image);
                                		m_ui->label->show();
                                		m_ui->m_button->setEnabled(true);
                                		m_ui->m_button->setFocus();
                                		m_ui->action_Cast_the_dice->setEnabled(true);
                                		emit dice_stopped_rolling();
                                	}
                                );
                                //Too big lamdas, I have to write some slots for these...
                                

                                *frameCount returns 20 for the specific gif file, but for some reason I must stop at frame #19. That's an easy workaround to remember.

                                ** What boggled my mind is that QMovie::stop() doesn't emit finished as I expected from the description in Detailed Description.

                                ...When the movie is done, QMovie emits finished(). ...

                                Again it's not a big deal to emit it my self. That's all for now, thank you very much for your help.

                                PS : Should the documentation contain an example or be more specific about the whole looping thing for a QMovie?

                                J Offline
                                J Offline
                                JKSH
                                Moderators
                                wrote on 27 Feb 2018, 23:15 last edited by JKSH
                                #15

                                @Petross404_Petros-S said in About QMovie : play gif animation only once:

                                *frameCount returns 20 for the specific gif file, but for some reason I must stop at frame #19.

                                If frameCount() == 20, then currentFrameNumber() ranges from 0 to 19. It's like an array: If an array has 20 elements, then its last valid index is 19.

                                Nonetheless, even if you used == movie->frameCount(), I'd expect your movie to just loop forever instead of crash. Check your stack trace; there might be other issues in your application.

                                ** What boggled my mind is that QMovie::stop() doesn't emit finished as I expected from the description in Detailed Description.

                                This sounds like a bug. I verified (with Qt 5.10.0 MSVC 2015 x86) that it does emit stateChanged(QMovie::NotRunning) but doesn't emit finished(). Could you please file a separate bug report for this?

                                Please post the links to both reports (documentation and missing finished() signal) here, thanks!

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                P 1 Reply Last reply 27 Feb 2018, 23:32
                                2
                                • J JKSH
                                  27 Feb 2018, 23:15

                                  @Petross404_Petros-S said in About QMovie : play gif animation only once:

                                  *frameCount returns 20 for the specific gif file, but for some reason I must stop at frame #19.

                                  If frameCount() == 20, then currentFrameNumber() ranges from 0 to 19. It's like an array: If an array has 20 elements, then its last valid index is 19.

                                  Nonetheless, even if you used == movie->frameCount(), I'd expect your movie to just loop forever instead of crash. Check your stack trace; there might be other issues in your application.

                                  ** What boggled my mind is that QMovie::stop() doesn't emit finished as I expected from the description in Detailed Description.

                                  This sounds like a bug. I verified (with Qt 5.10.0 MSVC 2015 x86) that it does emit stateChanged(QMovie::NotRunning) but doesn't emit finished(). Could you please file a separate bug report for this?

                                  Please post the links to both reports (documentation and missing finished() signal) here, thanks!

                                  P Offline
                                  P Offline
                                  Petross404_Petros S
                                  wrote on 27 Feb 2018, 23:32 last edited by Petross404_Petros S
                                  #16

                                  @JKSH Sure I will. I didn't know that not emitting finished is a bug (although I suspected it) and I mentioned it in the documentation bug report too. I will report the missing signal tomorrow morning.

                                  Also, the whole frameCount - 1 thing reminded me the arrays and now you confirmed it. I will retry to crash it and see what you are talking about. Thank you all for the support. :)

                                  1 Reply Last reply
                                  2
                                  • P Offline
                                    P Offline
                                    Petross404_Petros S
                                    wrote on 28 Feb 2018, 11:11 last edited by Petross404_Petros S
                                    #17

                                    Good morning (or whatever in your local time zone is), here is the bug report for the missing signal.

                                    1 Reply Last reply
                                    2

                                    7/17

                                    27 Feb 2018, 18:31

                                    • Login

                                    • Login or register to search.
                                    7 out of 17
                                    • First post
                                      7/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved