Question

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?

Était-ce utile?

La solution 2

I got it working by implementing heightForWidth in a custom widget which extended QWebView

Autres conseils

According to this post on qtcentre.org, you should be able to correct the behavior by setting the size policy.

Update:

Without modifying the size policies at all, a QWebView in a default QHBoxLayout layout on a QWidget results in a web view that resizes properly within the QWidget at sizes greater than 800x600 (using QtCreator, C++, Windows 7, Qt 4.8.1).

Update 2:

I did some digging and found that this question relates to a previous question you posted that contains the relevant requirements :)

The following code seems to meet those requirements. The only relevant bits are that I changed the horizontal and vertical size policies of the QWebView to "expanding".

MainWindow.cpp:

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

MainWindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QWidget" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>587</width>
    <height>442</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="QLabel" name="label">
     <property name="text">
      <string>TextLabel</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
     </property>
    </widget>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>TextLabel</string>
       </property>
       <property name="alignment">
        <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QWebView" name="webView">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="url">
        <url>
         <string>http://www.google.ca/</string>
        </url>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QWebView</class>
   <extends>QWidget</extends>
   <header>QtWebKit/QWebView</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Update 3: Update 4:

Given the URL provided in the comments. It displays as follows.

With a main window at 640x480:

enter image description here

With a main window at 1024x768:

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top