Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get QPixmap from Image Item in C++



  • Dear all,
    I want to pass an Image item to a C++ method and from that retrieve as QPixmap the image displayed. Is it possibile ?
    In pseudo-code, what I want to achieve is:
    in QML:
    @
    Image {
    id: image
    // can be remote url, qrc or local file
    source: "path/to/image"
    }

    MouseArea {
    onClicked: CppObject.postImage( image )
    }
    @
    and in the C++ implementation of postImage:
    @
    void CppObject::postImage( QQuickItem* image ) {
    QPixmap pix = image->howToRetrieveTheImageAsQPixmap();
    }
    @

    Thanks,
    Gianluca.


  • Moderators

    Hi,

    See the "QPixmap documentation":http://doc.qt.io/qt-5/qpixmap.html#fromImage



  • [quote author="JKSH" date="1418255486"]Hi,

    See the "QPixmap documentation":http://doc.qt.io/qt-5/qpixmap.html#fromImage[/quote]

    I read the documentation ... but the method you point it's not useful for me. I need to get a QPixmap from a Image (not QImage). Image item is a Qt Quick 2 component. It's different from QImage.


  • Moderators

    [quote author="Gianluca" date="1418282896"]I read the documentation ... but the method you point it's not useful for me. I need to get a QPixmap from a Image (not QImage). Image item is a Qt Quick 2 component. It's different from QImage.[/quote]My apologies! I misread your post and thought you wanted to use QImage.

    I don't know of a way to extract the data from a QML Image object. The closest I can think of is QQuickView::grabWindow(), but that grabs everything visible, not just the image.

    If you don't get any answers here, try asking at the "Interest mailing list":http://lists.qt-project.org/mailman/listinfo/interest -- Qt engineers are active there (you'll need to subscribe first)



  • grabWindow() is quite clumsy, and while at this time there is no way to get a QPixmap from a QML Image, the other way around is possible - you can have a QPixmap inside a QML component which you can modify in C++ and display in QML using a custom image provider.

    Check this out: http://stackoverflow.com/questions/27429371/qml-and-c-image-interoperability



  • [quote author="utcenter" date="1418322791"]
    Check this out: http://stackoverflow.com/questions/27429371/qml-and-c-image-interoperability[/quote]

    I cannot use that method because I'm writing a library, so the functionality that I need will be used by the users of the library not by myself.

    I think that for now the only possible solution is to pass the value of source property instead of passing the whole Item.

    But in this case the question change in:
    How can I create a QPixmap from "image://" and "qrc://" url schemas from C++ ?



  • bq. I cannot use that method because I’m writing a library, so the functionality that I need will be used by the users of the library not by myself.

    You probably can put it into the library, i.e. the plugin, with:

    @void QQmlExtensionPlugin::​initializeEngine(QQmlEngine * engine, const char * uri)@

    Just register the image provider with the engine in that function.

    There is nothing preventing you from creating a pixmap from a file in qrc, but it would be quite limiting only being able to use images packed in the resource file.


Log in to reply