Calling QApplication::processEvents() inside an OpenMP loop?
Greetings all,
In my QT application, I use OpenMP to parallelize loops.In the loop , I want to update progressbar values of the current QMainWindow. Following code snippet shows how I am trying to do this :
#ifdef OPENMP_ENABLE
#pragma 开发者_StackOverflow中文版omp parallel for
#endif
for (int i = 0; i < endIndex; i++) {
getMainWindow()->setProgress(currProg); //change the value of QProgressBar
QApplication::processEvents(); //update events,widgets
//Do some processing
}
Here getMainWindow()->SetProgress() method simply change the values of the QProgressbar attached to statusbar of the QMainWindow.
When compile and run the application with OpenMP enabled , it gives the following error:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 161975a0. Receiver 'MainWindowClass' (of type 'MainWindow') was created in thread 13d78f8", file kernel\qcoreapplication.cpp, line 348
Any tips how to call QApplication::processEvents() with OpenMP ?
Thanks in advance.
Subclass QThread and put your business logic there and use OpenMP there. Use signals and slots to update the QProgressBar.
You can only call GUI methods in the main GUI thread. When you call QApplication::processEvents outside of it, inside a OpenMP thread, you break this rule.
I suppose the following should also work:
#pragma omp parrallel for
for (int i = 0; i < endIndex; i++)
{
#pragma omp single
{
getMainWindow()->setProgress(currProg); //change the value of QProgressBar
QApplication::processEvents(); //update events,widgets
}
// Do some processing
}
精彩评论