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

Issue While deleting a Movie after the Given time



  • I have this error While compiling

    I want to stop and close the movie after the 8 seconds . How to do this ... Any suggestions
    /home/adx-soft1/Desktop/Sankarapandiyan/labelui/mainwindow.cpp:52: error: ‘movie’ is not captured
    movie->deleteLater();
    ^~~~~

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QMovie>
    #include <QTimer>
    #include <QStatusBar>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->labelgif->setVisible(false);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        ui->labelgif->setVisible(true);
              ui->labelgif->setAttribute(Qt::WA_TranslucentBackground);
    
    
    
           QMovie *movie = new QMovie(":/new/prefix1/somegif.gif");
               if (!movie->isValid()) {
               // Something went wrong :(
               return;
               }
    
                ui->labelgif->setMovie(movie);
                ui->labelgif->show();
                movie->start();
    
           QTimer *timer = new QTimer (this);
    
           timer->start(1000);
    
    
           QTimer::singleShot(8000,[this, &timer]{
            movie->deleteLater();
           });
    }
    
    
    


  • @sankarapandiyan hi
    you have to capture movie in the QTimer::singleShot()

    why do you create a new QTimer instance ?

    you can do :

    
        QMovie *movie = new QMovie(":/new/prefix1/somegif.gif");
                if (movie->isValid()) {
                    movie->start();
                }
    
                QObject::connect(movie,&QMovie::destroyed,[](){
                   qDebug()<<"movie deleted";
                });
    
            QTimer::singleShot(3000,[&movie]{
             movie->deleteLater();
            });
    


  • @LeLev Yes , The movie deleted by using delete later using signal slot ,

    But the whole window closed , But i want to close the movie only not this-> delete later ,
    But it closed both the window and movie .



  • alternate text)

    Above image is get by Debugging the code

    Whole window closed by default and shows , The program has unexpectedly finished.



  • move your QMovie *movie to private members


  • Lifetime Qt Champion

    @sankarapandiyan said in Issue While deleting a Movie after the Given time:

    Whole window closed by default and shows

    Your application is crashing.
    You should said that instead of saying that the window is closed because it is not the same.

    It looks like movie was already deleted.
    And do not catch the pointer by reference:

    QTimer::singleShot(8000,[movie]{
             movie->deleteLater();
            });
    

  • Moderators

    @sankarapandiyan Please don't delete your posts after you have received your answer. leave your posts online so that other people can understand the thread.


  • Banned

    This post is deleted!

  • Banned

    This post is deleted!

Log in to reply