QtQuick 2.0 Image from Canvas

  • Hi guys,

    I'm trying to get an Image item from a Canvas item in QtQuick 2.0.
    The reason why I try this is because I want to provide the Canvas' image as a ShaderEffectSource.

    I thought maybe the method Canvas::getImageData should do the job, but unfortunately I get the error "TypeError: Object QQuickCanvasItem(...) has no method 'getImageData'".

    Is this even a good method to get an Image from a Canvas?
    And if so, am I the only one who is not able to call the method getImageData?

    Thanks in advance!

  • Ok, I figured out why getImageData won't work. It's not a method of the Canvas, but of its context.
    I'm not sure whether I read the Doc wrong (http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-canvas.html#getImageData-method ) or if its maybe really not quite clear.

    Anyways, I still didn't manage to get an Image item out of the Canvas. This does not seem to be the right way.

    Does someone know how to do this?

  • Why do you want to turn it into an Image element? The image element is for loading images from disk.

    Any Item can be converted into a ShaderEffectSource by doing:

    @ShaderEffectSource {
    sourceItem: myItem

    If your myItem is a canvas the shader effect source will contain the contents of the canvas. An even easier way is to set
    @ layer.enabled: true@

    directly on the canvas element. This creates an implicit ShaderEffectSource behind the scenes and replaces the Canvas element with the ShaderEffectSource.

    That being said... Several QQuickItem's (such as Image) implement the QSGTextureProvider interface which mean that they can be directly used as a texture in a ShaderEffect, without going through a ShaderEffectSource. The benefit of this is that it saves the scene graph the cost of rendering the item into a framebuffer object to get a texture for it. As the Canvas element is already a texture once it gets rendered in the scene graph, it should have implemented this interface, but as of today it doesn't.

  • Great, that worked! As usual with Qt/QML way easier than I though ;)
    Thank you!

Log in to reply

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