我想为大图像页面拍摄QWebEngineView
的屏幕截图。当在红色背景下运行代码时,将正确呈现红色背景,但图像无法呈现。
如何在截屏之前确保页面完全呈现?
from PySide2 import QtCore, QtWidgets, QtWebEngineWidgets
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
img { max-width:100%; max-height:100%; }
div { height: 270px; width: 540px; }
body { margin: 0; background-color: red; }
</style>
</head>
<body>
<div>
<img src="https://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.png">
</div>
</body>
</html>
"""
class Capture(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super(Capture, self).__init__(parent)
self.loadFinished.connect(self.save)
self.show()
self.resize(540, 270+5)
@QtCore.Slot(bool)
def save(self, isOk):
if not isOk:
print('Error')
return
self.grab().save('page.png', b'PNG')
if __name__ == '__main__':
app = QtWidgets.QApplication()
capture = Capture()
capture.setHtml(html)
app.exec_()
编辑:
C ++中的等效代码段:https://pastebin.com/cH7ZgFDB