Getting Object Screen Position, Location, Coordinates (SWT)

Last edited on


The absolute screen position of an object can be useful to know, for example, to use with the nativeMouseClick() function.

import names
def main():
    # ...

    widget = waitForObject(names.QLineEdit)
    pos = get_swt_screen_pos(widget)

def get_swt_screen_pos(widget, xOffset=0, yOffset=0):
    # widget could actually be a Squish wrapper object,
    # so check if there is an item property with
    # a non-null value, and if so use that instead
    if hasattr(widget, "item") and not isNull(widget.item):
        widget = widget.item.parent
        bounds = widget.item.bounds
        xOffset += bounds.x
        yOffset += bounds.y
    # Handle TreeItem, which does not have toDisplay();
    # calculate based on JTree with offset based on
    # TreeItem bounds:
    if not hasattr(widget, "toDisplay"):
        bounds = widget.bounds
        xOffset += bounds.x
        yOffset += bounds.y
        widget = widget.parent
    return widget.toDisplay(xOffset, yOffset)