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. Is child coordinates (x, y, z) associated with visual parent, or object parent?
Forum Updated to NodeBB v4.3 + New Features

Is child coordinates (x, y, z) associated with visual parent, or object parent?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml parentqml coordinates
4 Posts 3 Posters 1.1k Views 2 Watching
  • 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.
  • lawrence.emkeL Offline
    lawrence.emkeL Offline
    lawrence.emke
    wrote on last edited by lawrence.emke
    #1

    I created a learning project (qmlproject). In it I create a Rectangle with a small
    size (100,100); color: white; with coordinates (10,10); I gave the rectangle an id of "ccb". and the window an id of "win"; I used the default setting in "win"

    Within the "ccb" rectangle I defined two children rectangles of "ccb". I gave
    them each smaller sizes, and colors "yellow" and "red". I then set the coordinates of the yellow rectangle within the white rectangle and the
    red rectangle outside of the white rectangle. All of this acted as I assumed
    it would.

    Now here is the question: I assigned 'parent: "win" ' to each of the smaller
    rectangles. I was assuming that the visual parent would be the "win" window.
    and that the coordinates would be applied to the window geometry, and that
    to locations of the two smaller rectangles would change. But they did not change.

    Why is that? What does a visual parent mean if not that the coordinates are
    related to the visual parent?

    If the coordinate are not associated with the visual parent then what is its purpose?

    import QtQuick 2.12
    import QtQuick.Window 2.12

    Window
    {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    color: "green"
    id: win

    Rectangle
    {
        id: ccb
        x: 87
        y: 190
        width: 100
        height: 100
    
    
        Component.onCompleted:
        {
            console.log("number: " + ccb.children.length )
        }
    
        Rectangle
        {
            id: rect1
            color: "yellow"
            parent: win
            x: 49
            z:1
            width: 30
            height: 30
        }
        Rectangle
        {
            parent: win
            id: rect2
            color: "red"
            x: 200
            z:2
            width: 12
            height: 40
        }
    }
    

    }

    Received two error messages. What are they saying except don't do this?

    file:///home/leemke/proj/qt/item2/item2.qml:30:13: Unable to assign QQuickWindowQmlImpl to QQuickItem
    file:///home/leemke/proj/qt/item2/item2.qml:38:13: Unable to assign QQuickWindowQmlImpl to QQuickItem

    Well, I found out that you can assign a child to a sibling, even when
    the children objects are defined under a different parent. And
    when you do this the number of children (children.length) goes
    to zero. Why can I not go up the parent/child relationship chain?

    And when I assigned to two child rectangles to a different visual parent,
    are the object definitions also moved, or did they remain under
    the defining parent? And if they remained then why did the number
    of children go to zero? How many children are dependent on the
    existence of the defining parent, and how can I access them
    if I want to destroy them?

    sierdzioS 1 Reply Last reply
    0
    • lawrence.emkeL lawrence.emke

      I created a learning project (qmlproject). In it I create a Rectangle with a small
      size (100,100); color: white; with coordinates (10,10); I gave the rectangle an id of "ccb". and the window an id of "win"; I used the default setting in "win"

      Within the "ccb" rectangle I defined two children rectangles of "ccb". I gave
      them each smaller sizes, and colors "yellow" and "red". I then set the coordinates of the yellow rectangle within the white rectangle and the
      red rectangle outside of the white rectangle. All of this acted as I assumed
      it would.

      Now here is the question: I assigned 'parent: "win" ' to each of the smaller
      rectangles. I was assuming that the visual parent would be the "win" window.
      and that the coordinates would be applied to the window geometry, and that
      to locations of the two smaller rectangles would change. But they did not change.

      Why is that? What does a visual parent mean if not that the coordinates are
      related to the visual parent?

      If the coordinate are not associated with the visual parent then what is its purpose?

      import QtQuick 2.12
      import QtQuick.Window 2.12

      Window
      {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
      color: "green"
      id: win

      Rectangle
      {
          id: ccb
          x: 87
          y: 190
          width: 100
          height: 100
      
      
          Component.onCompleted:
          {
              console.log("number: " + ccb.children.length )
          }
      
          Rectangle
          {
              id: rect1
              color: "yellow"
              parent: win
              x: 49
              z:1
              width: 30
              height: 30
          }
          Rectangle
          {
              parent: win
              id: rect2
              color: "red"
              x: 200
              z:2
              width: 12
              height: 40
          }
      }
      

      }

      Received two error messages. What are they saying except don't do this?

      file:///home/leemke/proj/qt/item2/item2.qml:30:13: Unable to assign QQuickWindowQmlImpl to QQuickItem
      file:///home/leemke/proj/qt/item2/item2.qml:38:13: Unable to assign QQuickWindowQmlImpl to QQuickItem

      Well, I found out that you can assign a child to a sibling, even when
      the children objects are defined under a different parent. And
      when you do this the number of children (children.length) goes
      to zero. Why can I not go up the parent/child relationship chain?

      And when I assigned to two child rectangles to a different visual parent,
      are the object definitions also moved, or did they remain under
      the defining parent? And if they remained then why did the number
      of children go to zero? How many children are dependent on the
      existence of the defining parent, and how can I access them
      if I want to destroy them?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @lawrence-emke said in Is child coordinates (x, y, z) associated with visual parent, or object parent?:

      Received two error messages. What are they saying except don't do this?

      The Window element is not a QQuickItem (thus also not Item in QML), so setting visual parent there does not work, that class does not have such concept.

      While I understand this is a test project for learning/ testing, I'd recommend not to mess with parent. It's rarely used in practice (in fact, I have never seen it used in any actual project).

      Regarding positioning: the documentation is clear on this:

      Declaring an item as a child of another does not automatically mean that the child item will be appropriately positioned or sized to fit within its parent. Some QML types may have in-built behaviors that affect the positioning of child items — for example, a Row object automatically re-positions its children into a horizontal formation — but these are behaviors enforced by the types' own specific implementations. Additionally, a parent item will not automatically clip its children to visually contain them within the parent's visual bounds, unless its clip property is set to true.

      source

      (Z(:^

      J.HilkJ 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @lawrence-emke said in Is child coordinates (x, y, z) associated with visual parent, or object parent?:

        Received two error messages. What are they saying except don't do this?

        The Window element is not a QQuickItem (thus also not Item in QML), so setting visual parent there does not work, that class does not have such concept.

        While I understand this is a test project for learning/ testing, I'd recommend not to mess with parent. It's rarely used in practice (in fact, I have never seen it used in any actual project).

        Regarding positioning: the documentation is clear on this:

        Declaring an item as a child of another does not automatically mean that the child item will be appropriately positioned or sized to fit within its parent. Some QML types may have in-built behaviors that affect the positioning of child items — for example, a Row object automatically re-positions its children into a horizontal formation — but these are behaviors enforced by the types' own specific implementations. Additionally, a parent item will not automatically clip its children to visually contain them within the parent's visual bounds, unless its clip property is set to true.

        source

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @sierdzio said in Is child coordinates (x, y, z) associated with visual parent, or object parent?:

        (in fact, I have never seen it used in any actual project).

        Like I always, I'm the one who does the fringe stuff

        I have a custom Popup component, that has to have a Rectangle as the root element and not a Window(so it can't be Popup itself). But I create instances of it deep inside the item hierarchy
        for that reason I internally reassign the parent to the top most Item, on creation 😬


        that said, @lawrence-emke Window has a component that can and does function as parent, the parent of ccb exists, so you could do the following:

        Window
        {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
            color: "green"
            id: win
        
            Rectangle
            {
                id: ccb
                x: 87
                y: 190
                width: 100
                height: 100
        
        
                Component.onCompleted:
                {
                    console.log("number: " + ccb.children.length )
                }
        
                Rectangle
                {
                    id: rect1
                    color: "yellow"
                    parent: ccb.parent
                    x: 49
                    z:1
                    width: 30
                    height: 30
                }
                Rectangle
                {
                    parent: ccb.parent
                    id: rect2
                    color: "red"
                    x: 200
                    z:2
                    width: 12
                    height: 40
                }
            }
        }
        

        and it should behave as expected


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • lawrence.emkeL Offline
          lawrence.emkeL Offline
          lawrence.emke
          wrote on last edited by lawrence.emke
          #4

          I am designing a game that has 100+ pieces. They are all have simple definitions and I am considering a dynamic creation of the pieces rather
          than static definitions. They will be associated with individual static locations
          on the playing surface. I thought reparenting of the visual parent will solve
          some problems. And since they will be dynamically created my understanding is that the instantiating object will still own the physical instances.

          Now I wonder, in my example, Was the reparented object's physical parent property also moved with the visual parent? Since the child elements are
          dynamically generated they don't have any id values. So where do I destroy
          them? Do I destroy (or modify) them under the generating object or the visual parent? and how do I obtain access to them?

          My first thought was the "children" property
          of the parent object. The children property seems to be a JS array. It does have a "length" attribute which keeps track of the number of children. But trying to
          access child object via " parent.children[ x] " does not seem to work.

          How do I access reparented objects without any id property? Do I do it from the physical parent or the visual parent? And what parent property do I use to access the object and its properties?

          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