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
Forum Updated to NodeBB v4.3 + New Features

About QMovie : play gif animation only once

Scheduled Pinned Locked Moved Solved General and Desktop
qmoviegifanimation
17 Posts 3 Posters 14.2k Views 2 Watching
  • 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.
  • Petross404_Petros SP Offline
    Petross404_Petros SP Offline
    Petross404_Petros S
    wrote on 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?

    JKSHJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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 :)

      Petross404_Petros SP 1 Reply Last reply
      1
      • mrjjM mrjj

        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 :)

        Petross404_Petros SP Offline
        Petross404_Petros SP Offline
        Petross404_Petros S
        wrote on 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?

        mrjjM 1 Reply Last reply
        0
        • Petross404_Petros SP Petross404_Petros S

          @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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #9

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

          Petross404_Petros SP 1 Reply Last reply
          1
          • mrjjM mrjj

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

            Petross404_Petros SP Offline
            Petross404_Petros SP Offline
            Petross404_Petros S
            wrote on last edited by
            #10

            @mrjj Great, thanks!

            mrjjM 1 Reply Last reply
            0
            • Petross404_Petros SP Petross404_Petros S

              @mrjj Great, thanks!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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.

              Petross404_Petros SP 1 Reply Last reply
              0
              • mrjjM mrjj

                @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.

                Petross404_Petros SP Offline
                Petross404_Petros SP Offline
                Petross404_Petros S
                wrote on 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.

                mrjjM 1 Reply Last reply
                0
                • Petross404_Petros SP Petross404_Petros S

                  @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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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.

                  Petross404_Petros SP 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @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.

                    Petross404_Petros SP Offline
                    Petross404_Petros SP Offline
                    Petross404_Petros S
                    wrote on 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
                    • Petross404_Petros SP Petross404_Petros S

                      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?

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 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

                      Petross404_Petros SP 1 Reply Last reply
                      2
                      • JKSHJ JKSH

                        @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!

                        Petross404_Petros SP Offline
                        Petross404_Petros SP Offline
                        Petross404_Petros S
                        wrote on 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
                        • Petross404_Petros SP Offline
                          Petross404_Petros SP Offline
                          Petross404_Petros S
                          wrote on 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

                          • Login

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