来自QWebPage的QImage未完全呈现

时间:2011-02-10 18:25:45

标签: javascript google-maps pyqt

我希望在完全加载时将页面内容保存到图像,但有时我会得到输出栅格未完全呈现。

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函数)获取谷歌地图(640x640像素)。

Image http://i56.tinypic.com/15ojg3s.png

1 个答案:

答案 0 :(得分:0)

结帐this question。使用QWebPage.mainFrame().evaluateJavaScript()方法添加此javascript代码:

google.maps.event.addListenerOnce(map, 'idle', function(){
    // do something only the first time the map is loaded
});

您可能还会发现QWebFrame::addToJavaScriptWindowObject ( const QString & name, QObject * object )方法很有用。