I don't know the first thing about Qt, but I'm trying to be sassy and borrow code from other sources (http://lateral.netmanagers.com.ar/weblog/posts/BB901.html#disqus_thread);)
I have a problem. When I run test () for the first time, everything runs smoothly. However, when I run it a second time, I get nasty segfaults. I suspect the problem is that I am not finishing qt. What should I change about this program so that it works several times? Thanks in advance!
from PyQt4 import QtCore, QtGui, QtWebKit import logging logging.basicConfig(level=logging.DEBUG) class Capturer(object): """A class to capture webpages as images""" def __init__(self, url, filename, app): self.url = url self.app = app self.filename = filename self.saw_initial_layout = False self.saw_document_complete = False def loadFinishedSlot(self): self.saw_document_complete = True if self.saw_initial_layout and self.saw_document_complete: self.doCapture() def initialLayoutSlot(self): self.saw_initial_layout = True if self.saw_initial_layout and self.saw_document_complete: self.doCapture() def capture(self): """Captures url as an image to the file specified""" self.wb = QtWebKit.QWebPage() self.wb.mainFrame().setScrollBarPolicy( QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff) self.wb.mainFrame().setScrollBarPolicy( QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff) self.wb.loadFinished.connect(self.loadFinishedSlot) self.wb.mainFrame().initialLayoutCompleted.connect( self.initialLayoutSlot) logging.debug("Load %s", self.url) self.wb.mainFrame().load(QtCore.QUrl(self.url)) def doCapture(self): logging.debug("Beginning capture") self.wb.setViewportSize(self.wb.mainFrame().contentsSize()) img = QtGui.QImage(self.wb.viewportSize(), QtGui.QImage.Format_ARGB32) painter = QtGui.QPainter(img) self.wb.mainFrame().render(painter) painter.end() img.save(self.filename) self.app.quit() def test(): """Run a simple capture""" app = QtGui.QApplication([]) c = Capturer("http://www.google.com", "google.png", app) c.capture() logging.debug("About to run exec_") app.exec_() DEBUG:root:Load http://www.google.com QObject::connect: Cannot connect (null)::configurationAdded(QNetworkConfiguration) to QNetworkConfigurationManager::configurationAdded(QNetworkConfiguration) QObject::connect: Cannot connect (null)::configurationRemoved(QNetworkConfiguration) to QNetworkConfigurationManager::configurationRemoved(QNetworkConfiguration) QObject::connect: Cannot connect (null)::configurationUpdateComplete() to QNetworkConfigurationManager::updateCompleted() QObject::connect: Cannot connect (null)::onlineStateChanged(bool) to QNetworkConfigurationManager::onlineStateChanged(bool) QObject::connect: Cannot connect (null)::configurationChanged(QNetworkConfiguration) to QNetworkConfigurationManager::configurationChanged(QNetworkConfiguration) Process Python segmentation fault (this last line is comes from emacs)
source share