How to take screen captures



  • I would like to use Qt's QWebEngine module to take screen captures from an URL. However, although the documentation section on QWebEnginePage lists QWebEnginePage::print and QWebEnginePage::printToPdf, it doesn't mention any way to save pages to image files.

    Does anyone know if Qt's QWebEngine module can be used to take screen captures of websites?



  • @rmam
    I would not have thought so. Though I could be wrong. :)



  • You need to render the webEngineView:

    QSize size(1024,800);
    view.resize(size);
    QImage image(size, QImage::Format_ARGB32);
    QPainter painter(&image);
     view->render(&painter);  // render the webEngineView
     painter.end();
    

    From my experience, the raw result is not very good, need a blurr effect.



  • @mpergand thanks for the help. I've managed to get a minimal working example to run based on your code snippet.

    However, it appears that this approach involves creating a window. Is it possible to take a screenshot without opening a window?



  • Does anyone have a suggestion?
    I have the very same problem: How do I render a QWebEngineView that has received content via QWebEngineView::setHtml().
    I do not want to show the Widget.

    Snippet:

    QImage img(region_width, region_height, QImage::Format_ARGB32);
    QWebEngineView* webEngineView = new QWebEngineView();
    webEngineView->resize(region_width, region_height);
    webEngineView->setStyleSheet("background:red");
    QWebEnginePage* webEnginePage = webEngineView->page();
    webEnginePage->settings()>setAttribute(QWebEngineSettings::ScreenCaptureEnabled, true);
    webEngineView->setHtml("<p  style=\"background-color:#FFF067;color:green\"> TEXT</p>");
    QPainter p(&img);
    webEngineView->render(&p, QPoint(0, 0), QRegion(regionSize));
    p.end();
    img.save("img.png", "PNG", 80);
    

    The result is always an entirely white image, regardless of the given background color, the <p> does not get rendered.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.