2
我想在完全加載時將頁面內容保存到圖像,但有時候我得到的輸出柵格未完全渲染。QWebPage中的QImage未完全渲染
import sys
import signal
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
def onLoadFinished(result):
if not result:
print "Request failed"
sys.exit(1)
webpage.setViewportSize(webpage.mainFrame().contentsSize())
image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
webpage.mainFrame().render(painter)
painter.end()
if os.path.exists("output.png"):
os.remove("output.png")
image.save("output.png")
sys.exit(0) # quit this application
webpage.mainFrame().load(QUrl("file:///page.html"))
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
sys.exit(app.exec_())
該頁面使用JavaScript(onload函數)獲取谷歌地圖(640x640px)。
Image http://i56.tinypic.com/15ojg3s.png
也許這是因爲圖像在頁面加載完成後使用Ajax加載。這是有用的http://stackoverflow.com/questions/1302874/how-to-know-when-a-web-page-is-loaded-when-using-qtwebkit? – xioxox 2011-03-04 15:17:28