5

I have a very simple application using WebEngineView and I just wanted to resize the widget displaying to the contents of the html file. I'm expecting it to be 30 pixels wide. Instead my program prints QSize(0,0) and even worser the widget is not displayed at all.

What I'm doing wrong here?

#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto view = new QWebEngineView;
    QString html = "<html><body><div width=30px>Text</div></body></html>";
    view->setHtml(html);
    auto contentsSize=view->page()->contentsSize().toSize();
    qDebug() << contentsSize;
    view->setFixedSize(contentsSize);
    view->show();
    return app.exec();
}

Putting my QWebEngineView into a dialog still doesn't work:

#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto dialog = new QDialog;
    dialog->setLayout(new QHBoxLayout);
    auto view = new QWebEngineView;
    dialog->layout()->addWidget(view);
    QString html = "<html><body><div width=30px>Text</div></body></html>";
    view->setHtml(html);
    auto contentsSize=view->page()->contentsSize().toSize();
    qDebug() << contentsSize;
    view->setFixedSize(contentsSize);
    dialog->show();
    return app.exec();
}

I also tried to connect to the signal loadFinished, but there is no effect.

#include <QWebEngineView>
#include <QApplication>
#include <QDebug>
#include <QDialog>
#include <QHBoxLayout>
#include <QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    auto dialog = new QDialog;
    dialog->setLayout(new QHBoxLayout);
    auto view = new QWebEngineView;
    dialog->layout()->addWidget(view);
    QString html = "<html><body><div width=30px>Text</div></body></html>";
    view->setHtml(html);
    QObject::connect(view->page(), &QWebEnginePage::loadFinished, [&view](bool b) {
        qDebug() << b;
        auto contentsSize = view->page()->contentsSize().toSize();
        qDebug() << contentsSize;
        view->setFixedSize(contentsSize);
    });
    dialog->show();
    return app.exec();

}

Aleph0
  • 5,816
  • 4
  • 29
  • 80
  • Did you try to put your widget on mainwindow or any other layout? – Simon Feb 06 '18 at 10:40
  • Yes. I did. I put it into a QDialog. I'll try it again. – Aleph0 Feb 06 '18 at 10:51
  • 1
    What about using the resize function, `view->resize(100,100);` – Simon Feb 06 '18 at 11:05
  • I don't know beforehand what kind of content I need to display. In Qt 5.3 the equivalent contentsSize function did just the right thing, but now with Qt 5.10 it is somewhat broken, or I need to use it correctly. – Aleph0 Feb 06 '18 at 11:07
  • Try to use the signal `QWebEnginePage::contentsSizeChanged` – Simon Feb 06 '18 at 12:54
  • This signal is of no help. What I wanted is a function telling me simply the best size for a given HTML content. This was possible with Qt 5.3, this I know for sure. – Aleph0 Feb 06 '18 at 13:39
  • view->resize(parent->size()) works fine – roberto May 05 '20 at 06:23

0 Answers0