0

I have a Button connected on click with myfuntion see below.

I'm using a QEventLoop because I am waiting for an event, wich will call _myEventLoop.exit(0);

When I click the button two times in a row without waiting for the event to happen I get the error message :

QEventLoop::exec: instance 0x22f47378 has already called exec()

I do a call to exit(1) before calling exec() but it seems that this exit returns immediately, then the function passe by exec(), since it is still running return 1, myfunction returns and only then the first exec() return with 1.

How can i be sure that the previous call to exec() has returned before continuing ?

void myfuntion()
{
    _myEventLoop.exit(1);
    if(_myEventLoop.exec() == 0)
    {
        // DoSomething
    }
}
Giacogiac
  • 189
  • 1
  • 11
  • What is the goal here exactly? I don't see what is the point in restarting a local event loop over and over again. – thuga Sep 29 '15 at 14:09
  • My goal was to block the user of the function until some event. And if the function was called by someonde else to release the first user and block the last caller. From what I understood of how the eventLoop work I can't and I completely changed conception. – Giacogiac Sep 30 '15 at 08:02
  • @Giacogiac could you please elaborate your approach that worked? – RDP Apr 11 '17 at 19:38
  • @RDP I didn't manage to block execution. So I use a callback. I wanted to write : `void MyClass::onClick() { if(_myEventManager->pick()) // blocking { // DoSomething } }` What I do : `void MyClass::onClick() { _myEventManager->cancelPick(); // calls previous callback with success == false _myEventManager->requestPick(this, &MyClass::onPick); } void MyClass::onPick(bool success) { if(success) { // DoSomething } } ` – Giacogiac Apr 18 '17 at 11:57

1 Answers1

1

you can call QEventLoop::processEvents to Processes any pending events.
your code will be like:

void myfuntion()
{
    _myEventLoop.exit(1);
    _myEventLoop.processEvents();

    if(_myEventLoop.exec() == 0)
    {
        // DoSomething
    }
}
ahmed
  • 5,430
  • 1
  • 20
  • 36