PDA

View Full Version : QtWebKit and on-the-fly images



miwarre
18th November 2009, 18:25
Hi,

assuming in my application I have:

* a QWebView widget
* a QImage generated internally (i.e. not stored in any file)

I would like to find a way for the HTML set into the QWebView to access the image. Of course, I could save the image to a temporary file and reference the file in the src attribute of an img tag.

But, as I do not need to keep the image after having displayed it, I wonder if there is a way to access an in-memory Qt image, without passing via a disk file.

I played with the QWebFrame::addToJavaScriptWindowObject() function, but I could not figure out how to tell the img tag to look for its source at the object and not at a file.

Any idea?

Thanks,
M.

GreedyGecko
1st June 2011, 17:36
I have exactly the same problem. Did anyone have a solution?

joyer83
1st June 2011, 19:00
It can be done, but it is not exactly a walk in a park.

The webkit uses QNetworkAccessManager and QNetworkReply internally to download resources like html-pages, images, etc.. from web. What you need to do is to provide your own network reply object to webkit which "downloads" data from the in-memory location.

So, first derive a new class from QNetworkReply and reimplement it so that it returns data from the in-memory location. Also, remember that webkit will expect the returned data to be a HTML-response, with all the correct HTTP-headers included.

Then derive a new class from QNetworkAccessManager and reimplement its method QNetworkReply *createRequest( Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0 ). Inside that factory method you can create and initialize an object from your network reply class and then return it to caller.

Finally, QWebPage has setNetworkAccessManager() method which you can then use to pass your own network access manager to it.

joyer83
10th June 2011, 10:02
Ooh, I just stumbled upon this article in Qt Qaurterly (http://doc.qt.nokia.com/qq/32/qq32-webkit-protocols.html), which describes pretty well of how to do implement this.

wysota
11th June 2011, 08:23
You can always embed image data in the image url (http://en.wikipedia.org/wiki/Data_URI_scheme).

joyer83
12th June 2011, 15:44
I didn't even know that you can pass binary data in a URI, nice to know :)