Bringing window to foreground (Qt)

Last edited on


When automating multiple applications or a single application with multiple windows it can be useful (or required) to bring one window to the foreground, or to make the non active application active.

The problem with this is that modern operating systems and window managers actively try to prevent that an application makes itself active. And this is something that Squish cannot change.

However, sometimes the following workarounds might work.

IMPORTANT - Additional settings for Linux

On Linux you may have to configure the following:

(German: “Fensterverhalten”, “Fensterverhalten”, “Aktivierung”, “Vorbeugung gegen unerwünschte Aktivierung”.)

Look for the setting “Focus stealing prevention level” and set it to None.

IMPORTANT - Additional settings for Windows

On Windows you might have to configure the ForegroundLockTimeout ( google ForegroundLockTimeout ) setting.

Changing this setting may require a reboot or relogin to take effect.

Updates to Windows may reset this registry key.

Workaround #1: TopLevel API

This is part of the API of Squish and can be used to manipulate Windows. TopLevel Object

Workaround #1: raise() + activateWindow()

In general:

def main()
    o = waitForObject({"type": "MainWindow", "unnamed": 1, "visible": 1, "windowTitle": "Address Book"})
    getattr(o, "raise")()

Also see:

Workaround #2: Create, show and close a QMessageBox

This relies on the side effect that closing a QMessageBox will activate the respective application:

def main()
    # Start first instance:
    ctx1 = startApplication("addressbook")

    # Start second instance:
    ctx2 = startApplication("addressbook")

    # Make first instance the current context in Squish:

    # Try to bring it to foreground:

def mainwindow_to_foreground():
    w = waitForObject({"type": "QMainWindow", "visible": 1})
    mb = QMessageBox(w)
    clickButton(waitForObject({"text": "OK", "type": "QPushButton", "visible": 1}))