Reputation: 8229
I have the following QML Rectangle
which has a parent item. The most important thing to note is that it applies a Translate QML element which I am struggling to understand as to what exactly it does the QML item and its children it applies to.
Code:
Window {
id: main_window
width: 640
height: 480
visible: true
Item {
id: rect_parent
objectName: "rect_parent_object"
x: 0
y: 0
width: parent.width
height: parent.height
transform: Translate {x: -20; y: -30}
Rectangle {
id: rect
objectName: "rect_object"
x: parent.width/2
y: parent.height/2
width: parent.width/3
height: parent.height/3
color: "red"
}
}
}
rect_parent
has a property transform: Translate
as you see in above code. Following is the X Y translation that is applied to it
transform: Translate {x: -20; y: -20}
In C++ part of my code in main.cpp
I am getting the QQuickItem
s in the following way.
QQuickItem *rectQuickItem = qml_engine->rootObjects()[0]->findChild<QQuickItem*>("rectObject");
QQuickItem *rectQuickItemParent = qml_engine->rootObjects()[0]->findChild<QQuickItem*>("rectParentObject");
Yes, I can get the x
and y
of rectQuickItem
in the following way.
QQuickItem *rectQuickItemParent = qml_engine->rootObjects()[0]->findChild<QQuickItem*>("rectParentObject");
qreal item_x = rectQuickItem->x();
qreal item_y = rectQuickItem->y();
Question:
But how do I get rectQuickItem
's translated x and y?
I found that item_x
and item_y
are not the x and y that are actually applied on the UI. It seems that transform: Translate
is adding some units to both x and y of rect
which I dont get when I query rectQuickItem->x()
.
In simpler words, I need the -20
and -30
applied on x and y in transform: Translate
block of rect_parent
which eventually applies to rect
Objective:
I am changing the parent of rectQuickItem
to display it on another window with the same respective x and y location as original parent. I need the units that got added to x
and y
of properties of rectQuickItem
due to transform: Translate
being set so as to display rect
on visually the same location as previous parent.
Additional question:
Would QQuickItem::MapToItem help me in any way here?
Upvotes: 0
Views: 973
Reputation: 243897
If you want to obtain the coordinates of an item with respect to another item, the procedure is:
static QPointF positionToAnotherItem(QQuickItem *source, QQuickItem *destine){
QPointF p = source->mapToScene(QPointF(0, 0));
return destine->mapFromScene(p);
}
static QPointF positionToParent(QQuickItem *item){
return positionToAnotherItem(item, item->parentItem());
}
the transformations do not apply immediately so by applying the above procedure you will not get the right positions, you must apply them with the help of the xChanged and YChanged signals.
QQuickItem *rectQuickItem = qml_engine.rootObjects()[0]->findChild<QQuickItem*>("rect_object");
//QQuickItem *rectQuickItemParent = qml_engine.rootObjects()[0]->findChild<QQuickItem*>("rect_parent_object");
QObject::connect(rectQuickItem, &QQuickItem::xChanged, [rectQuickItem]{
qDebug()<<positionToParent(rectQuickItem);
});
QObject::connect(rectQuickItem, &QQuickItem::yChanged, [rectQuickItem]{
qDebug()<<positionToParent(rectQuickItem);
});
In the following link there is a complete example
Upvotes: 1