Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt 4/5 specific screen grab routines #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions python/screen_grab/screen_grab.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from sgtk.platform.qt import QtCore, QtGui
import sgtk

QT_MAJOR = int(QtCore.qVersion().split(".")[0]) # Works for PySide* and PyQt*


class ScreenGrabber(QtGui.QDialog):
"""
Expand Down Expand Up @@ -332,10 +334,26 @@ def get_desktop_pixmap(rect):
:returns: Captured image
:rtype: :class:`~PySide.QtGui.QPixmap`
"""
desktop = QtGui.QApplication.desktop()
return QtGui.QPixmap.grabWindow(
desktop.winId(), rect.x(), rect.y(), rect.width(), rect.height()
)
desktop_id = QtGui.QApplication.desktop().winId()
x_y_w_h = rect.x(), rect.y(), rect.width(), rect.height()

if QT_MAJOR == 5:
screen = QtGui.QApplication.primaryScreen()
try:
pixmap = screen.grabWindow(desktop_id, *x_y_w_h)
except TypeError as error:
if "quintptr" in str(error):
ptr_type = getattr(__builtins__, "long", int) # Py3 safe long
pixmap = screen.grabWindow(ptr_type(desktop_id), *x_y_w_h)
else:
raise
elif QT_MAJOR == 4:
pixmap = QtGui.QPixmap.grabWindow(desktop_id, *x_y_w_h)
else:
message = "Screen capture not implmented for Qt %d"
raise NotImplementedError(message % QT_MAJOR)

return pixmap


# Backwards compatibility, as this used to be a module-level
Expand Down