Is child coordinates (x, y, z) associated with visual parent, or object parent?
-
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.12Window
{
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: "green"
id: winRectangle { 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 QQuickItemWell, 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? -
@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 aQQuickItem
(thus also notItem
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.
-
@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
-
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?