我想将网页渲染到屏幕外的图像,但是当我使用以下代码时,输出图像为空白,我无法解决问题
class PageShotter(QWidget):
def __init__(self,url,parent=None):
QWidget.__init__(self,parent)
self.url = url
def shot(self):
self.webview = QtWebEngineWidgets.QWebEngineView(self)
self.webview.load(QtCore.QUrl(self.url))
webpage = self.webview.page()
self.webview.setVisible(True)
self.webview.loadFinished.connect(self.save)
def save(self,finished):
if finished:
size = self.webview.contentsRect()
print(u"width:%d,hight:%d" % (size.width(), size.height()))
img = QtGui.QImage(size.width(), size.height(), QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(img)
self.webview.render(painter,QtCore.QPoint())
painter.end()
filename = 'page.png'
if img.save(filename):
filepath = os.path.join(os.path.dirname(__file__), filename)
print(u"success:%s" % filepath)
else:
print(u"fail")
else:
print("Error")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
shotter = PageShotter(web_site.base_url)
shotter.shot()
app.exec()