Important: Please read the Qt Code of Conduct -

[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: ""

    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);"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 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: ""
    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);"yay.png");

  • Thanks,
    I have solved it by doing the same from c++ side based on the answer here

Log in to reply