How to test Drag'and'Drop behavior of a widget with QTest?

  • I have a GUI with QTreeView and QGraphicsSceneView.

    The model used by the tree view supports drag events and graphics scene supports drop events.

    I want to simulate user actions with QTest: mousePress, mouseMove, mouseRelease and so on.

    I write this code (pardon my Python):

    item_rect = self.main_wnd.tree_view.visualRect(constant_index) # this is the index of the item I want to drag
    QtTest.QTest.mouseMove(self.main_wnd.tree_view.viewport(),, 300) # mouse moved OK
    QtTest.QTest.mousePress(self.main_wnd.tree_view.viewport(), QtCore.Qt.LeftButton, QtCore.Qt.KeyboardModifiers(), 
                      , 300)  # item got selected, but no drag
    QtTest.QTest.mouseMove(self.main_wnd._scene_views[None].viewport(), QtCore.QPoint(), 300)  # mouse moved OK
    QtTest.QTest.mouseRelease(self.main_wnd.tree_view.viewport(), QtCore.Qt.LeftButton, QtCore.Qt.KeyboardModifiers(),
                      , 300)  # nothing happens, because no dragging started...

    Here my mouse really moves to the tree item rect, selects it -- and moves to the center of the scene without any dragging or dropping.

    Can you tell me, how can I fix it?

  • I don't know what exactly the QTest methods do. I use Googletest myself. For my environment, I have written a working method that simulates a mouse drag. In that method, I send a mouseDown event, then send multiple mouseMove events, a final mouseMove event to the exact location of the mouseUp, and in the end a mouseUp event.
    In between all those events, I call QApplication::processEvents().

    It's important that the position of start and end of drag is sufficiently far apart.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.