import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
Window {
width: 640
height: 480
color: "white"
title: qsTr("My QML")
visible: true
// 鼠标点击进出
MouseArea{
id: mouseArea
width: 200
height: 200
//左键右键摁下
acceptedButtons: Qt.LeftButton | Qt.RightButton
//光标进入后改变
cursorShape: Qt.CrossCursor
//默认false 改为true会在鼠标进出控件时触发 onContainsMouseChanged containsMouse(进true 出false)
hoverEnabled: true
//enable置为false后就无视按钮所有事件
// enabled: false
//长按触发 onPressAndHold 单位毫秒
pressAndHoldInterval: 3000
Rectangle{
anchors.fill: parent
color: "black"
}
//完成一次点击后触发
onClicked: {
console.log("clicked")
}
//点击触发
onPressed: {
var ret = pressedButtons & Qt.LeftButton
var ret2 = pressedButtons & Qt.RightButton
console.log(ret ? "left pressed":ret2 ? "right pressed" : "other pressed")
}
//释放触发
onReleased: {
console.log("release")
}
//鼠标进出事件 (点击后判断进出 默认 实时判断进出 需要将hoverEnabled置为true)
onContainsMouseChanged: {
console.log("onContainsMouseChanged",containsMouse)
}
//鼠标进出事件 (在hoverEnabled是false时与onContainsMouseChanged行为一致)
onContainsPressChanged: {
console.log("onContainsPressChanged",containsPress)
}
//X坐标改变触发
onMouseXChanged: {
console.log("X = ",mouseX)
}
//Y坐标改变触发
onMouseYChanged: {
console.log("Y = ",mouseY)
}
//长按触发
onPressAndHold: {
console.log("onPressAndHold")
}
}
}
QML-MouseArea
于 2024-03-01 09:01:43 首次发布