Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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);
    }
    


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



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



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



  • 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



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



  • 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()



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



  • @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);
    }
    

Log in to reply