1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.