PDA

View Full Version : Use QImage/QPixmap as source for HTML img elements in QWebEngine



Jakob
13th November 2015, 14:01
Hi.

I have a QWebEngineView loading a html from Qt resources which works fine. I can also add images to the resources and use them as source for my <img> elements in the web page. My problem is that I can't dynamically add images to the webpage at runtime without using a horrible hack (see: http://stackoverflow.com/questions/6598554/is-there-any-way-to-insert-qpixmap-object-in-html).

The images I want to load are on the local system, so my first thought was to reference them using the file:// protocol, but this is not allowed.

My second thought was to add images to the Qt resource system at runtime, but that doesen't seem to be possible either.

Is there really no nice way to add new images to QWebEngine web pages?

Best regards
Jakob Simon-Gaarde

anda_skoa
13th November 2015, 14:41
Have you tried enabling QWebEngineSettings::LocalContentCanAccessFileUrls?

If that does not work, you could try this:

- create a temporary directory using QTemporaryDir
- extract the assets from the resource into that dir
- then load the local file

Since everything is local files, it should allow to reference other local files.
In the worst case you need to copy the additional images to the same temporary directory.

Cheers,
_

Jakob
13th November 2015, 15:01
Thanks for the reply.

I forgot to say that my main html-document is loaded from qrc which seems to be what is blocking the access to the local filesystem.

I guess I'm forced to load my html from the filesystem then, or what?

Cheers

anda_skoa
13th November 2015, 16:38
I forgot to say that my main html-document is loaded from qrc which seems to be what is blocking the access to the local filesystem.

No, you already said that.



I guess I'm forced to load my html from the filesystem then, or what?

See my other comment.

Cheers,
_

Jakob
13th November 2015, 18:24
ok, well then using the QWebEngineSettings::LocalContentCanAccessFileUrls attribute was not enough. Actually it seems be enough to extract the main html file to a local tempdir. I can still load other resources via qrc afterwards.

I think it would be nice if it was possible to extend and reduce the resource dictionary runtime so you could access bytearrays using :/ and qrc:/.

Ie:
* QResource::addData(const QString &path, const QByteArray &data);
* QResource::removeData(const QString &path);

Cheers Jakob