0

I have used QFuture to start a thread for a function that I already had in my code that was taking a long time and freezing my GUI.

The thread seems to be working ok:

QFuture<int> result = QtConcurrent::run(&m_DC, &DatabaseController::getAll);

however when the getAll function ends I think this thread is still running which it shouldn't be, but I don't know how to end or finish it. I've looked at the documentation but it doesn't really make it clear.

Basically I'm showing a progress bar as busy when the thread starts and want to hide it when it ends

if(result.isRunning())
{
    ui->progressBar->setRange(0,0);
    ui->progressBar->show();
}

if(result.isFinished())
{
    ui->progressBar->hide();
}
bash.d
  • 13,029
  • 3
  • 29
  • 42
AngryDuck
  • 4,358
  • 13
  • 57
  • 91

1 Answers1

0

Using QFutureWatcher to monitor the QFuture returned by QtConcurrent::Run, connect QFutureWatcher::finished to a slot to wrapped the display of your progressBar. Something like

connect(&job_watcher_,
        SIGNAL(finished()),
        SLOT(endLengthyJob()));

MainWindow.cpp

void MainWindow::endLengthyJob()
{
    ui->progress_bar->hide();
} // end_slot(MainWindow::endLengthyJob)

A side note: Thread spawned by QtConcurrent::Run cannot be terminated.

YamHon.CHAN
  • 866
  • 4
  • 20
  • 36
  • it cant be terminated at all? seems pretty poor if you ask me. this example requires me to have a pointless function though thats called at the end of my other function right? surely i can just have it know when the function is ended somehow? – AngryDuck Apr 23 '13 at 09:15
  • I once faced similar problem [link](http://stackoverflow.com/questions/15377236/qt-terminate-thread-spawn-by-qconcurrentrun), check the answer section – YamHon.CHAN Apr 23 '13 at 09:23
  • just spent some time sorting this code for my application and got it all sorted connect a signal to a slot that hide the bar all working now thanks – AngryDuck Apr 23 '13 at 09:40