How can I create a properly sizing QWebView?

Posted by chacham15 on Stack Overflow See other posts from Stack Overflow or by chacham15
Published on 2012-08-31T23:58:13Z Indexed on 2012/09/01 3:38 UTC
Read the original article Hit count: 289

Filed under:
|
|
|

I want to make a QWebView appear expanding to the width and height so that ideally it will have no scroll bars. Some websites may have fixed widths that wont allow this, but I am not concerned with those. In any case, I cannot do as I wish because QWebView implements sizeHint as follows:

QSize QWebView::sizeHint() const
{
    return QSize(800, 600); // ####...
}

This is incorrect on a number of levels. 1. It doesnt at all take into account the size of the actual web page. 2. It doesnt take into account that the height and width are related to each other. (To prove #2 think about text in web pages that wraps to the next line.)

As a simple fix I tried to do (where QResizingWebView extends QWebView):

QSize QResizingWebView::sizeHint() const{
    return this->page()->mainFrame()->contentsSize();
}

While this is closer to the result, it also has 2 flaws. 1. It doesnt take into account the relation between the displayed width/height. 2. this->page()->mainFrame()->contentsSize() is inaccurate from what I can tell from my preliminary testing (it has returned heights larger than it should under many cases, although this may be related to #1).

Does anyone have any tips to fix this?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt