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

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.3k Views 1 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.
  • X Offline
    X Offline
    Xsara
    wrote on 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);
    }
    
    JonBJ 1 Reply Last reply
    0
    • X Xsara

      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);
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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 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 :-(

        JonBJ 1 Reply Last reply
        0
        • X Xsara

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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 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

            JonBJ 1 Reply Last reply
            0
            • X Xsara

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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 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
                0
                • X Xsara

                  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 last edited by Bonnie
                  #8

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

                  X 1 Reply Last reply
                  1
                  • B Bonnie

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

                    X Offline
                    X Offline
                    Xsara
                    wrote on 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

                    • Login

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