【问题标题】:QML Screen Coordinates of Component组件的 QML 屏幕坐标
【发布时间】:2014-02-22 15:10:12
【问题描述】:

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

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

其中 myThing 是文件中任何其他组件的 ID。但是,如果这个文件被合并到另一个 QML 文件中并且它定义的组件被重用,我就不再获得屏幕坐标;我得到了相对于执行上述语句的组件的局部坐标。

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

【问题讨论】:

    标签: qt qml qt-quick


    【解决方案1】:
    Component.onCompleted: {
        var globalCoordinares = myThing.mapToItem(myThing.parent, 0, 0)
        console.log("X: " + globalCoordinares.x + " y: " + globalCoordinares.y)
    }
    

    myThing.parent 是您的主要组件。

    【讨论】:

    • 不幸的是,我无法访问将使用此小部件的主要组件。
    • 你是对的。我使用myThing.parent 对其进行了测试,以防父 QML 元素填满整个窗口。如果那是您的根元素,您也可以使用myThing.parent.parent。不是很好和可维护,但它的工作原理。
    • @PaulCarlisle,存在使用 import 访问不同 QML 的方法。 import "../Path/To/Parent/" as ParentOfMyThing 那么,应该可以使用以下东西:ParentOfMyThing.someId
    • 主要组件可以使用动态作用域设置为全局,只需一个property Window vroot : id_root,然后vroot 将可用于整个应用程序。
    • Items 应该有一个 mapToGlobal 方法。此外,如果您使用ApplicationWindow 创建窗口,您始终可以使用ApplicationWindow.contentItem 访问顶层Item。我认为Window 也是一个附加属性,用于获取实例化您的项目的窗口。 (也就是说,现在。2014 Qt 可能会有所不同)
    【解决方案2】:

    这是一个快速且非常肮脏的解决方案。注意:这只是经过轻微测试,但到目前为止似乎有效。我不建议在生产应用程序中使用它,因为它是一个完整的 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));
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      从 Qt 5.7 开始,有Item.mapToGlobal:

      Component.onCompleted: {
          var globalCoordinates = mapToGlobal(0, 0)
          console.log("X: " + globalCoordinates.x + " y: " + globalCoordinates.y)
      }
      

      【讨论】:

      • 不幸的是,这显然不适用于代表...
      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多