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. How to start an animation gif inside QPushButton when it is clicked on?
QtWS25 Last Chance

How to start an animation gif inside QPushButton when it is clicked on?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.2k 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.
  • X Offline
    X Offline
    Xsara
    wrote on 6 May 2020, 20:13 last edited by
    #1

    Re: How to apply gif to Qpushbutton

    I am a beginner and I am trying to put a QMovie inside a QPushButton, and it works great, but I want that this gif only appears when I clicked the button, how can I do this?

    This is my code:

    void CreatorDialog::onBusy()
    {
        connect(movie_gif, &QMovie::frameChanged, [=]{
        button_create->setDefault(true);
        button_create->setIcon(movie_gif->currentPixmap());
    });
            movie_gif->start();
    }
    
    CreatorDialog::CreatorDialog() {
    	m_contentVBox = new QVBoxLayout ();
    	setLayout(m_contentVBox);
    	
    		m_contentVBox->addStretch ();
    		m_buttonHBox = new QHBoxLayout ();
    		m_contentVBox->addLayout(m_buttonHBox);
    
    //Sara urgente parei aqui
            auto movie_gif = new QMovie(this);
            movie_gif->setFileName(":/images/loading.gif");
    
               m_buttonHBox->addStretch();
                button_create = new QPushButton ("Cancel");
    
                connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onCancelButtonClicked()));
                m_buttonHBox->addWidget (button_create);
                button_create = new QPushButton ("Create");
    
    //            connect(movie_gif, &QMovie::frameChanged, [=]{
    //            button_create->setDefault(true);//Sara urgente
    //            button_create->setIcon(movie_gif->currentPixmap());
    //        });
    //        movie_gif->start();
    
                connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onBusy()));
                connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onCreateButtonClicked()));
                m_buttonHBox->addWidget (button_create);
    }
    
    J 1 Reply Last reply 6 May 2020, 20:21
    0
    • X Xsara
      6 May 2020, 20:13

      Re: How to apply gif to Qpushbutton

      I am a beginner and I am trying to put a QMovie inside a QPushButton, and it works great, but I want that this gif only appears when I clicked the button, how can I do this?

      This is my code:

      void CreatorDialog::onBusy()
      {
          connect(movie_gif, &QMovie::frameChanged, [=]{
          button_create->setDefault(true);
          button_create->setIcon(movie_gif->currentPixmap());
      });
              movie_gif->start();
      }
      
      CreatorDialog::CreatorDialog() {
      	m_contentVBox = new QVBoxLayout ();
      	setLayout(m_contentVBox);
      	
      		m_contentVBox->addStretch ();
      		m_buttonHBox = new QHBoxLayout ();
      		m_contentVBox->addLayout(m_buttonHBox);
      
      //Sara urgente parei aqui
              auto movie_gif = new QMovie(this);
              movie_gif->setFileName(":/images/loading.gif");
      
                 m_buttonHBox->addStretch();
                  button_create = new QPushButton ("Cancel");
      
                  connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onCancelButtonClicked()));
                  m_buttonHBox->addWidget (button_create);
                  button_create = new QPushButton ("Create");
      
      //            connect(movie_gif, &QMovie::frameChanged, [=]{
      //            button_create->setDefault(true);//Sara urgente
      //            button_create->setIcon(movie_gif->currentPixmap());
      //        });
      //        movie_gif->start();
      
                  connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onBusy()));
                  connect(button_create, SIGNAL(clicked(bool)), this, SLOT(onCreateButtonClicked()));
                  m_buttonHBox->addWidget (button_create);
      }
      
      J Offline
      J Offline
      JonB
      wrote on 6 May 2020, 20:21 last edited by JonB 5 Jun 2020, 20:24
      #2

      @Xsara
      You seem to almost have it right. Do the connect(movie_gif, &QMovie::frameChanged once outside of the slot, where you currently have it commented out, and do the movie_gif->start(); but not the connect() inside the slot.

      You presently have 3 connect(button_create, , SIGNAL(clicked(bool)) going on, that needs tidying.

      And if you are a beginner it's worth changing to https://wiki.qt.io/New_Signal_Slot_Syntax for your signals & slots.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        Xsara
        wrote on 6 May 2020, 20:23 last edited by
        #3

        If I uncomment the

        connect(movie_gif, &QMovie::frameChanged, [=]{
        button_create->setDefault(true);//Sara urgente
        button_create->setIcon(movie_gif->currentPixmap());
        });
        

        and let the slot onBusy with the:

        movie_gif->start();
        

        It doesn't start :-(

        J 1 Reply Last reply 6 May 2020, 20:28
        0
        • X Xsara
          6 May 2020, 20:23

          If I uncomment the

          connect(movie_gif, &QMovie::frameChanged, [=]{
          button_create->setDefault(true);//Sara urgente
          button_create->setIcon(movie_gif->currentPixmap());
          });
          

          and let the slot onBusy with the:

          movie_gif->start();
          

          It doesn't start :-(

          J Offline
          J Offline
          JonB
          wrote on 6 May 2020, 20:28 last edited by
          #4

          @Xsara
          I was talking about your approach being close, maybe not your implementation.

          I can see things like there is

          auto movie_gif = new QMovie(this);
          

          so that's a local variable in the constructor, what is the movie_gif in the slot? If that's a member variable it has nothing to do with the one you created with the new. You need to understand this if that is the case.

          1 Reply Last reply
          0
          • X Offline
            X Offline
            Xsara
            wrote on 6 May 2020, 20:41 last edited by
            #5

            my header contains:

            public:
            	CreatorDialog();
                QMovie *movie_gif;
            

            but I don't know how to make movie_gif auto, and without the

            auto movie_gif = new QMovie(this);
            

            it doesn't works

            J 1 Reply Last reply 6 May 2020, 21:13
            0
            • X Xsara
              6 May 2020, 20:41

              my header contains:

              public:
              	CreatorDialog();
                  QMovie *movie_gif;
              

              but I don't know how to make movie_gif auto, and without the

              auto movie_gif = new QMovie(this);
              

              it doesn't works

              J Offline
              J Offline
              JonB
              wrote on 6 May 2020, 21:13 last edited by
              #6

              @Xsara
              You need

              movie_gif = new QMovie(this);
              

              to set the member variable. And it would be worth your while to spend some time reading some C++ basics, for your own productivity.

              1 Reply Last reply
              1
              • X Offline
                X Offline
                Xsara
                wrote on 6 May 2020, 22:16 last edited by
                #7

                I've been searching a solution for many days, readed about everything and I didn't found how can I place the QMovie *movie_gif = new QMovie(this); in the header or how to start the video with onBusy() and not inside the CreatorDialog()

                B 1 Reply Last reply 7 May 2020, 00:05
                0
                • X Xsara
                  6 May 2020, 22:16

                  I've been searching a solution for many days, readed about everything and I didn't found how can I place the QMovie *movie_gif = new QMovie(this); in the header or how to start the video with onBusy() and not inside the CreatorDialog()

                  B Offline
                  B Offline
                  Bonnie
                  wrote on 7 May 2020, 00:05 last edited by Bonnie 5 Jul 2020, 00:10
                  #8

                  @Xsara I think what @JonB means is, you need to delete that auto in your cpp file.

                  X 1 Reply Last reply 7 May 2020, 03:44
                  1
                  • B Bonnie
                    7 May 2020, 00:05

                    @Xsara I think what @JonB means is, you need to delete that auto in your cpp file.

                    X Offline
                    X Offline
                    Xsara
                    wrote on 7 May 2020, 03:44 last edited by
                    #9

                    @Bonnie you are right, that was the problem, now I see that I didn't see the gif working because I have another slot for the same button and this conflicts with the gif and the movie appears after running the onCreateButtonClicked() and I don't know how to click the button, the loading image appears while the onCreateButtonClicked() is called.
                    So I will correct the code here:

                    on header:

                    private:
                        QMovie *movie_gif; 
                    
                    protected slots:
                    	virtual void onCreateButtonClicked () = 0;
                        virtual void onBusy ();
                    

                    on cpp:

                    #include "CreatorDialog.h"
                    #include <QBoxLayout>
                    #include <QPushButton>
                    #include <QMovie>
                    
                    void CreatorDialog::onBusy()
                    {
                            movie_gif->start();
                    }
                    
                    CreatorDialog::CreatorDialog() {
                    	m_contentVBox = new QVBoxLayout ();
                    	setLayout(m_contentVBox);
                    	
                    		m_contentVBox->addStretch ();
                    				
                    		m_buttonHBox = new QHBoxLayout ();
                    		m_contentVBox->addLayout(m_buttonHBox);
                    
                            movie_gif = new QMovie(this);
                            movie_gif->setFileName(":/images/loading.gif");
                    
                            	m_buttonHBox->addStretch();
                                QPushButton *button = new QPushButton ("Cancel");
                    
                                connect(button, SIGNAL(clicked(bool)), this, SLOT(onCancelButtonClicked()));
                                m_buttonHBox->addWidget (button);
                    
                                button = new QPushButton ("Create");
                    
                                connect(movie_gif, &QMovie::frameChanged, [=]{
                                button->setDefault(true);//Sara urgente
                                button->setIcon(movie_gif->currentPixmap());
                            });
                    
                                connect(button, SIGNAL(clicked(bool)), this, SLOT(onBusy()));
                                connect(button, SIGNAL(clicked(bool)), this, SLOT(onCreateButtonClicked()));
                                m_buttonHBox->addWidget (button);
                    }
                    
                    1 Reply Last reply
                    0

                    6/9

                    6 May 2020, 21:13

                    • Login

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