How to capture full web page using QtWebEngine?

此生再无相见时 提交于 2021-02-07 10:04:53

问题


I use the following code in a QWebEngineView object to capture a web page to an image:

image=new QImage(w,h, QImage::Format_ARGB32);
painter = new QPainter(image);
render(painter);
image->save(imagefilename);

Unfortunately, it only renders part of the web page that is shown on the widget to the image. How to render the whole page to the image?


回答1:


It is possible to hide webview and resize it to show the whole web page. I'm using PyQt5 (version 5.12), but the idea should apply to C++ Qt as well:

# Create hidden QWebEngineView
view.setAttribute(Qt.WA_DontShowOnScreen)
view.show()
...

# Resize the view and then capture image
def take_screenshot():
    view.grab().save(output_file, b'PNG')

size = view.page().contentsSize().toSize()
view.resize(size)
QTimer.singleShot(1000, take_screenshot)


来源:https://stackoverflow.com/questions/55295666/how-to-capture-full-web-page-using-qtwebengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!