[Python] PySide6 QGuiApplication.primaryScreen().grabWindow() creates null QPixMap

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement a screenshot functionality into my PySide6 application using QGuiApplication.primaryScreen().grabWindow().

    So far I've tried on two systems, both are running Ubuntu 22.04.5, Python 3.10, identical venvs with PySide 6.9.1. On one system the below MWE runs without issues, on the other the screenshot is null and not a QBitmap.

    The only difference I can think of between the computers is the one where it works is my daily driver for development which may have additional things installed while the one where it doesn't work is a fresh install. Could there be a hidden dependency which would affect PySide6's ability to screenshot?

    Any insight is appreciated, I've been trying to crack this for a while now.

    MWE:

    from PySide6.QtGui import QGuiApplication

    if __name__ == '__main__':
    app = QGuiApplication([])
    screen = app.primaryScreen()
    screenshot = screen.grabWindow(0)
    retval = screenshot.save('screenshot.png', 'PNG')

    if not retval:
    print("[PRINT] Failed to save screenshot")

    if screenshot.isNull():
    print("[PRINT] Screenshot is null")
    else:
    print("[PRINT] Screenshot is not null")

    if not screenshot.isQBitmap():
    print("[PRINT] Screenshot is not a QBitmap")
    else:
    print("[PRINT] Screenshot is a QBitmap")

    else:
    print("Screenshot saved as 'screenshot.png'")
    app.quit()

    Continue reading...

Compartilhe esta Página