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 . -
)
Above image is get by Debugging the code
Whole window closed by default and shows , The program has unexpectedly finished.
-
@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(); });
-
@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.
-
This post is deleted!
-
This post is deleted!