Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to test Drag'and'Drop behavior of a widget with QTest?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • kitekatK Offline
    kitekatK Offline
    kitekat
    wrote on last edited by kitekat
    #1

    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(), item_rect.center(), 300) # mouse moved OK
    QtTest.QTest.mousePress(self.main_wnd.tree_view.viewport(), QtCore.Qt.LeftButton, QtCore.Qt.KeyboardModifiers(), 
                                item_rect.center(), 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(),
                                constant_rect.center(), 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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved