鼠标区域元素(MouseArea Element)
继承于Item,为了与不同的元素交互,你通常需要使用MouseArea(鼠标区域)元素。这是一个矩形的非可视化元素对象,你可以通过它来捕捉鼠标事件。当用户与可视化端口交互时,mouseArea通常被用来与可视化元素对象一起执行命令。
//此属性包含鼠标区域反应的鼠标按钮。为了指定MouseArea将对多个按钮作出反应,使用“|”(or)运算符组合Qt::MouseButtons标志值:MouseArea{acceptedButtons:Qt.LeftButton | Qt.righbutton}为了指示接受所有可能的鼠标按钮,特殊值“Qt”。可以使用“所有按钮”:MouseArea { acceptedButtons: Qt.AllButtons }默认 Qt.LeftButton.acceptedButtons : Qt::MouseButtonscontainsMouse : bool //鼠标悬停到控件上使能hoverEnabled =true时才能在不按下的时候获取到鼠标状态containsPress : boolcursorShape : Qt::CursorShape //鼠标形状dragdrag.active : booldrag.axis : enumerationdrag.filterChildren : booldrag.maximumX : realdrag.maximumY : realdrag.minimumX : realdrag.minimumY : realdrag.smoothed : booldrag.target : Itemdrag.threshold : realenabled : boolhoverEnabled : bool //鼠标悬停到控件上使能mouseX : realmouseY : realpressAndHoldInterval : int //修改长按事件间隔pressed : boolpressedButtons : 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 : boolscrollGestureEnabled : bool
canceled()clicked(MouseEvent mouse) //当鼠标按下并松开时触发doubleClicked(MouseEvent mouse)entered()exited()positionChanged(MouseEvent mouse)pressAndHold(MouseEvent mouse)//按下并不释放,长按默认800mspressed(MouseEvent mouse)released(MouseEvent mouse)wheel(WheelEvent wheel)
Rectangle {id: rect1x: 12; y: 12width: 76; height: 96color: "lightsteelblue"MouseArea {id: areawidth: parent.widthheight: parent.heightonClicked: rect2.visible = !rect2.visible}}Rectangle {id: rect2x: 112; y: 12width: 76; height: 96border.color: "lightsteelblue"border.width: 4radius: 8}


注意
这是QtQuick中非常重要的概念,输入处理与可视化显示分开。这样你的交互区域可以比你显示的区域大很多。
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 //筛选子窗口,拖动事件在子窗口中是否有效onClicked: console.log("lightsteelblue Clicked")Rectangle {color: "yellow"x: 50; y : 50width: 100; height: 100MouseArea {anchors.fill: parentonClicked: console.log("yellow Clicked")}}}}}
Rectangle {color: "yellow"width: 100; height: 100MouseArea {anchors.fill: parentonClicked: console.log("clicked yellow")onDoubleClicked: {console.log("onDoubleClicked yellow")}}Rectangle {color: "blue"width: 50; height: 50MouseArea {anchors.fill: parentpropagateComposedEvents: trueonClicked: {console.log("clicked blue")mouse.accepted = false}onDoubleClicked: {console.log("onDoubleClicked blue")mouse.accepted = false}}}}
键盘按键
Keys.enabled: trueKeys.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_0break;}}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.keybreak;}}
