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



  • I have 2 QQuickItems like below which I can fetch on C++ side using the QMLEngine like this.

    QQuickItem * quick_item_1 = m_qml_engine->rootObjects()[0]->findChild<QQuickItem*>("quickitem1");
    QQuickItem * quick_item_2 = m_qml_engine->rootObjects()[0]->findChild<QQuickItem*>("quickitem2");
    

    Note: 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_surface by changing their parent to new_parent_surface like this.

    quick_item_1->setParentItem(new_parent_surface);
    quick_item_2->setParentItem(new_parent_surface);
    

    This works fine for the objective of drawing them on a new parent QQuickItem. I get the both quick_item_1 & quick_item_2 drawn on new_parent_surface. Even though new_parent_surface is 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_1 & quick_item_2 is 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.

    quick_item_2->mapToItem(new_parent_surface, quick_item_2->position());
    

    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 QQuickItem on another? Are there other recommended ways to do this?


Log in to reply