PDA

View Full Version : QFutureWatcher doesn't get finished signal



Mobility
19th September 2012, 18:44
Hi,


My program starts new thread for making PDF files having many pictures and other heavy stuff. During creation of those PDFs, I want that the main window to show animation indicating that PDF is on progress and stop the animation once PDF is ready.


Problem is that it seems that my QFutureWatcher never receives the finished signal and go to the pysayta_lataus_animaatio() function. I've used hours and hours trying to resolve what's wrong here but cannot figure it out.


Can someone see what is wrong with this code? Thanks in advance!

mainwindow.h

public slots:

void pysayta_lataus_animaatio();


mainwindow.cpp


void MainWindow::on_pB_yhtio_laskut_clicked() // slot, called when user selected push button for creating PDF
{
TRACE("-> void MainWindow::on_pB_yhtio_laskut_clicked()");

QFutureWatcher lataus_watcher;
connect(&lataus_watcher, SIGNAL(finished()), this, SLOT(pysayta_lataus_animaatio()));
QFuture future = QtConcurrent::run(this, &MainWindow::PDF_laskut_yhtio);
lataus_watcher.setFuture(future);
TRACE("Watcher set.");

QMovie *movie = new QMovie(":kuvat/lataa_pieni.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
processLabel->setFixedSize(50,50);
processLabel->move(430,93);
movie->start();
processLabel->show();
TRACE("animation started");
}

void MainWindow::pysayta_lataus_animaatio()
{
TRACE("-> void MainWindow::pysayta_lataus_animaatio()"); // TRACE writes given QString to a log file.
//TBD: stop the animation
}

ChrisW67
19th September 2012, 21:59
As soon as execution leaves the slot the QFutureWatcher and QFuture objects (line 5 and 7) are destroyed because they go out of scope. Destroying the watcher removes the connection. You need to ensure these object's have a lifetime longer than the process they are watching.

Also, the memory allocation at line 11 is a memory leak... the label does not take ownership of the movie.

Mobility
20th September 2012, 06:10
Thanks for you reply. It started working after moving creation QFutureWatcher and QFuture objects next to #include lines. Thanks again!

wysota
20th September 2012, 09:25
Thanks for you reply. It started working after moving creation QFutureWatcher and QFuture objects next to #include lines. Thanks again!

That's not a good approach. Create the future watcher using the new operator in the function where you had it before. The future object can be created on the stack, since it will be copied to the watcher.