Automating drag & drop between Qt applications

Last edited on

Overview

It can be useful to automate drag & drop operations between two Qt applications. With Squish at least two slightly different approaches can be used.

Approach #1: Using dragAndDrop()

import os
import sys

def main():
    aut = "addressbook.app"
    path = os.environ["SQUISH_PREFIX"] + "/examples/qt4/addressbook"
    registerAUT(aut, path)

    # Start first application, store application context
    # and move/resize to avoid overlap:
    ctx1 = startApplication('"' + aut + '"')
    win = waitForObject("{type='MainWindow' visible='1'}")
    win.move(50, 30)
    win.setFixedHeight(200)

    # Give some time for move to take place:
    snooze(0.5)

    # Get source object screen coordinates as int values
    # (extracting when application context of second
    # application is active is too late):
    source = waitForObject("{text='New' type='QToolButton' visible='1'}")   
    pos1 = source.mapToGlobal(QPoint(0, 0))
    source_x = pos1.x
    source_y = pos1.x



    # Start second application, store application context
    # and move/resize to avoid overlap:
    ctx2 = startApplication('"' + aut + '"')
    win = waitForObject("{type='MainWindow' visible='1'}")
    win.move(50, 270)
    win.setFixedHeight(200)

    # Give some time for move to take place:
    snooze(0.5)

    # Get target object reference and screen coordinates:
    target = waitForObject("{text='New' type='QToolButton' visible='1'}")   
    pos2 = target.mapToGlobal(QPoint(0, 0))
    target_x = pos2.x
    target_y = pos2.y

    # Perform drag & drop "relative" to target:
    x_offset = -target_x + source_x
    y_offset = -target_y + source_y
    dragAndDrop(target, x_offset + 20, y_offset + 20, target, 20, 20, Qt.CopyAction)

    snooze(3)

def registerAUT(aut, path, squishserver_host="localhost", squishserver_port=3000):
    s = '"' + os.environ["SQUISH_PREFIX"] + '/bin/squishserver"'
    s += ' --host ' + squishserver_host
    s += ' port=' + str(squishserver_port)
    s += ' --config addAUT "' + aut + '" "' + path + '"'
    if sys.platform == "win32" and s.endswith('"'):
        s = '"' + s
    os.system(s)
test.py

Approach #2: Using mousePress(), mouseMove(), mouseRelease()

import os
import sys

def main():
    aut = "addressbook.app"
    path = os.environ["SQUISH_PREFIX"] + "/examples/qt4/addressbook"
    registerAUT(aut, path)

    # Start first application, store application context
    # and move to avoid overlap:
    ctx1 = startApplication('"' + aut + '"')
    win = waitForObject("{type='MainWindow' visible='1'}")
    win.move(50, 30)
    win.setFixedHeight(200)

    # Give some time for move to take place:
    snooze(0.5)

    # Get source object screen coordinates as int values
    # (extracting when application context of second
    # application is active is too late):
    source = waitForObject("{text='New' type='QToolButton' visible='1'}")   
    pos1 = source.mapToGlobal(QPoint(0, 0))
    source_x = pos1.x
    source_y = pos1.x



    # Start second application, store application context
    # and move to avoid overlap:
    ctx2 = startApplication('"' + aut + '"')
    win = waitForObject("{type='MainWindow' visible='1'}")
    win.move(50, 270)
    win.setFixedHeight(200)

    # Give some time for move to take place:
    snooze(0.5)

    # Get target object reference and screen coordinates:
    target = waitForObject("{text='New' type='QToolButton' visible='1'}")   
    pos2 = target.mapToGlobal(QPoint(0, 0))
    target_x = pos2.x
    target_y = pos2.y

    # Perform drag & drop "relative" to target:
    mouseMove(source_x + 20, source_y + 20)
    snooze(1)
    mousePress(source_x + 20, source_y + 20, MouseButton.LeftButton, 0)
    snooze(1)
    mouseMove(target_x + 20, target_y + 20)
    snooze(1)
    mouseRelease(MouseButton.LeftButton)

    snooze(3)

def registerAUT(aut, path, squishserver_host="localhost", squishserver_port=3000):
    s = '"' + os.environ["SQUISH_PREFIX"] + '/bin/squishserver"'
    s += ' --host ' + squishserver_host
    s += ' port=' + str(squishserver_port)
    s += ' --config addAUT "' + aut + '" "' + path + '"'
    if sys.platform == "win32" and s.endswith('"'):
        s = '"' + s
    os.system(s)
test.py

Related information

dragAndDrop (Qt)

mousePress

mouseMove

mouseRelease