当前位置: 首页 > 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的一些基础知识了,浏览过程中,如若发现错误,欢迎大家指正,有问题的欢迎评论区留言或者私信。最后,如果大家觉得有所帮助,可以点一下赞,谢谢大家!祝大家天天开心,顺遂无虞!

相关文章:

  • 【LangChain大模型应用与多智能体开发 ② 接入智谱AI】
  • 【大模型面试每日一题】Day 27:自注意力机制中Q/K/V矩阵的作用与缩放因子原理
  • 搜索二叉树
  • InnoDB引擎底层解析(二)之InnoDB的Buffer Pool(三)
  • Linux驱动:基本环境准备
  • 【免费使用】剪Y专业版 8.1/CapCut 视频编辑处理,素材和滤镜
  • 基于CSP模型实现的游戏排行榜
  • AI大模型核心基础:向量与张量原理及实践应用指南
  • ARM笔记-嵌入式系统基础
  • 基于python的百度迁徙迁入、迁出数据分析(城市版)
  • 将ft2232外部的EEPROM中的信息读出来的方法,方便写入到下一个eeprom里面去;
  • Firecrawl MCP Server 深度使用指南
  • Linux系统基础——是什么、适用在哪里、如何选
  • NSSCTF-[羊城杯 2023]程序猿Quby
  • 建筑机械员(建筑施工机械管理人员)考试练习题
  • unordered_set与unordered_map实现详解剖析
  • 通过设备节点获取已注册的 i2c client
  • Vue3 Composition API: 企业级应用最佳实践方案
  • 什么是电离层闪烁
  • YOLOv8损失函数代码详解(示例展示数据变换过程)
  • cpa广告网站怎么做/临沂网站建设方案服务
  • 怎样为网站做外链/怎么开网站平台
  • 开发公司网签物料/seo网页的基础知识
  • php做自己的网站/百度百科官网登录
  • 内容管理系统有哪些/seo对网店推广的作用有哪些
  • 网站备案上传照片几寸/天津百度网站快速优化