组件的 QML 屏幕坐标 [英] QML Screen Coordinates of Component

查看:50
本文介绍了组件的 QML 屏幕坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a simple, self-contained QML application, I can get the absolute screen coordinates of a component by saying

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

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

where myThing is the id of any other component in the file. However, if this file gets incorporated into another QML file and the component it defines is reused, I don't get the screen coordinates anymore; I get the local coordinates relative to the component where the statements above are executed.

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

How can I get the absolute screen coordinates of items?

推荐答案

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

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

where myThing.parent is your main compontent.

这篇关于组件的 QML 屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆