PDA

View Full Version : QProgressDialog causing crash when used inside of a QThread class



agerlach
1st February 2011, 20:59
All,

I have a class that inherits from QThread. In the run() method I have a for loop that performs my calculations. Because this takes several minutes I would like to use QProgressDialog. I am using it as follows



QProgressDialog progress(tr("Training the system..."),tr("Cancel"),0, numPts,0);
progress.setCancelButton(0);
progress.setWindowModality(Qt::WindowModal);

for(int ii = 0; ii < numPts; ii++)
{
progress.setValue(ii);
...
}
progress.setValue(numPts);
progress.close();

When I run my code I get the following errors:

ASSERT failure in QWidget: "Widgets must be created in the GUI thread.", file kernel\qwidget.cpp, line 1231
ASSERT: "qApp && qApp->thread() == QThread::currentThread()" in file kernel\qapplication_win.cpp, line 929

If I comment out all the lines using progress the code runs fine.

The weird thing is that this is seemingly a function of numPts. When I use a dataset where I need to loop 54877 times (numPts) progress works. When I use a dataset where I need to loop 99116 times (numPts) progress causes the crash above.

I don't even know where to start on debugging this so any help is gratefully appreciated.

Lykurg
1st February 2011, 21:05
You cant use gui classes in a thread! All QWidgets have to stay in the main thread. Therefore create the progress dialog there and update it through signal and slots with the worker thread.

agerlach
1st February 2011, 21:52
Thanks, I'll do that.

I figured that was the case by what the error messages state, but it doesn't make sense on why it works part of the time!