Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to map the position of a QQuickItem to its new parent
Qt 6.11 is out! See what's new in the release blog

How to map the position of a QQuickItem to its new parent

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 721 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Roby BrundleR Offline
    Roby BrundleR Offline
    Roby Brundle
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved