简体   繁体   中英

how to run qt main window in loop avoid Qt app.exec() blocking main thread

I'm new to Qt, but need to solve a difficult problem.

Need to run qt main window in loop, in which app.exec() will blocking main function until exit is received. but i want to show and hide the main window in loop based on the events received to hide and show the main window

here is sample code now i'm using example:

void main(int argc, char **argv)
{
  //QT window init
  w.setVisible(true);
  app.exec();
}

Can call time init of qt window and do hide and show in loop?

I need to add while loop, on based on events hide or show main window will show and hide

void main(int argc, char **argv)
{
  //QT window init
  w.setVisible(true);
  app.exec();
  while(1)
 {
  //if show status received 
    if(show)
     w.setVisible(true);
  // if hide window status received 
   else if(hide)
     w.setVisible(false);
 }
}

how to keep app.exe alive all the time to achieve hide/show of window? any other QT api call?

i have gone through this link

Qt's recommended solution for this case is to add a QTimer and set the timeout to zero, which will guarantee that your callback gets called in every loop.

To make your application perform idle processing, ie, executing a special function whenever there are no pending events, use a QTimer with 0 timeout. More advanced idle processing schemes can be achieved using processEvents().

https://doc.qt.io/qt-5/qapplication.html#exec

You could go the brave way to subclass QApplication and copy/paste the source of exec() but I'd strongly recommend against that as the preparation and cleanup code in there can change at any new release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM