How to get pictures from QWebView
Asked Answered
U

1

6

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.

Unmoral answered 26/4, 2012 at 14:43 Comment(2)
What do you mean with "get a picture"? Getting a screenshot of the page? A screenshot of a portion of the page? Downloading an image contained in the DOM?Lyrate
I want to get an image which is in the view. For example, the image of <img src="path" /> This image without redownloading it, just from the view. If it don't work, then screenshot of a portion will do.Unmoral
L
11

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");
Lyrate answered 27/4, 2012 at 9:1 Comment(1)
el.geometry().width(), el.geometry().height() could be replaced with el.geometry().size()Bangweulu

© 2022 - 2024 — McMap. All rights reserved.