Render QML element as bitmap
-
I'm trying to save a QML element as an image file following the method suggested in "this thread":http://qt-project.org/forums/viewthread/3948
The problem seems to be a cast which isn't occurring or I'm casting to something deprecated.
Can someone point a way of solving the cast or a better method to render a QML element as an image?
@void Capturer::save(QObject *imageObj)
{
if(!imageObj)
{
qDebug() << "imageObj is NULL";
return;
} else {
qDebug() << "imageObj is: " << imageObj;
qDebug() << "imageObj parent is: " << imageObj->parent();
}QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj); if (!item) { qDebug() << "GraphicsObject is NULL"; return; }
}
// Outputs:
//
// imageObj is: QQuickImage(0x102c4dd10)
// imageObj parent is: QQuickColumn(0x102c4d940)
// GraphicsObject is NULL @Thanks!
-
Maybe another suggestion: you can render a QQuickWindow to a custom QOpenGLFramebufferObject, I guess there is a way to save the content of that FBO to an image?
http://qt-project.org/doc/qt-5/qquickwindow.html#setRenderTarget -
I wanted to work with image data (for the first time saving) and than manipulate with it (e.g. with openCV), but still wanted to make gui with QML. But the binding is quiet messy. Is something like this even possible?
I did not find any good tutorial or example to do this. -
Is there anyone has some idea or solution ?
-
I found a simpler solution than my suggestion before, you can use "QQuickWindow::grabWindow()":http://qt-project.org/doc/qt-5/qquickwindow.html#grabWindow to get an QImage of the entire window.
If you want an image of just a specific element inside of that window you can crop the image to that element, positions should be available to do that problematically. That is at least the easiest way i can thin of, since you can't get an image of a single QQuickItem, but the whole window only as far as I know. -
That's a good idea. Do we have to obtain the top QQuickItem To get QQuickWindow ?
-
That depends on how you create your QML application I think.
With the default Qt Quick 2 template in Qt Creator, you can just call the method directly:
@
QtQuick2ApplicationViewer viewer;
QImage img = viewer.grabWindow();
@
because QtQuick2ApplicationViewer inherits from QQuickView and QQuickView from QQuickWindow. :) -
Yes! Maybe I will use some tricks to do that. If I have any problem I will ask you. Thank you so much :)