Windows消息循环而不是QApplication :: exec()/ QApplication :: processEvents() [英] Windows message loop instead of QApplication::exec() / QApplication::processEvents()

查看:1015
本文介绍了Windows消息循环而不是QApplication :: exec()/ QApplication :: processEvents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我用标准的Windows消息循环实现替换 QApplication :: exec(),我会错过任何 Qt ?这应该澄清我的意思:

Do I miss any Qt functionality if I substitute QApplication::exec() with standard Windows message loop implementation? This should clarify what I mean:

使用Qt方式运行事件处理:

The ususal "Qt" way to run event processing:

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 Window w;
 w.show();
 return a.exec();
}

Windows运行事件处理的方式:

"Windows" way to run event processing:

#include <Windows.h>

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 Window w;
 w.show();

 MSG msg;
 while(GetMessage(&msg, 0, 0, 0)){
    TranslateMessage(&msg);
    DispatchMessage(&msg);
 }

 return msg.wParam;
}

上面演示了对 QApplication 实例,而 QApplication 实例本身甚至没有自己的事件循环。

The above demonstrates having external message loop with respect to QApplication instance, while QApplication instance itself doesn’t even have its own event loop at all.

换句话说,如果我有 main.exe 程序(不知道Qt)与消息循环和 .dll 与Qt GUI和 QApplication 实例里面,将是确定让外部消息循环从 main.exe 来处理Qt GUI的事件?
提前感谢!

In other words, if I have main.exe program (knowing nothing about Qt) with message loop and a .dll with Qt GUI and QApplication instance inside, will it be ok to let the external message loop from main.exe to handle events for Qt GUI? Thanks in advance!

编辑1:
我会回答自己,如果它对某人有用:
我们有一个主要的.exe模块用.NET中的C#编写,它运行事件循环处理,我们有一些用Qt / C ++编写的.dlls,它们有一个GUIinside(和一个QApplication实例共享)。 QApplication :: exec()从未被调用,但所有的事件成功地由主.exe(.NET)模块的事件循环和所有的Qt功能存在(信号/槽,线程等) 。)

EDIT 1: I’ll just answer myself in case it’s usefull for somebody: We have a main .exe module written in C# under .NET that runs event loop processing, and we have a couple of .dlls written in Qt/C++ that have a GUI "inside" (and a QApplication instance that is shared). QApplication::exec() is never called but all the events are successfully dispatched by the main .exe (.NET) module’s event loop and all the Qt functionallity is present( signals/slots, threads, etc.)

编辑2:
这适用于Qt 4.8.2,但对于Qt 5.1.0,事情有点不同。现在您必须调用QApplication :: processEvents()一次,因为它在其第一次调用(在GetMessage或PeekMessage上安装WindowsHook)执行一些初始初始化。之后,在你的应用程序中调用GetMessage的Qt事件获得进程,你是金牌:)

EDIT 2: That worked for Qt 4.8.2 but for Qt 5.1.0 things are a little bit different. Now you have to call QApplication::processEvents() once because it performs some initial initialization on its first call( installs WindowsHook on GetMessage or PeekMessage ). And after that whoever calls GetMessage in your application Qt events get processes and you are golden :)

推荐答案

我的想法是,跨线程调用槽不会工作,因为Qt事件循环执行这些调用。

The first thing which comes to my mind is that calling slots across threads won't work because the Qt event loop is executing those calls.

但更重要的问题可能是:你为什么要这样做,特别是因为在qeventdispatcher_win.cpp是基本上做同样的事情?

But the more important question is probably: Why do you want to do it like this especially since in qeventdispatcher_win.cpp is doing essentially the same thing?

这篇关于Windows消息循环而不是QApplication :: exec()/ QApplication :: processEvents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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