鼠标区域元素(MouseArea Element)
继承于Item,为了与不同的元素交互,你通常需要使用MouseArea(鼠标区域)元素。这是一个矩形的非可视化元素对象,你可以通过它来捕捉鼠标事件。当用户与可视化端口交互时,mouseArea通常被用来与可视化元素对象一起执行命令。
//此属性包含鼠标区域反应的鼠标按钮。
为了指定MouseArea将对多个按钮作出反应,使用“|”(or)运算符组合Qt::MouseButtons标志值:
MouseArea{acceptedButtons:Qt.LeftButton | Qt.righbutton}
为了指示接受所有可能的鼠标按钮,特殊值“Qt”。可以使用“所有按钮”:MouseArea { acceptedButtons: Qt.AllButtons }
默认 Qt.LeftButton.
acceptedButtons : Qt::MouseButtons
containsMouse : bool //鼠标悬停到控件上使能hoverEnabled =true时才能在不按下的时候获取到鼠标状态
containsPress : bool
cursorShape : Qt::CursorShape //鼠标形状
drag
drag.active : bool
drag.axis : enumeration
drag.filterChildren : bool
drag.maximumX : real
drag.maximumY : real
drag.minimumX : real
drag.minimumY : real
drag.smoothed : bool
drag.target : Item
drag.threshold : real
enabled : bool
hoverEnabled : bool //鼠标悬停到控件上使能
mouseX : real
mouseY : real
pressAndHoldInterval : int //修改长按事件间隔
pressed : bool
pressedButtons : MouseButtons //通过此属性& Qt.LeftButton可判断当前按下的鼠标状态是左右哪个
preventStealing : bool
//
This property holds whether composed mouse events will automatically propagate to other MouseAreas that overlap with this MouseArea but are lower in the visual stacking order. By default, this property is false.
MouseArea contains several composed events: clicked, doubleClicked and pressAndHold. These are composed of basic mouse events, like pressed, and can be propagated differently in comparison to basic events.
此属性决定合成的鼠标事件是否会自动传播到与此鼠标区域重叠但视觉堆叠顺序较低的其他鼠标区域。使用次属性时,视觉顶层的元素mousearea accepted=false,不能将鼠标点击事件拦截下来
propagateComposedEvents : bool
scrollGestureEnabled : bool
canceled()
clicked(MouseEvent mouse) //当鼠标按下并松开时触发
doubleClicked(MouseEvent mouse)
entered()
exited()
positionChanged(MouseEvent mouse)
pressAndHold(MouseEvent mouse)//按下并不释放,长按默认800ms
pressed(MouseEvent mouse)
released(MouseEvent mouse)
wheel(WheelEvent wheel)
Rectangle {
id: rect1
x: 12; y: 12
width: 76; height: 96
color: "lightsteelblue"
MouseArea {
id: area
width: parent.width
height: parent.height
onClicked: rect2.visible = !rect2.visible
}
}
Rectangle {
id: rect2
x: 112; y: 12
width: 76; height: 96
border.color: "lightsteelblue"
border.width: 4
radius: 8
}
注意
这是QtQuick中非常重要的概念,输入处理与可视化显示分开。这样你的交互区域可以比你显示的区域大很多。
Rectangle {
width: 480
height: 320
Rectangle {
x: 30; y: 30
width: 300; height: 240
color: "lightsteelblue"
MouseArea {
anchors.fill: parent
drag.target: parent;
drag.axis: "XAxis"
drag.minimumX: 30
drag.maximumX: 150
drag.filterChildren: true //筛选子窗口,拖动事件在子窗口中是否有效
onClicked: console.log("lightsteelblue Clicked")
Rectangle {
color: "yellow"
x: 50; y : 50
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: console.log("yellow Clicked")
}
}
}
}
}
Rectangle {
color: "yellow"
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: console.log("clicked yellow")
onDoubleClicked: {
console.log("onDoubleClicked yellow")
}
}
Rectangle {
color: "blue"
width: 50; height: 50
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
console.log("clicked blue")
mouse.accepted = false
}
onDoubleClicked: {
console.log("onDoubleClicked blue")
mouse.accepted = false
}
}
}
}
键盘按键
Keys.enabled: true
Keys.onEscapePressed: Qt.quit() //键入esc退出
Keys.onBackPressed: Qt.quit() //键入回退退出
Keys.onPressed: { //按键按下信号响应槽
switch(event.key){
case Qt.Key_0:
case Qt.Key_1:
case Qt.Key_2:
case Qt.Key_3:
case Qt.Key_4:
case Qt.Key_5:
case Qt.Key_6:
case Qt.Key_7:
case Qt.Key_8:
case Qt.Key_9:
event.accepted=true;
keyView.text = event.key - Qt.Key_0
break;
}
}
Keys.onReleased: { //按键松开信号响应槽 需要focus属性才能使得元素获取到焦点
switch(event.key){
case Qt.Key_0:
case Qt.Key_1:
case Qt.Key_2:
case Qt.Key_3:
case Qt.Key_4:
case Qt.Key_5:
case Qt.Key_6:
case Qt.Key_7:
case Qt.Key_8:
case Qt.Key_9:
event.accepted=true;
keyView.text = event.key
break;
}
}