How to get the contentSize of a web page in Qt5.4-QtWebEngine

岁酱吖の 提交于 2019-12-06 04:28:41

Check if the QWebEnginePage::geometryChangeRequested signal does what you want.

Also you display a QWebEnginePage by creating a QWebEngineView (it's a QWidget) and calling QWebEngineView::setPage(yourPage).

I couldn't find any native Qt method for that, but I came up with a workaround: once the page is loaded, I do the following to resize my widget to the web content:

webView->page()->runJavaScript("document.documentElement.scrollWidth;",[=](QVariant result){
int newWidth=result.toInt()+10;
webView->resize(newWidth,webView->height());
});

webView->page()->runJavaScript("document.documentElement.scrollHeight;",[=](QVariant result){
int newHeight=result.toInt();
webView->resize(webView->width(),newHeight);
});

NB: I added a 10px width margin

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