Search code examples
c++imageqtqwebview

How to get pictures from QWebView


Does anybody know how to get a picture from qwebview? My situation is, there is no scope to use the image url and then a QNetworkRequest. I just need to 'extract' the image from the QWebview.


Solution

  • First you need to get the QWebElement with the image you want to save - if you don't have it already, a good way to get it is

    QWebElement el = view.page()->mainFrame()->findFirstElement("IMG[src='path/to/img'");
    

    assuming view is the name of your QWebView. Then,

    QImage image(el.geometry().width(), el.geometry().height(), QImage::Format_ARGB32);
    QPainter painter(&image);
    el.render(&painter);
    painter.end();
    image.save("path/to/img.png");