Save QQuickView screen to image
-
I am trying to save a QQuickView as an image but with no success. I tried the obvious approach:
@view->renderTarget()->toImage().save(fileName);@
However this crashes, and the reason for this is probably that renderTarget() returns a null pointer.
Am I doing something wrong? Or maybe there is a better way?
-
Try:
@
QImage image = view->grabWindow();
@This function might not work if the window is not visible.
"QQuickWindow::grabWindow()":http://qt-project.org/doc/qt-5.0/qtquick/qquickwindow.html#grabWindow -
I'm not sure if there is a direct way to do this but you can grab the entire window and then cut out the bit you want.
Try this:
@
QQuickWindow *window = item->window();
QImage grabbed = window->grabWindow();
QRectF rf(item->x(), item->y(), item->width(), item->height());
rf = rf.intersected(QRectF(0, 0, grabbed.width(), grabbed.height()));
QImage itemImage = grabbed.copy(rf.toAlignedRect());
@Read more "here":http://qt-project.org/forums/viewthread/18371.
-
Yeah that could work in a limiter range of cases, but it won't work for nested and layered structures. I'd rather be able to get the objects pixmap from the final stage of the framebuffer before it is blended and composed in the scene.
Plus, the grabWindow() method is brutally slow and even in the doc is labeled as a performance problem.
-
Yes that is true. You can get a texture from a "QQuickItem":http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html only if it is a "textureProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html#textureProvider. At a quick glance, there doesn't seem to be anything that exposes an objects' pixmap.