Why is it still not possible to save a QML Image to file?
-
In reference to the following threads:
https://forum.qt.io/topic/3864/how-to-save-an-image-from-qml-image-element
https://forum.qt.io/topic/17505/saving-qml-image/2
https://forum.qt.io/topic/28006/save-image-from-qmlThis feature seems trivial but is surprisingly challenging. What is the easiest approach as of Qt 5.5?
-
@robolivable Now you can use grabToImage.
-
Okay, so the idea here is to utilize the Item element's grabToImage method to save everything inside the element to a file. Placing the Image inside the Item, and calling the method provides the feature I was looking for. Thanks, @p3c0!
Reference code:
Item { id: "itemInQuestion"; Image { id: myImage; source: "http://www.arbitrarySource.com/arbitraryImage.jpeg" } } // ... MouseArea { id: "someMouseArea"; onClicked: { itemInQuestion.grabToImage(function (result) { result.saveToFile("/arbitrary/file/system/path/myArbitraryImage.jpeg"); }); } }
EDIT: I hadn't realized the grabToImage method is an inherited method in Image. The use of a wrapping Item is unnecessary in this case. I should also note that this method does not save the actual image, but a lower resolution copy, as quoted from the documentation: "This function will render the item to an offscreen surface and copy that surface from the GPU's memory into the CPU's memory, which can be quite costly." Is there a better way of saving the full resolution image to a file without having to request it from it's source a second time? This sort of brings me back to my original question.