Qt QML 坐标转换函数

版本

Qt5.15

相关函数

下面这些函数是隶属于Item的

/// Item
object mapFromGlobal(real x, real y)
object mapFromItem(Item item, rect r)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapFromItem(Item item, point p)
object mapFromItem(Item item, real x, real y)
object mapToGlobal(real x, real y)
object mapToItem(Item item, rect r)
object mapToItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, point p)
object mapToItem(Item item, real x, real y)

值得注意的是mapFromGlobalmapToGlobal的使用版本是自Qt5.7开始的

mapFromItemmapToItem是自Qt 5.15开始的

mapFrom和mapTo,
前者是指从被指向的item坐标系里的坐标参数,映射到使用者坐标系里的坐标是如何;
后者是指使用者坐标系里的坐标参数,映射到被指向的item坐标系里的坐标是如何。

打个调用示例:

Rectangle {
	id: rect1
	width: 100
	height: 100
	...
	Button {
		id: button1
		x: 50
		y: 52
		width: 30
		height: 30
		...
...

mapToItem

button1.mapToItem(rect1, 10, 12)

该函数用法是button1里QPointF(10, 12)这个坐标,在rect1里坐标是QPointF(0 + 50 + 10, 0 + 52 + 12)

官方解释:
Maps the point (x, y) or rect (x, y, width, height), which is in this item’s coordinate system, to item’s coordinate system, and returns a point or rect matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
If item is a null value, this maps the point or rect to the coordinate system of the root QML view.
The versions accepting point and rect are since Qt 5.15.

有道翻译:
将位于该项坐标系统中的点(x, y)或矩形(x, y,宽度,高度)映射到该项的坐标系统,并返回与映射坐标匹配的点或矩形。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
如果item为空值,则将点或矩形映射到根QML视图的坐标系统。
接受point和rect的版本是从Qt 5.15开始的。

mapFromItem

button1.mapFromItem(rect1, 10, 12)

该函数用法是rect1里QPointF(10, 12)这个坐标,在button1里坐标是QPointF(0 - 50 + 10, 0 - 52 + 12)

官方解释
Maps the point (x, y) or rect (x, y, width, height), which is in item’s coordinate system, to this item’s coordinate system, and returns a point or rect matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
If item is a null value, this maps the point or rect from the coordinate system of the root QML view.
The versions accepting point and rect are since Qt 5.15.

有道翻译
将项目坐标系统中的点(x, y)或矩形(x, y,宽度,高度)映射到该项目的坐标系统,并返回与映射坐标匹配的点或矩形。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
如果item为空值,则从根QML视图的坐标系统映射点或矩形。
接受point和rect的版本是从Qt 5.15开始的。

mapToGlobal与mapFromGlobal

button1.mapToGlobal(10, 12)
button1.mapFromGlobal(10, 12)

上面这两个与上同理,只是换成了Global,这个Global的对应对象就是根节点的Item所在坐标系,不过要注意的
是Window这个根节点,其的坐标系是整个屏幕的坐标系,而不是窗口的坐标系

mapToGlobal官方解释
Maps the point (x, y), which is in this item’s coordinate system, to the global coordinate system, and returns a point matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
This method was introduced in Qt 5.7.

有道翻译
将位于该项坐标系统中的点(x, y)映射到全局坐标系统,并返回与映射坐标匹配的点。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
这种方法是在Qt 5.7中引入的。

mapFromGlobal官方解释
Maps the point (x, y), which is in the global coordinate system, to the item’s coordinate system, and returns a point matching the mapped coordinate.
The following properties of the item are used in the mapping: x, y, scale, rotation, transformOrigin, and transform.
This method was introduced in Qt 5.7.

有道翻译
将全局坐标系中的点(x, y)映射到项目的坐标系,并返回与映射坐标匹配的点。
在映射中使用项目的以下属性:x、y、缩放、旋转、transformOrigin和转换。
这种方法是在Qt 5.7中引入的。

相关小技巧

button1.mapToItem(null, 10, 12)

该函数用法是button1里QPointF(10, 12)这个坐标,在整个窗口里的坐标是如何
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt QML是一种基于JavaScript的声明式语言,用于快速构建跨平台的用户界面。它有时需要与C++代码交互,因为C++可以提供更高性能和更底层的功能。 要在Qt QML中调用C函数,需要进行以下步骤: 1. 创建一个继承自QObject的C++类,并在其中定义所需的函数。这些函数需要使用Q_INVOKABLE宏进行标记,以便在QML中调用。 ```cpp // MyFunctions.h #include <QObject> class MyFunctions: public QObject { Q_OBJECT public: explicit MyFunctions(QObject *parent = nullptr); Q_INVOKABLE void myFunction(); }; ``` 2. 在QML文件中导入C++类,并使用其实例调用函数。 ```qml import MyFunctions 1.0 Window { // ... Button { text: "调用C函数" onClicked: { MyFunctions.myFunction(); } } // ... } ``` 3. 在C++代码中将该类注册到QML引擎中。 ```cpp // main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include "MyFunctions.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<MyFunctions>("MyFunctions", 1, 0, "MyFunctions"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } ``` 通过以上步骤,就可以在Qt QML中成功调用C函数了。在按钮点击事件中调用C++类的函数,可以在C++代码中执行所需的操作,并将结果返回到QML界面中进行展示。这种方式可以实现Qt QML框架与C++高性能功能的结合,使得开发者能够更好地发挥Qt的优秀特性和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溪渣渣_梁世华

打赏?我甚至没有任何收费的章节

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值