如何使用QCoreApplication::postEvent注入合成输入事件
How to use QCoreApplication::postEvent to inject synthetic input events
我正在注入键盘和鼠标事件,这些事件正在通过网络进入我的Qt应用程序,并使用QCoreApplication::postEvent
为此。鼠标坐标为绝对屏幕像素坐标。
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
引用),所以我简单地使用它作为postEvent
的接收器参数。现在我的应用程序有多个小部件,上面的代码不再做我想要的了。
第二个小部件以全屏模式显示,我知道所有鼠标事件都必须转到这个窗口,但使用上面的代码,它们仍然路由到主小部件。
我如何选择正确的小部件作为接收器(鼠标x,y轴下的那个)?是否有一个标准的方法,让Qt选择正确的小部件,或者我必须自己跟踪这个?
您可以使用QApplication::widgetAt()
在该位置找到正确的小部件,然后发布到该位置吗?
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()
)。
不幸的是,我还没有测试这些
我建议张贴一些代码,根据文档的签名是:
void QCoreApplication::postEvent ( QObject * receiver, QEvent * event ) [static]
你有没有试过给一个指针到相应的QObject
作为receiver
参数?
(注意QWidget
继承QObject
)
下面是问题的答案:
我现在使用下面的工作很好(非常感谢Dusty Campbell):
QPoint pos(x, y); QWidget *receiver = QApplication::widgetAt(pos); if (receiver) { QMouseEvent *event = new QMouseEvent(type, receiver->mapFromGlobal(pos), mouse_button, mouse_buttons, Qt::NoModifier); QCoreApplication::postEvent(receiver, event); }
相关文章:
- 如何在Qt 4.8中阻止/忽略/丢弃早于特定超时的用户输入事件
- 输入验证将数字放入向量中
- 我觉得我放入结构中的输入代码可以压缩,关于如何在保持代码简短的同时保持数据个性化的任何建议?
- 如何获取C++中的输入并将其"split"到列表中?换句话说,取 N 个输入并放入 N 长度的数组中
- 丢失读入的每一行输入中的最后一个令牌
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 有没有办法在使用 GLFW 按下按键后只处理一个输入事件?
- C++多按键输入或按键按下/释放事件
- 将输入事件发送到窗口
- 跟踪鼠标输入事件
- 处理自定义QT小部件中的输入事件
- 如何处理 OpenSceneGraph 键盘输入事件中的区分大小写
- GetSave文件名 / IFileSaveDialog - 用户输入的事件/对文件名更改做出反应?
- 计算输入文件中数组中放入的"things"数
- 是否可以使用 select(2) 来监视 SDL 文本输入事件
- 事件处理程序是Embarcadero C++Builder中的重入程序吗
- ifstream 代码不会将输入放入变量中
- SIEM通过TCP流式传输,将多条消息输入一个事件中
- SFML 2实时文本输入?(无事件循环)
- 游戏键盘输入和事件