QML组件的屏幕坐标

9

如果我有一个简单的、自包含的QML应用程序,我可以通过以下方式获取组件的绝对屏幕坐标:

Component.onCompeted: {    
    var l = myThing.mapToItem(null, 0, 0)
    console.log("X: " + l.x + " y: " + l.y)
}

这里的myThing是文件中任何其他组件的id。然而,如果该文件被合并到另一个QML文件中并且定义的组件被重复使用,那么我就不能再获得屏幕坐标;我将得到相对于执行上述语句的组件的本地坐标。

如何获取物品的绝对屏幕坐标?

3个回答

13
Component.onCompleted: {
    var globalCoordinares = myThing.mapToItem(myThing.parent, 0, 0)
    console.log("X: " + globalCoordinares.x + " y: " + globalCoordinares.y)
}

其中myThing.parent是您的主要组件。


2
很遗憾,我无法访问将使用此小部件的主要组件。 - Paul Carlisle
你是对的。我用 myThing.parent 进行了测试,如果父级 QML 元素填充整个窗口,则可以正常工作。如果这是您的根元素,则还可以使用 myThing.parent.parent。虽然不太好看且难以维护,但它确实有效。 - Simon Warta
@PaulCarlisle,有一种方法可以使用import访问不同的QML。 import "../Path/To/Parent/" as ParentOfMyThing 然后,应该可以使用以下内容:ParentOfMyThing.someId - Alexander Borodulya
主要组件可以使用动态作用域进行全局设置,只需添加 property Window vroot : id_root,然后 vroot 将在整个应用程序中可用。 - dtech
1
每个Item都应该有一个mapToGlobal方法。如果您使用ApplicationWindow创建窗口,则始终可以通过ApplicationWindow.contentItem访问顶级Item。我认为Window也是一个附加属性,用于获取实例化您的Item所在的窗口。(即,现在是这样。对于2014年的Qt可能会有所不同) - derM

3
自Qt 5.7版本以来,存在Item.mapToGlobal函数:
Component.onCompleted: {
    var globalCoordinates = mapToGlobal(0, 0)
    console.log("X: " + globalCoordinates.x + " y: " + globalCoordinates.y)
}

不幸的是,这似乎在委托上不起作用... - art-solopov

1

这是一个快速而粗糙的解决方案。注意:这只经过了轻微的测试,但目前似乎可以工作。我不建议在生产应用中使用它,因为它是一个完全的hack,并且很可能在某些时候出现问题。

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 640
    height: 480
    x: 150.0
    y: 150.0
    title: qsTr("Hello World")

    Rectangle {
        id: lolRect
        height: 50
        width: 50
        anchors.centerIn: parent;

        Component.onCompleted: {

            function getGlobalCordinatesOfItem(item) {

                // Find the root QML Window.
                function getRootWindowForItem(item) {
                    var cItem = item.parent;
                    if (cItem) {
                        return getRootWindowForItem(cItem);
                    } else {

                        // Path to the root items ApplicationWindow
                        var rootWindow = item.data[0].target;
                        if (rootWindow && rootWindow.toString().indexOf("ApplicationWindow") !== -1) {
                            return rootWindow;
                        } else {
                            console.exception("Unable to find root window!");
                            return null;
                        }
                    }
                }

                // Calculate the items position.
                var rootWindow = getRootWindowForItem(item);
                if (rootWindow) {
                    return Qt.point(rootWindow.x + lolRect.x,
                                    rootWindow.y + lolRect.y);
                } else {
                    return null;
                }
            }

            // Print the result.
            console.log(getGlobalCordinatesOfItem(this));
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接