PDA

View Full Version : Qt signal handling question



gd
12th November 2009, 15:06
Hi all,

I should implement a very simple form in which I have two buttons: Start and Stop.
Connected to the Start button I have a loop like:

PROCESS_IS_RUNNING=true;

while (i<I_Max && PROCESS_IS_RUNNING)
{
....
}

where PROCESS_IS_RUNNING is a global bool variable.

When the Stop button is clicked the variable PROCESS_IS_RUNNING is set to false value. I was expecting in this way to stop the loop when the Stop button is pressed but this is not the case. I cannot press the Stop button until the process connected to the Start button is completed (when i=I_MAX). I guess this is a signals handling problem under qt but I cannot figure out how to implement the solution. Any suggestion?

Thank you in advance for you attention

Bye

spirit
12th November 2009, 15:18
with this infinite loop you blocks main thread.


while (i<I_Max && PROCESS_IS_RUNNING)
{
....
}

there are two solutions:
1. put qApp->processEvent() in that loop.
2. put this loop in separate thread.
I also suggest you to read this (http://doc.trolltech.com/qq/qq27-responsive-guis.html).

gd
12th November 2009, 16:26
Thanks a lot for your help. Anyway I'm quite new in using Qt and there are still some points not very clear to me.

I have decided to use the processEvents() option. In principle everything is clear to me on how to use it but I have some difficulties to implement it in my code.

I define the QApplication in my main.cpp file:

int main(int argc, char *argv[])
{
QApplication app(argc,argv);

qdialog m;
app.setMainWidget(&m);
m.show();
return app.exec();

}

I have the loop which I want to stop in a slot of qdialog class. Of course app is not defined in the qdialog class and so I cannot use something like app.processEvents() in my loop. On the other hand if I define app in qdialog class I cannot pass to it the command line options.
I'm a little bit confused! Where and in which way should I define app?

Bye

spirit
12th November 2009, 16:36
if you carefully read documentation about QApplication you will see that there is qApp macro which performs access to an application instance.
so, you can use this macro whenever you need.


int main(int argc, char **argv)
{
QApplication app(argc, argv);

MyWidget mw;
mw.show();

return app.exec();
}

void MyWidget::loop()
{
while (m_flag) {
...
qApp->processEvents();
}
}