Maximizing, minimizing, restoring, resizing, positioning Windows (Qt)

Last edited on

def main():
    startApplication("addressbook")

    o = waitForObject({"type": "MainWindow"})

    test.log("Maximizing...")
    o.setWindowState(Qt.WindowMaximized)
    snooze(1)

    test.log("Back to normal...")
    o.setWindowState(Qt.WindowNoState)
    snooze(1)

    test.log("Minimizing...")
    o.setWindowState(Qt.WindowMinimized)
    snooze(1)

    test.log("Back to normal...")
    o.setWindowState(Qt.WindowNoState)

    test.log("Raise and activate after restoring from minimized state...")
    getattr(o, "raise")()
    o.activateWindow()
    snooze(1)

    test.log("Positioning on left screen half...")
    to_screen_half(o)
    snooze(1)

    test.log("Positioning on right screen half...")
    to_screen_half(o, True)
    snooze(1)

def to_screen_half(window_object, to_right_half=False):
    sg = QDesktopWidget().availableGeometry()
    fg = window_object.frameGeometry
    wg = window_object.geometry

    x = sg.x
    if to_right_half:
        x = (sg.width / 2) + sg.x
    y = sg.y

    w = (sg.width - (fg.width - wg.width)) / 2
    h = sg.height - (fg.height - wg.height)

    window_object.move(x, y)
    window_object.resize(w, h)
test.py