Simulate drag-drop in Qt

You may think mouse drag-drop simulation is as easy as  mouse click simulation because a drag-drop event is just composed of a mouse press event, some mouse move events, and a mouse release event. Unfortunately, it is proven that simulating the mouse drag-drop action is much more difficult than simulating the mouse click action. The qApp->sendEvent does not work at all, the QTest does not have a mouseDrag function like mouseMove, mousePress, and mouseRelease. If you use mouseMove, mousePress, and mouseRelease to emulate mouse drag, you will see the mouse is moved but the drag never happens. This is probably because the drag event is sent at a lower or the same level as mouseMove, mousePress, and mouseRelease so mouseMove, mousePress, and mouseRelease won’t trigger the drag event. We must resort to a lower level function to trigger the drag event. The native Windows function SendMessage does not work either. At last, we come to the SendInput function which is at the lowest level among these event simulation functions. Using SendInput to send the mouse press, mouse move and the mouse release events, we can see the mouse pointer is moving to the target but the drag-drop won’t happen unless we move the mouse a little manually at the end. This problem is solved by moving the SendInput to another thread. I guess the drag-drop is blocked by SendInput. Note that you should add a little delay(Sleep(10)) between each event to make drag-drop occur.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Comments are closed, but trackbacks and pingbacks are open.