Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to map the position of a QQuickItem to its new parent
Roby Brundle last edited by
I have 2
QQuickItems like below which I can fetch on C++ side using the
QQuickItem * quick_item_1 = m_qml_engine->rootObjects()->findChild<QQuickItem*>("quickitem1"); QQuickItem * quick_item_2 = m_qml_engine->rootObjects()->findChild<QQuickItem*>("quickitem2");
quick_item_1's immediate parent is different &
quick_item_2's immediate parent is also different. But they both are drawn on the same application window into different immediate parents.
I am drawing both of them offscreen on a different
QQuickItem. Let's call it
new_parent_surface. I draw both these items on
new_parent_surfaceby changing their parent to
This works fine for the objective of drawing them on a new parent QQuickItem. I get the both
new_parent_surface. Even though
new_parent_surfaceis not drawn on UI, but if I take a snapshot using grabToImage of
new_parent_surface, I can see the 2 items drawn on them. Fine till here.
However the positioning of
quick_item_2is not correct. I want to position them similar to the way they were positioned their original parent item. I can do some percentage math & try positioning them the same way as they were drawn on their original parent but isn't there a QQQuickItem or Qt API to translate this positioning to a new parent?
I tried to look into QQuickItem's mapping APIs like mapToItem & trying them out like this.
But the positioning is still not correct.
So, how can I map a QQuickItem's position into its new parent QQuickItem after doing a setParentItem?
Also, is this correct way to draw a
QQuickItemon another? Are there other recommended ways to do this?