[Qt-interest] QEventLoop on heap or stack?

Joshua Grauman jnfo-c at grauman.com
Fri Mar 4 19:50:57 CET 2011


Hello,

I am having some problems with a QEventLoop. I haven't been able to 
reproduce the bug on my computer (where the eventloop won't exit), but 
from a user's description, I think the problem is with my exec() function. 
Looking through the Qt code I see they handle the QEventLoop differently 
than I do. It looks like the results should be the same to me. Can anyone 
tell me if the following two ways of handling the QEventLoop (on heap vs 
stack) should cause any substantial difference?

My way:

   ret=0;
   eventLoop=new QEventLoop();
   show();
   eventLoop->exec();
   delete eventLoop;
   eventLoop=0;
   if(accepted)
     return(ret);
   return(-1);

VS (How I see it in Qt source)

   ret=0;
   QEventLoop el;
   eventLoop=⪙
   show();
   eventLoop->exec();
   eventLoop=0;
   if(accepted)
     return(ret);
   return(-1);

Thanks!

Josh

My code follows:

void myForm::on_pushButton1_clicked(void)
{
   //do stuff
   accepted=1;
   if(eventLoop)
     eventLoop->exit();
   hide();
}

void myForm::on_pushButton2_clicked(void)
{
   accepted=0;
   if(eventLoop)
     eventLoop->exit();
   hide();
}

int myForm::exec(void)
{
   if(eventLoop)
   {
     printf("Recursive problem!\n");
     exit(0);
   }
   ret=0;
   eventLoop=new QEventLoop();
   show();
   eventLoop->exec();
   delete eventLoop;
   eventLoop=0;
   if(accepted)
     return(ret);
   return(-1);
}




More information about the Qt-interest-old mailing list