I am working on a spesific case about taking images from a running QML application.
I do that by rendering QQuickItems(which is exposed from qml) on a off-screen window to grab their images.
Exposing & passing part: (this can be done in main.cpp)
QQuickItem* item = m_engine->rootObjects()[0]->findChild<QQuickItem*>("rectObject");
QmlOffScreenRenderer renderer(item);
If I define a height and a width for a qml item then I can access exposed item sizes from it's width() and height() method:
#In Qml:
Rectangle {
objectName: "rectObject"
width: 100
height: 100
}
#In C++:
m_item->height(); -> gives 100
m_item->width(); -> gives 100
###But...
If I set anchors instead of height property like:
#In Qml:
Rectangle {
objectName: "rectObject"
anchors {
top: parent.top
bottom: parent.bottom
right: parent.left
width: 100
}
#In C++:
m_item->height(); -> gives 0
m_item->width(); -> gives 100
I know that when a QML item exposed to C++, the item's size is not immediately available in C++. This is because the size of a QML item is determined by the layout system, which runs asynchronously. So, how can I wait the exposed Qml item polished(so I can set window size and opengl frame buffer size to it) to render it on my new window to capture an image?
I would be very greatful if you could share your opinion on that :)
Edit
- Removed unneeded code blocks
What I've tried:
- I have called polish() method of QQuickItem to force it to be polished in my off-screen window before rendering process but it does not seem to work, maybe the order the code is wrong somehow.