简体   繁体   English

如何在循环中运行 qt 主 window 避免 Qt app.exec() 阻塞主线程

[英]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.我是 Qt 的新手,但需要解决一个难题。

Need to run qt main window in loop, in which app.exec() will blocking main function until exit is received.需要在循环中运行 qt 主 window ,其中 app.exec() 将阻塞主 function 直到收到退出。 but i want to show and hide the main window in loop based on the events received to hide and show the main window但我想根据收到的事件在循环中显示和隐藏主 window 以隐藏和显示主 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?可以调用 qt window 的时间初始化并在循环中隐藏和显示吗?

I need to add while loop, on based on events hide or show main window will show and hide我需要添加while循环,基于事件隐藏或显示主window将显示和隐藏

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?如何让 app.exe 一直处于活动状态以实现 window 的隐藏/显示? any other QT api call?还有其他 QT api 电话吗?

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.对于这种情况,Qt 推荐的解决方案是添加一个 QTimer 并将超时设置为零,这将保证您的回调在每个循环中都被调用。

To make your application perform idle processing, ie, executing a special function whenever there are no pending events, use a QTimer with 0 timeout.要让您的应用程序执行空闲处理,即在没有未决事件时执行特殊的 function,请使用具有 0 超时的 QTimer。 More advanced idle processing schemes can be achieved using processEvents().使用 processEvents() 可以实现更高级的空闲处理方案。

https://doc.qt.io/qt-5/qapplication.html#exec 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.您可以 go 勇敢地继承 QApplication 并复制/粘贴 exec() 的源代码,但我强烈建议您不要这样做,因为其中的准备和清理代码可以在任何新版本中更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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