How to properly size QWebView?



  • 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?



  • Yes, don't. Having a browser that randomly changes size as you move from page to page is a user interface disaster.

    The height of a web page is driven by the width of the available display space. The contentsSize() height is based on rendering HTML with the constraint of the widget's current width. Changing the widget width after the calculation necessarily invalidates the previously calculated height.



  • The thing is that what is being rendered is not the internet, but rather emails which are formatted using html (therefore, there is no navigation). Also the reason that I want to resize the email whenever the size of the window changes.



  • eh. What?
    You want to fix window size to size of webpage? Why?

    You can write something like:

    @
    //...
    QWebView *view;
    QMainWindow *window = new QMainWindow();
    window->setCentralWidget(view);
    window->show();
    @

    Or use some layout:
    @
    //...
    QWidget window = new QWidget();
    QLayout
    layout = new QLayout();
    QWebView* view = new QWebView();

    layout->addWidget(view);
    window->setLayout(layout);
    window->show();
    @

    This will keep your WebView expanding



  • peppy, I want to render an email in the smallest height possible for a given width (the width of the window) so that scroll bars are avoided. The first example does not attempt to limit the height of the widget, and the second does not work because it relies on sizeHint being accurate, which it isnt.



  • The smallest height as possible? The smallest height as possible depends on that current website, how is done, isn't it?



  • yes, the smallest height possible does depend on the html, hence the problem



  • The solution I ended up going with is implementing heightForWidth (see the new question)



  • And what will you app do, if my site is higher than screen of some user?



  • QResizingWebView will be contained by a QScrollArea



  • So you go out of your way to avoid the scroll bars that are naturally provided by QWebView when they are needed, only to wrap the whole thing in a QScrollArea so that you get scroll bars when you need them. Colour me confused.



  • I know this has been a while, but essentially what I wanted to do here was to mix htmlviews and widgets into one scrolling view. Therefore, each element inside the scrolling view has to always be at its full size. Does that make sense?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.