当前位置: 首页 > article >正文

Qml-Item的函数使用

Qml-Item的函数使用

Item的提供了一些函数用于处理item之间父子关系,焦点链,以及item之间的坐标转换,本文重点示范item之间的坐标转换

Item的函数

  1. 函数childAt(real x,real y) :在item所在坐标系中,返回点point(x,y)处的第一个可视元素;
  2. 函数bool contains(point point) :在item所在坐标系中,点point是否在item中
  3. 函数point mapFromItem(Item item1, point p) :有4个重载函数,将item1对象中的 p点,转换到当前对象中对应点。src:item1, dst:当前对象
  4. 函数point mapToItem(Item item1, point p) :有4个重载函数,将当前对象中的 p点,转会到item1中应点。src:当前对象, dst:item1
  5. 函数** point mapFromGlobal(real x, real y)**: 将全局点point(x,y) 转换到当前对象中对应的点
  6. 函数point mapToGlobal(real x, real y): 将当前对象中点point(x,y) 转换到全局坐标系中对应的点

Item函数使用实例代码

在Rectangle中方一个MouseArea 和 两个子Rectangle,然后根据鼠标点击,调用Item公用函数,代码如下:

	import QtQuick

//验证Item中公用方法
Item{

    height: 480
    width: 320

    Rectangle{
        id:idRecPar
        x:10
        y:10
        width: 200
        height:200

        MouseArea{
            anchors.fill: parent

            onClicked:(mouse)=> {
                console.log("mouseX = " + mouseX + " mouseY = " + mouseY + " .x = " + mouse.x +" .y = " + mouse.y);

                var item = parent.childAt(mouse.x,mouse.y);                         //childAt() 获取在item坐标系中点(x,y)处的第一个子对象
                if(item === null)
                {
                    console.log("is null");
                    return;
                }
                //else
                    //console.log("clicked item ",item.id);
                if(item === idRecChild1)
                {
                  console.log("is child1");
                }
                else if(item === idRecChild2){
                    console.log("is child2");
                }

                var point2 = parent.mapToItem(idRecChild1, mouseX,mouseY);        //parent坐标系中点(x,y)转换到idRecChild1对象的坐标系中对应的点
                console.log("point 2 = ",point2);
                if(idRecChild1.contains(point2)){
                    console.log("in idRecChild1");
                    //point2 转到父对象中
                    var point3 = parent.mapFromItem(idRecChild1,point2);            //将idRecChild1中point2 点,转化到parent坐标系中对应点
                    console.log("point3 = ",point3);
                    if(point3.x === mouse.x && point3.y === mouse.y)
                    {

                    }
                }

                //发现mapToItem 和 mapFromItem, 浮点坐标可能转换出来都是浮点整数了,如果进行相应的 ===判断,可能出错
                var global1 = parent.mapToGlobal(mouse.x,mouse.y);                  //转为全局坐标
                var point4 = idRecChild1.mapFromGlobal(global1);
                console.log("global1 = ",global1," point 4 = ",point4);
                if(point4 === point2)
                {

                }

            }
        }

        Rectangle{
            id:idRecChild1
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.margins: 10
            width: 80
            height:80
            color:"lightgreen"
        }

        Rectangle{
            id:idRecChild2
            anchors.top: parent.top
            anchors.right: parent.right
            anchors.margins: 10
            width: 80
            height:80
            color:"lightblue"
        }
    }
}

Item函数运行输出如下

此处有个特殊现象:mapToItem()mapFromItem(), 浮点坐标可能转换出来都是浮点整数了,如果进行相应的 ===判断,可能出错;

在这里插入图片描述


http://www.kler.cn/a/349590.html

相关文章:

  • Vue 3 30天精进之旅:Day 10 - Vue Router
  • 2025一区新风口:小波变换+KAN!速占!
  • 【仓颉】仓颉编程语言Windows安装指南 配置环境变量 最简单解决中文乱码问题和其他解决方案大全
  • Unity 粒子特效在UI中使用裁剪效果
  • vue-有关于TS与路由器
  • [权限提升] Windows 提权 — 系统内核溢出漏洞提权
  • 如何有效进行主机加固?深信达MCK提供答案
  • SpringBoot实现电子文件签字+合同系统!
  • “八股文”面试:助力、阻力还是空谈?
  • 算法笔记day04
  • 5.资源《Arduino UNO R3 proteus 使用CD4511驱动数码管工程文件(含驱动代码)》说明。
  • 金字塔流(Pyramid Flow): 用于生成人工智能长视频的新文本-视频开源模型
  • Leetcode 第 141 场双周赛题解
  • Linux命令介绍:如何使用stat深入解析文件和文件系统状态
  • 江苏盐城中级职称申报条件及流程详解
  • ②PROFINET 转 EtherNet/IP, EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关
  • python爬虫--某动漫信息采集
  • 2024软件测试面试题大全【含答案】
  • Vue3 响应式数据
  • vue-jsonp的使用和腾讯地图当前经纬度和位置详情的获取
  • 【Vue】扫盲(五)Vue 的生命周期与钩子函数详解
  • Java基础:面向对象编程3
  • LLM-生成器判别器的实现
  • Vue中计算属性computed—(详解计算属性vs方法Methods,包括案例+代码)
  • 如何使用Python爬虫处理JavaScript动态加载的内容?
  • JavaSE——集合8:Map接口