Qt5.4-QtWebEngine中如何获取网页的contentSize [英] How to get the contentSize of a web page in Qt5.4-QtWebEngine

查看:82
本文介绍了Qt5.4-QtWebEngine中如何获取网页的contentSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将新的 Qt5.4 与模块 QtWebEngine 一起使用,从我看来,mainFrame() 不再存在.如何获取页面的 contentSize/size 以及如何呈现它?我尝试使用 setView 和 view 但不起作用.

I'm using the new Qt5.4 with the module QtWebEngine and from what I see mainFrame() doesn't exists anymore. How to get the contentSize/size of the page and how to render it now? I tried with the setView and view but doesn't work.

推荐答案

我找不到任何用于此的本地 Qt 方法,但我想出了一个解决方法:页面加载完毕后,我会执行以下操作以将小部件的大小调整为 Web 内容:

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);
});

注意:我添加了 10px 宽度的边距

NB: I added a 10px width margin

这篇关于Qt5.4-QtWebEngine中如何获取网页的contentSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆