How to use QCoreApplication :: postEvent to inject synthetic input events

I introduce keyboard and mouse events that are sent over the network to my Qt application and use QCoreApplication::postEvent for this. The mouse coordinates are the absolute coordinates of the screen pixel.

 QMouseEvent *event = new QMouseEvent(type, QPoint(x, y), mouse_button, mouse_buttons, Qt::NoModifier); QCoreApplication::postEvent(g_qtdraw.main.widget, event); 

Initially, I only had one widget (referenced by g_qtdraw.main.widget ), so I just used it as a receiver argument for postEvent . Now my application has more than one widget, and the above code no longer does what I want.

The second widget is displayed in full screen mode, and I know that all mouse events should go to this window, but with the above code, they are still routed to the main widget.

How to choose the correct widget as the receiver (the one under the x, y coords mouse)? Is there a standard way so Qt selects the correct widget or do I need to track this myself?

change

Now I use the following, which works great (Thanks a lot to 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); } 
+5
source share
2 answers

Can you use QApplication::widgetAt() to find the correct widget at this position and then send to it?

 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 would not expect you to have to do this for key events. They should be sent to the focused widget ( QApplication::focusWidget() ).

Unfortunately, I have not tested anything.

+8
source

I would suggest posting some code in accordance with the documentation :

 void QCoreApplication::postEvent ( QObject * receiver, QEvent * event ) [static] 

Have you tried to point to the corresponding QObject as the receiver argument?

( edit: note that QWidget inherits QObject )

+3
source

Source: https://habr.com/ru/post/908248/


All Articles