How to display a QImage in QWebKit?

2019-08-26 09:55发布

问题:

Is there any way to display a QImage (in memory, not on filesystem) in a QWebFrame without writing the image out to a temporary file?

回答1:

One option might be using the data URI scheme. You basically base64 encode your picture and write the complete data into the URL. For example:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC

WebKit should support these URIs either directly, or as part of a HTML source code, like

<img src="data:image/png;base64,blaBLABLA" />

Be aware: if your image is big, you might be running into some constraints. Usually this used more for small stuff like icons.



回答2:

Damn. Steffen beat me to it! :)

To add, you could convert to base 64 by saving the image to a QBuffer(&byteArray) and then saying byteArray.toBase64()