Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Saving QML image from c++
Forum Update on Monday, May 27th 2025

[Solved] Saving QML image from c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 4.3k Views
  • 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.
  • H Offline
    H Offline
    haris123
    wrote on 25 May 2016, 14:05 last edited by haris123
    #1

    I am trying to display network image using qml and then save this image using c++ code,

    Here is the qml code,

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import com.login 1.0
    Window {
        visible: true
        width : 500
        height: 500
    
         Login{id: login}
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
              //  Qt.quit();
                login.save(image);
            }
        }
    
    
    
        Image {
            id: image
            source: "http://www.test.com/webp/gallery/4.jpg"
        }
    
          
    }
    

    And inside my login class saving image like,

    void Login::save( QQuickItem *item)
    {
        qDebug()<<"width: "<<item->width();
        qDebug()<<"height: "<<item->height();
    
        QQuickWindow *window = item->window();
        QImage image = window->grabWindow();
    
        QPixmap pix = QPixmap::fromImage(image);
        pix.save("C:/Users/haris/Desktop/output.png");
    }
    

    I am getting the correct width and height of the image inside c++ class, but the problem is I cannot find a way to save the image item from QQuickItem.

    Right now I am saving the image by grabing the window, which actually not giving the actual image size on output file, instead giving output file with current qml window size.

    Basically I am following the code here http://stackoverflow.com/questions/11011391/saving-qml-image but it seems QDeclarativeItem is deprecated in Qt5, so I choose QQuickItem where as there is no paint option in QQuickItem.

    P 1 Reply Last reply 25 May 2016, 16:02
    0
    • H haris123
      25 May 2016, 14:05

      I am trying to display network image using qml and then save this image using c++ code,

      Here is the qml code,

      import QtQuick 2.3
      import QtQuick.Window 2.2
      import com.login 1.0
      Window {
          visible: true
          width : 500
          height: 500
      
           Login{id: login}
      
          MouseArea {
              anchors.fill: parent
              onClicked: {
                //  Qt.quit();
                  login.save(image);
              }
          }
      
      
      
          Image {
              id: image
              source: "http://www.test.com/webp/gallery/4.jpg"
          }
      
            
      }
      

      And inside my login class saving image like,

      void Login::save( QQuickItem *item)
      {
          qDebug()<<"width: "<<item->width();
          qDebug()<<"height: "<<item->height();
      
          QQuickWindow *window = item->window();
          QImage image = window->grabWindow();
      
          QPixmap pix = QPixmap::fromImage(image);
          pix.save("C:/Users/haris/Desktop/output.png");
      }
      

      I am getting the correct width and height of the image inside c++ class, but the problem is I cannot find a way to save the image item from QQuickItem.

      Right now I am saving the image by grabing the window, which actually not giving the actual image size on output file, instead giving output file with current qml window size.

      Basically I am following the code here http://stackoverflow.com/questions/11011391/saving-qml-image but it seems QDeclarativeItem is deprecated in Qt5, so I choose QQuickItem where as there is no paint option in QQuickItem.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 25 May 2016, 16:02 last edited by p3c0
      #2

      Hi @haris123 ,
      Instead you can use grabToImage and send this data back to C++.
      For eg.:

      //QML 
      Image {
          id: img
          source: "http://www.test.com/webp/gallery/4.jpg"
      }
      
      img.grabToImage(function(result) {
          obj.getImage(result.image); //obj = class set as context property
      });
      

      getImage returns a QImage which can be passed to C++ for further processing.

      //C++
      void MyObject::getImage(QVariant var)
      {
          QImage img = qvariant_cast<QImage>(var);
          img.save("yay.png");
      }
      
      

      157

      1 Reply Last reply
      1
      • H Offline
        H Offline
        haris123
        wrote on 25 May 2016, 16:49 last edited by
        #3

        Thanks,
        I have solved it by doing the same from c++ side based on the answer here http://stackoverflow.com/questions/37439554/save-qml-image-inside-c/37439848#37439848

        1 Reply Last reply
        0

        1/3

        25 May 2016, 14:05

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved