Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly size QWebView?
Forum Updated to NodeBB v4.3 + New Features

How to properly size QWebView?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 11.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chacham15
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chacham15
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Peppy
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chacham15
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Peppy
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                chacham15
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chacham15
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Peppy
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      chacham15
                      wrote on last edited by
                      #10

                      QResizingWebView will be contained by a QScrollArea

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        ChrisW67
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          chacham15
                          wrote on last edited by
                          #12

                          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?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved