如何使用QCoreApplication :: postEvent注入合成输入事件 [英] How to use QCoreApplication::postEvent to inject synthetic input events
问题描述
我正在将通过网络传入的键盘和鼠标事件注入Qt应用程序,并为此使用 QCoreApplication :: postEvent
。鼠标坐标是屏幕的绝对像素坐标。
I'm injecting Keyboard and Mouse events which are comming over the network into my Qt Application and use QCoreApplication::postEvent
for this. The mouse coordinates are absolute screen pixel coordinates.
QMouseEvent *event = new QMouseEvent(type, QPoint(x, y), mouse_button, mouse_buttons,
Qt::NoModifier);
QCoreApplication::postEvent(g_qtdraw.main.widget, event);
最初我只有一个小部件(由 g_qtdraw.main.widget <引用/ code>),因此我只是使用该变量作为
postEvent
的接收器参数。现在我的应用程序有多个小部件,上面的代码不再满足我的要求。
Initially I had just one widget (referenced by g_qtdraw.main.widget
) so I simply used that one as the receiver argument for postEvent
. Now my application has more than one widget and the above code does not do what I want any longer.
第二个小部件以全屏模式显示,我知道所有鼠标事件必须转到此窗口,但是使用上面的代码,它们仍然被路由到主窗口小部件。
A second widget is shown in fullscreen mode and I know that all mouse events have to go to this window but with the above code they are still routed to the main widget.
我如何选择正确的窗口小部件作为接收器(下一个鼠标x,y坐标)?有没有一种标准的方法,以便Qt选择正确的小部件,或者我必须自己跟踪此小部件?
How do I choose the correct widget as the receiver (the one under the mouse x,y coords)? Is there a standard way, so that Qt chooses the right widget or do I have to track this myself?
推荐答案
您可以使用 QApplication :: widgetAt()
在该位置找到正确的小部件,然后发布到该位置?
Can you use QApplication::widgetAt()
to find the correct widget at the position and then post to that?
QPoint pos(x, y);
QMouseEvent *event = new QMouseEvent(type, pos, mouse_button, mouse_buttons, Qt::NoModifier);
QWidget *receiver = QApplication::widgetAt(pos);
QCoreApplication::postEvent(receiver, event);
我不希望您在关键事件中必须这样做。它们应该发送到焦点小部件( QApplication :: focusWidget()
)。
I wouldn't expect that you would have to do this for the key events though. They should be sent to the focused widget (QApplication::focusWidget()
).
不幸的是,我还没有t测试了其中任何一个。
Unfortunately, I haven't tested any of this.
这篇关于如何使用QCoreApplication :: postEvent注入合成输入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!