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.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.