简体   繁体   中英

How to get the X and Y translated values applied to a QQuickItem on to the C++ side

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 XY 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?

If you want to obtain the coordinates of an item with respect to another item, the procedure is:

  • convert the position of the item to a position relative to the scene
  • convert the position with respect to the scene to the position with respect to the other item.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM