PDA

View Full Version : Extract QImage from QQuickItem



oberlus
6th June 2013, 15:08
Hi,

I would like to process an image loaded into a Flickable QML element in C++ land but I am failing to obtain the original image. So far I'm only seem able to obtain an image of the QQuickWindow
and would liek to know if there is a way to obtain the whole loaded image.

I am calling a Q_INVOKABLE method of a C++ object as follows:



Flickable {
id: content
clip: false
anchors.fill: parent
boundsBehavior: Flickable.StopAtBounds

Loader {
id: contentLoader
anchors.centerIn: parent
}

MouseArea {
anchors.fill: parent
onClicked: {
console.log("[veo] onClicked - capture triggered")
cplusplusobject.process(contentLoader.item) <----------------- call to C++
}
}


In C++ I have:



void
Cplusplusobject::process(const QVariant &v)
{
qDebug() << "[veo]" << __FUNCTION__;
QQuickItem *item = qobject_cast<QQuickItem *>( v.value<QObject*>() );
qDebug() << "Item dimensions:" << item->width() << item->height();

item->dumpObjectInfo();

QQuickWindow *window = item->window();
QImage image = window->grabWindow();
...


The printed item dimensions do correspond to the original image but I fail to see how to turn the QQuickItem into a QImage.

Any suggestions please?

VTiTux
27th December 2013, 09:50
Up
I'm also interested by an answer.

I would like to store an child QML item to an image.
Currently, I use the grabWindow() method. It works, but this isn't what I want. This stores the whole window.

I saw this method, which looks right:
http://stackoverflow.com/questions/11011391/saving-qml-image
But QtDeclarativeItem is obsolete, and QQuickItem hasn't got a Paint() method.
I tried to use the QQuickPaintedItem class, which has got a paint() method, but it is virtual. I don't know how to use it in my case (I'm a newbie).

Jhonesjay
19th September 2014, 20:33
I am also interested in that. Does anyone get it?

wysota
20th September 2014, 08:09
In Qt Quick items are OpenGL primitives. If you want to get them as images, you have to force the item to be rendered on a texture, e.g. using ShaderEffectSource and then use QQuickItem API to identify the texture so that you can download it from the atlas. That is not something straightforward though.

anda_skoa
20th September 2014, 10:42
If the item is fully visible, you could grab a screenshot of the window (QWindow::grabWindow()) and the copy the area of the item into the target image.

Cheers,
_