QtWebKit and on-the-fly images
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.
Re: QtWebKit and on-the-fly images
I have exactly the same problem. Did anyone have a solution?
Re: QtWebKit and on-the-fly images
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.
Re: QtWebKit and on-the-fly images
Ooh, I just stumbled upon this article in Qt Qaurterly, which describes pretty well of how to do implement this.
Re: QtWebKit and on-the-fly images
You can always embed image data in the image url (http://en.wikipedia.org/wiki/Data_URI_scheme).
Re: QtWebKit and on-the-fly images
I didn't even know that you can pass binary data in a URI, nice to know :)