VanDahlen
VanDahlen

Reputation: 103

QWebView: Get size of the content

does anybody know how to retrieve the Size of the content within a QWebView?

I've tried

QWebView v;
...
v.page()->mainFrame()->contentsSize();

but this always returns 640x480, independend of the current Content.

Upvotes: 2

Views: 868

Answers (1)

Ilka
Ilka

Reputation: 90

There are two caveats with the mentioned v.page().mainFrame().contentsSize().

wait until the page has finished loading

The mentioned method only works properly if the content has finished loading. You can not call it directly after a call to v.load(QUrl("https://example.com"));. Instead, connect to the loadFinished(bool) signal and wait for it to be emitted. Even if you set some static HTML with no external references with v.setHtml(const QString&), you still have to wait for the loadFinished(bool) signal to be emitted.

set preferredContentsSize to a smaller value

If your page is smaller than 800x600, then you probably want to set the QWebPage::preferredContentsSize to something small before loading the new page. If you do not do that, the v.page().mainFrame().contentsSize() will always return 800x600. However, the page will not be rendered wider than the specified preferred width but it can be rendered higher/longer than the specified height.

You should decide on a QWebPage::preferredContentsSize before loading the first page. Prepare for heavy rendering bugs when passing a smaller size than the QWebView widget had before to QWebPage::setPreferredContentSize after a previous page has already been rendered. (I am using Qt 5.212.0)

Upvotes: 0

Related Questions