Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Why is it still not possible to save a QML Image to file?

    QML and Qt Quick
    qml qt quick image download image download qml image downl qml image to fi
    2
    3
    2485
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • robolivable
      robolivable last edited by robolivable

      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-qml

      This feature seems trivial but is surprisingly challenging. What is the easiest approach as of Qt 5.5?

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @robolivable last edited by

        @robolivable Now you can use grabToImage.

        157

        1 Reply Last reply Reply Quote 0
        • robolivable
          robolivable last edited by robolivable

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post