Question

I am trying to render html data to qimage or qpicture using QWebPage/QWebFrame without QWebView:

#include <QtWebKitWidgets>

auto htmlData = R"(
<!DOCTYPE html>
<html>
<body>
<p>A quick brown fox jumps over the lazy dog.</p>
</body>
</html>
)";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebPage page;
    auto frame = page.mainFrame();
    frame->setContent(htmlData, "text/html");

    QImage img(500, 500, QImage::Format_ARGB32);
    QPainter p(&img);
    frame->render(&p);
    p.end();
    img.save("html.png");

    return 0;
}

The result image is blank. QWebFrame::print does produce correct PDF file though.

What do I need to do to make the html render properly?

Was it helpful?

Solution

OK. I finally figure it out. I need to call QWebPage::setViewportSize(). I guess it's automatically set if the page is part of QWebView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top