PDA

View Full Version : Holding Program until QWidget is closeed



FreddyKay
6th August 2013, 15:04
Hey Guys!

I am using a QWidget for collecting User Input. Now at a certain point I want the program to wait until the QWidget is closed before continuing. In contrast to the QDialog class the QWidget class does not offer an exec() method. Is there another way I can achieve the "waiting" behaviour?

anda_skoa
6th August 2013, 18:36
Event loop driven programs are always "running", exec() only makes it look like it is not "stopping" at some point.

Instead of writing code "sychronously", e.g. like this



// to one thing

dialog.exec();

// do anoher thing


you can always split the two parts into two methods and let the second part be triggered by a signal, e.g. like this



void MyClass::method1()
{
// to one thing
}

void MyClass::method2()
{
// to another thing
}


The trigger for the execution of the slot method2() is then a signal from the input widget, that it emits when it is satisfied with the input.

Cheers,
_

FreddyKay
7th August 2013, 08:48
First of all, thank you for your suggestion! I also thought of that workaround, I just wondered whether there is another, more elegant way of recreating "exec()" possibly using some Qt methods I wasn't aware of. Anyway thanks alot!

anda_skoa
7th August 2013, 09:09
You can do the same thing QDialog is doing internally, i.e. using a nested event loop.



QEventLoop loop;
connect(myWidget, SIGNAL(mySignal()), &loop, SLOT(quit()));
loop.exec();


As with QDialog::exec() be aware that this just moving event processing, not halting it.

In most cases it is safer to go for an asynchronous approach instead of pseudo-synchronous.
So what you consider a work around now is actually the more elegant solution :)

Cheers,
_

FreddyKay
7th August 2013, 14:37
This seems to be what I was looking for. I will give it a try, though its not as straightforward as the first approach, it might actually save me a bit of work ;) Thanks again!