Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Render html to image/qpicture



  • I am trying to render html data to qimage or qpicture using QWebPage/QWebFrame without QWebView:
    @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 and I can't figure what else I need to do to make the html render properly. QWebFrame::print does produce correct PDF file though.



  • 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.


Log in to reply