Qt 组件的QML屏幕坐标

Qt 组件的QML屏幕坐标,qt,qml,qt-quick,Qt,Qml,Qt Quick,如果我有一个简单的、自包含的QML应用程序,我可以通过以下方式获得组件的绝对屏幕坐标 Component.onCompeted: { var l = myThing.mapToItem(null, 0, 0) console.log("X: " + l.x + " y: " + l.y) } 其中myThing是文件中任何其他组件的id。但是,如果这个文件被合并到另一个QML文件中,并且它定义的组件被重用,我就不会再获得屏幕坐标;我得到了相对于执行上述语句的组件的局部坐

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

Component.onCompeted: {    
    var l = myThing.mapToItem(null, 0, 0)
    console.log("X: " + l.x + " y: " + l.y)
}
其中myThing是文件中任何其他组件的id。但是,如果这个文件被合并到另一个QML文件中,并且它定义的组件被重用,我就不会再获得屏幕坐标;我得到了相对于执行上述语句的组件的局部坐标

如何获取项目的绝对屏幕坐标

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

其中
虚构。家长
是您的主要内容。

这里有一个快速而肮脏的解决方案。注意:这只经过了少量测试,但到目前为止似乎有效。我不建议在生产应用程序中使用它,因为它是一个完整的黑客程序,可能会在某个时候崩溃

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));
        }
    }
}
自Qt 5.7以来,有:


不幸的是,我无法访问将使用此小部件的主组件。你是对的。我用myThing.parent对它进行了测试,如果父QML元素填满了整个窗口,它就会起作用。如果这是根元素,还可以使用
myThing.parent.parent
。不是很好,也不易维护,但它可以工作。@PaulCarlisle,有一种方法可以使用
import
访问不同的QML<代码>导入“./Path/To/Parent/”作为Mathing的父对象
然后,应该可以使用以下内容:
Mathing的父对象。someId
可以使用动态作用域将主组件设置为全局,只需有一个
属性窗口vroot:id\u root
,然后
vroot
将可用于整个应用程序。
Item
s应具有
mapToGlobal
方法。此外,如果使用
ApplicationWindow
创建窗口,则始终可以使用
ApplicationWindow.contentItem
访问顶级
项目。我认为
Window
也是一个附加属性,用于获取实例化项目的窗口。(也就是说,现在。2014 Qt可能不同)不幸的是,这显然不适用于代表。。。
Component.onCompleted: {
    var globalCoordinates = mapToGlobal(0, 0)
    console.log("X: " + globalCoordinates.x + " y: " + globalCoordinates.y)
}