[Solved] Saving QML image from c++

  • 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();
        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);

    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.

  • Moderators

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

    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.

    void MyObject::getImage(QVariant var)
        QImage img = qvariant_cast<QImage>(var);

  • 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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.