SHARE
TWEET

PyQt4 ScreenShot.py

a guest Nov 12th, 2010 541 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. from PyQt4 import QtCore
  3. from PyQt4 import QtGui
  4. from PyQt4 import QtWebKit
  5.  
  6. class ScreenShot(QtGui.QWidget):
  7.     renderFinished = QtCore.pyqtSignal(QtGui.QImage)
  8.    
  9.     def __init__(self, *args, **kw):
  10.         QtGui.QWidget.__init__(self, *args, **kw)
  11.         self.web_page = QtWebKit.QWebPage()
  12.         self.web_page.loadFinished.connect(self.takeScreenShot)
  13.         self.main_frame = self.web_page.mainFrame()
  14.         self.main_frame.setScrollBarPolicy(QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff)
  15.         self.main_frame.setScrollBarPolicy(QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff)
  16.  
  17.  
  18.     def render(self, target_url):
  19.         self.web_page.setViewportSize(QtCore.QSize(1024, 768))
  20.         self.main_frame.load(target_url)
  21.  
  22.  
  23.     def takeScreenShot(self, status):
  24.         if status:
  25.             contents_size = self.main_frame.contentsSize()
  26.             viewport_size = QtCore.QSize(contents_size.width() + 32,
  27.                                          contents_size.height() + 32)
  28.  
  29.             self.web_page.setViewportSize(viewport_size)
  30.  
  31.             offscreen_image = QtGui.QImage(viewport_size, QtGui.QImage.Format_RGB32)
  32.             painter = QtGui.QPainter(offscreen_image)
  33.  
  34.             self.main_frame.render(painter)
  35.             painter.end()
  36.  
  37.             self.renderFinished.emit(offscreen_image)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top