如何结合自己的消息循环和Qt事件循环? [英] How to combine own message loop and Qt event loop?

查看:113
本文介绍了如何结合自己的消息循环和Qt事件循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 QThread: class MyClass : public QThread 派生的类.在运行方法中,我有我自己的"消息循环:

I have a class derived from QThread: class MyClass : public QThread. In the run method I have "my own" message loop:

run() { 
  // exec(); // while not reached    
  while (_runMessageLoop && ...) {
    hr = CallDispatch(.....);
    if (hr== 0) QThread::msleep(100); 
    // QCoreApplication::processEvents(); // Does not work
  }
}

由于 exec() 没有被执行,我没有 Qt 事件循环.这显然会导致信号/槽无法正常工作.是否有机会将 Qt 和我自己的消息循环结合起来?或者我是否需要频繁触发计时器才能完成我在无限循环中完成的工作?

Since exec() is not executed, I have no Qt event loop. This obviously causes signal / slots not to work correctly. Is there any chance to combine the Qt and my own message loop? Or do I need a frequently firing timer in order to do what I have accomplished in my infinite loop?

推荐答案

Qt-wise"的正确方法是使用计时器并让 Qt 管理事件循环.

The right way "Qt-wise" is to use a timer and let Qt manage the event loop.

如果你需要依赖外部事物,你可以使用QAbstractSocket之类的东西在数据通过外部套接字传入时发送事件,例如.

If you need to depend on external things, you can use things like QAbstractSocket to send events when data comes in over an external socket, eg.

这篇关于如何结合自己的消息循环和Qt事件循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆