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

QML学习05MouseArea

QML学习

  • 1、前言
  • 2、MouseArea
    • 2.1 移进区域触发(hoverEnabled)
    • 2.2 吞噬鼠标点击信号(accepted )和光标样式(cursorShape)
    • 2.3 鼠标拖动(drag)
    • 2.4 允许拖动子控件来拖动整个矩形(drag.filterChildren)
    • 2.5 使能控件(enabled)
    • 2.6鼠标坐标(mouseX,mouseY)
    • 2.7 鼠标长按(onPressAndHold)
    • 2.8 信号传递(propagateComposedEvents)
  • 3、总结

1、前言

记录一下QML学习的一些基础知识,方便自己日后回顾,也可以给有需要的人提供帮助。

2、MouseArea

2.1 移进区域触发(hoverEnabled)

MouseArea{id: mouseAreawidth: 200height: 200acceptedButtons: Qt.LeftButton | Qt.RightButton     //支持鼠标左键,右键hoverEnabled: truecursorShape:Qt.OpenHandCursor       //光标样式Rectangle{anchors.fill:parentcolor: "black"}onHoveredChanged: {console.log("onHoverChanged");}onClicked: {console.log("clicked")}//hoverEnabled为true,光标移进区域时触发,false则不触发onContainsMouseChanged: {console.log("onContainsMouseChanged",containsMouse)}onContainsPressChanged: {console.log("onContainsPressChanged",containsPress)}onPressed: {var ret = pressedButtons & Qt.LeftButtonvar ret2 = pressedButtons & Qt.RightButtonconsole.log("pressed")}onReleased: {console.log("relased")}
}

2.2 吞噬鼠标点击信号(accepted )和光标样式(cursorShape)

MouseArea{id: mouseAreawidth: 200height: 200acceptedButtons: Qt.LeftButton | Qt.RightButton     //支持鼠标左键,右键hoverEnabled: truecursorShape:Qt.OpenHandCursor       //光标样式Rectangle {color: "yellow"width: 100; height: 100MouseArea {anchors.fill: parentonClicked: console.log("clicked yellow")}Rectangle {color: "blue"width: 50; height: 50MouseArea {anchors.fill: parentpropagateComposedEvents: true       //使蓝色区域点击信号也能发送出去onClicked: {console.log("clicked blue")mouse.accepted = false          //不吞噬蓝色区域鼠标点击}onDoubleClicked: {console.log("onDoubleClicked")}}}
}

2.3 鼠标拖动(drag)

Rectangle {id: containerwidth: 600; height: 200Rectangle {id: rectwidth: 50; height: 50color: "red"opacity: (600.0 - rect.x) / 600     //不透明度MouseArea {anchors.fill: parentdrag.target: rectdrag.axis: Drag.XAxis | Drag.YAxis        //x轴y轴拖动drag.minimumX: 0drag.maximumX: container.width - rect.width}}}

2.4 允许拖动子控件来拖动整个矩形(drag.filterChildren)

Rectangle {width: 480height: 320Rectangle {x: 30; y: 30width: 300; height: 240color: "lightsteelblue"MouseArea {anchors.fill: parentdrag.target: parent;drag.axis: "XAxis"drag.minimumX: 30drag.maximumX: 150drag.filterChildren: true      //true时子控件可以拖动整个矩形Rectangle {color: "yellow"x: 50; y : 50width: 100; height: 100MouseArea {anchors.fill: parentonClicked: console.log("Clicked")}}}}
}

2.5 使能控件(enabled)

MouseArea{id: mouseAreawidth: 200height: 200acceptedButtons: Qt.LeftButton | Qt.RightButton     //支持鼠标左键,右键hoverEnabled: trueenabled: true           //使能控件cursorShape:Qt.OpenHandCursor       //光标样式Rectangle{anchors.fill:parentcolor: "black"}onHoveredChanged: {console.log("onHoverChanged");}onClicked: {console.log("clicked")}//hoverEnabled为true,光标移进区域时触发,false则不触发onContainsMouseChanged: {console.log("onContainsMouseChanged",containsMouse)}onContainsPressChanged: {console.log("onContainsPressChanged",containsPress)}onPressed: {var ret = pressedButtons & Qt.LeftButtonvar ret2 = pressedButtons & Qt.RightButtonconsole.log("pressed")}onReleased: {console.log("relased")}}
}

2.6鼠标坐标(mouseX,mouseY)

MouseArea{id: mouseAreawidth: 200height: 200acceptedButtons: Qt.LeftButton | Qt.RightButton     //支持鼠标左键,右键hoverEnabled: trueenabled: true           //使能控件cursorShape:Qt.OpenHandCursor       //光标样式Rectangle{anchors.fill:parentcolor: "black"}onMouseXChanged: {console.log("x: ", mouseX)  //鼠标x坐标}onMouseYChanged: {console.log("y: ", mouseY)  //鼠标y坐标}onHoveredChanged: {console.log("onHoverChanged");}onClicked: {console.log("clicked")}//hoverEnabled为true,光标移进区域时触发,false则不触发onContainsMouseChanged: {console.log("onContainsMouseChanged",containsMouse)}onContainsPressChanged: {console.log("onContainsPressChanged",containsPress)}onPressed: {var ret = pressedButtons & Qt.LeftButtonvar ret2 = pressedButtons & Qt.RightButtonconsole.log("pressed")}onReleased: {console.log("relased")}}
}

2.7 鼠标长按(onPressAndHold)

MouseArea{id: mouseAreawidth: 200height: 200acceptedButtons: Qt.LeftButton | Qt.RightButton     //支持鼠标左键,右键hoverEnabled: trueenabled: true           //使能控件cursorShape:Qt.OpenHandCursor       //光标样式pressAndHoldInterval: 3000          //长按时间间隔Rectangle{anchors.fill:parentcolor: "black"}onPressAndHold:{console.log("onPressAndHold")     //长按触发}}
}

2.8 信号传递(propagateComposedEvents)

    Rectangle {color: "yellow"width: 100; height: 100MouseArea {anchors.fill: parentonClicked: console.log("clicked yellow")}Rectangle {color: "blue"width: 50; height: 50MouseArea {anchors.fill: parentpropagateComposedEvents: true      //允许信号传递onClicked: {console.log("clicked blue")mouse.accepted = false      //不吞噬蓝色矩形信号}}}}
}

3、总结

以上就是QML的MouseArea的一些基础知识了,浏览过程中,如若发现错误,欢迎大家指正,有问题的欢迎评论区留言或者私信。最后,如果大家觉得有所帮助,可以点一下赞,谢谢大家!祝大家天天开心,顺遂无虞!
http://www.xdnf.cn/news/625753.html

相关文章:

  • 51、c# 请列举出6个集合类及用途
  • VLLM推理可以分配不同显存限制给两张卡吗?
  • MongoDB 备份与恢复策略全面指南:保障数据安全的完整方案
  • springboot中redis的事务的研究
  • 深入理解nvidia container toolkit核心组件与流程
  • 10大Python知识图谱开源项目全解析
  • 【Linux 学习计划】-- Linux调试工具 - gdb cgdb
  • 怎么开发一个网络协议模块(C语言框架)之(二) 数据结构设计
  • RabbitMQ核心特性——重试、TTL、死信队列
  • python项目和依赖管理工具uv简介
  • OpenLayers 加载鼠标位置控件
  • git常用操作命令
  • 用本地大模型解析智能家居语音指令:构建一个离线可用的文本控制助手
  • vitepress | 文档:展示与说明只写一次,使用vitepress-deme-preview插件
  • 力扣HOT100之回溯:46. 全排列
  • juc面试题
  • LumaDot (亮度可调的屏幕圆点)
  • 分布式消息中间件基础
  • 网络协议与通信安全
  • Oracle 19c DG备库报错ORA-00313、ORA-00312、ORA-27037
  • 【Linux仓库】权限的量子纠缠:用户/组/other如何编织Linux访问控制网?
  • el-form 使用el-row el-col对齐 注意事项
  • 从碎片化到集成化:Profibus转Profinet网关引领设备管理数字化转型
  • 【TypeScript】知识点梳理(四)
  • 5月24日day35打卡
  • qiankun解决的问题
  • ABC406E 题解
  • python中Web框架Flask vs FastAPI 对比分析
  • 一个开源的 Blazor 跨平台入门级实战项目
  • 红黑树简单模拟实现