PDA

View Full Version : Progress Bar Dialog using threads



dove17
8th April 2010, 12:24
HI,
I am trying to call Progress bar Dialog using thread from my main gui window but the progress bar does not proceed. Here is the code snippet.

From main
************


myGUI::myGUI(QWidget *parent, Qt::WFlags flags)
:QMainWindow(parent, flags),
m_UiIsReadyToQuit(false)
{
ui.setupUi(this);
mProgressBarDialog = NULL;
connect(&thread, SIGNAL(ShowPB()),myPBarDialog,SLOT(PBoutput()));
}

void myGUI::Add_clicked()
{
if(myProBarDialog == NULL) {
myProBarDialog = new proBarDialog(this);
}
myProBarDialog->show();

// call the thread here
thread.render();

updateInstanceList();

// Stop the thread here
if (mProgressBarDialog != NULL)
{
mProgressBarDialog->close();
mProgressBarDialog = NULL;
}
thread.stop();
}

Thread code is like this
*****************

void PBarThread::render()
{
start(HighestPriority);
}


void PBarThread::run()
{
emit ShowPB();
}

and PBarDialog code is like this
******************
pBarDialog::pBarDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
ui.pBar->show();

}

void pBarDialog::PBoutput()
{
ui.pBar->show();
}

In Debug mode I can see that the call goes to ui.pBar->show() (not in PBOutput function). I see a frozen progress dialog, although it is running in separate thread. Why do I see a frozen Progress dialog? Why the slot PBoutput is not called although debug shows that thread has emited ShowPB() signal.

Help,
Dove17

TheJim01
8th April 2010, 20:12
What are the max/min sizes for your progress bar? If you have both set to zero, you'll get a marque-style progress bar. If you have them set to different values, you have to update the value of your progress bar to change it. I think your PBoutput should look more like:

void pBarDialog::PBoutput()
{
ui.pBar->setValue(ui.pBar->value() + 1); // your increments may be different.
}You could also change your signal to provide the increment amount, or even the full value.

dove17
9th April 2010, 07:14
The progress Bar min and max are set to (0,0). The Slot PBoutput() is never getting called although the signal emit ShowPB() is emited. The frozen progress bar which i see is coming from the lines
if(myProBarDialog == NULL) {
myProBarDialog = new proBarDialog(this);
}
myProBarDialog->show();

before the thread is called.

Any ideas , how to make it working ?

Dove17

TheJim01
9th April 2010, 16:46
I can tell you are stripping your code down to bare minimum to put on the forums, and that is fine and quite understandable, but we are missing a lot of valuable information. If you could please include your headers, we can see how you are defining your signals, slots, members, etc., which will help us understand your code better, which will allow us to help you better.

That being said, I just noticed a discrepancy in your code.

myProBarDialog = new proBarDialog(this);as compared to your apparent class name, "pBarDialog". You will need to instantiate with the appropriate class name:
myProBarDialog = new pBarDialog(this);

Aside: I also noticed you are not deleting your "mProgressBarDialog". By setting the pointer to NULL, you are changing where it's pointing, but not de-allocating the memory you grabbed when calling new. The memory leak would be small, but it is still a leak.