[Interest] QImage from ShaderEffectSource

manish sharma 83.manish at gmail.com
Sat Sep 13 16:28:28 CEST 2014


Hi,

I found a way to get the QImage of an QQuickItem, I am aware of
QQuickWindow::grabImage and QQuickItem::grabToImage will be available in
Qt5.4. So far i am getting correct QImage and it seems to work quite well.
I want to know what i am doing is correct.

Flow:-
- create ShaderEffectSource
- on ShaderEffectSource::scheduledUpdateCompleted signal hook to
afterRendering of window
- inside afterRendering get the
ShaderEffectSource::textureProvider::texture and use glGetTexImage to get
an QImage.

Code:-
*C++*
    QuickWindow()
    {
        .....
        m_snapshotItem = root->findChild<QQuickItem*>("snapShotItem");
        connect (m_snapshotItem, SIGNAL(scheduledUpdateCompleted()), this,
SLOT(takeSnapshot()));
     }

private slots:
    void takeSnapshot()
    {
        connect(this, SIGNAL(afterRendering()), this, SLOT(getImage()),
 Qt::DirectConnection);
    }

    void getImage()
    {
        qDebug() << "taking snapshot..!";
        disconnect(this, SIGNAL(afterRendering()), this, SLOT(getImage()));
        QSGTexture *texture = m_snapshotItem->textureProvider()->texture();
        texture->bind();
        GLint internalFormat;
        glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS,
&internalFormat); // get internal format type of GL texture
        QImage img(texture->textureSize(),  QImage::Format_RGBX8888);
        glGetTexImage(GL_TEXTURE_2D, 0, internalFormat, GL_UNSIGNED_BYTE,
img.bits());
        img.save("c:\\temp\\snapshot.bmp");
    }

*QML*
Item {
    anchors.fill: parent

    Rectangle {
        id: rect
        anchors.fill: parent
        color: "green"
    }

    ShaderEffectSource {
        objectName: 'snapShotItem'
        anchors.fill: rect
        live: false
        recursive: false
        sourceItem: rect
    }
}

Regards,
Manish
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140913/a6e2ff17/attachment.html>


More information about the Interest mailing list