Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

c++ - How to use QCoreApplication::postEvent to inject synthetic input events

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);

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

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()).

Unfortunately, I haven't tested any of this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...