MouseArea QML Type
实现简单的鼠标操作。更多
Import Statement: | import QtQuick |
Inherits: |
属性
- acceptedButtons : Qt::MouseButtons
- containsMouse : bool
- 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
- preventStealing : bool
- propagateComposedEvents : bool
- scrollGestureEnabled : bool
信号
- canceled()
- clicked(MouseEvent mouse)
- doubleClicked(MouseEvent mouse)
- entered()
- exited()
- positionChanged(MouseEvent mouse)
- pressAndHold(MouseEvent mouse)
- pressed(MouseEvent mouse)
- released(MouseEvent mouse)
- wheel(WheelEvent wheel)
详细说明
MouseArea 是一种不可见的项目,通常与可见项目结合使用,以便为该项目提供鼠标处理功能。通过有效地充当代理,鼠标处理的逻辑可以包含在 MouseArea 项目中。
enabled 属性用于启用和禁用代理项的鼠标处理功能。禁用时,鼠标区域对鼠标事件透明。
MouseArea 是一个不可见项,但它有一个可见属性。当设置为 false 时,鼠标区域将对鼠标事件透明。
pressed 只读属性指示用户是否按住鼠标区域上的鼠标按钮。该属性通常用于用户界面属性之间的绑定。containsMouse 只读属性显示鼠标区域上是否有鼠标指针,但默认情况下,只有按住鼠标按钮时才会显示;详情请查看containsMouse 文档。
有关鼠标位置和按钮点击的信息是通过信号提供的,为此定义了事件处理程序属性。最常用的涉及处理鼠标按下和点击的信号有:onClicked、onDoubleClicked、onPressed、onReleased 和 onPressAndHold。还可以通过 onWheel 信号处理鼠标滚轮事件。
如果一个 MouseArea 与其他 MouseArea 项目的区域重叠,可以通过将propagateComposedEvents 设置为 true 并拒绝应传播的事件,选择将clicked
、doubleClicked
和pressAndHold
事件传播到这些其他项目。详情请参见propagateComposedEvents 文档。
默认情况下,MouseArea 项目只报告鼠标点击,而不报告鼠标光标位置的变化。设置hoverEnabled 属性可确保使用为 onPositionChanged、onEntered 和 onExited 定义的处理程序,而且即使没有按下鼠标按钮,也会更新containsMouse 属性。
使用示例
下面的示例在Rectangle 中使用了一个 MouseArea,当点击时,Rectangle 颜色会变为红色:
import QtQuick Rectangle { width: 100; height: 100 color: "green" MouseArea { anchors.fill: parent onClicked: { parent.color = 'red' } } }
许多 MouseArea 信号都会传递一个mouse 参数,其中包含有关鼠标事件的其他信息,如位置、按钮和任何按键修饰符。
下面是上一个示例的扩展,当区域被右键单击时会产生不同的颜色:
Rectangle { width: 100; height: 100 color: "green" MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: (mouse)=> { if (mouse.button == Qt.RightButton) parent.color = 'blue'; else parent.color = 'red'; } } }
另请参见 MouseEvent 、MouseArea 示例和 Qt Quick 中的重要概念 - 用户输入。
属性文档
这些属性包含鼠标光标的坐标。
如果hoverEnabled 属性为 false,那么这些属性只有在按下按钮时才有效,而且只要按住按钮,即使鼠标移动到区域外,这些属性也将保持有效。
默认情况下,该属性为 false。
如果hoverEnabled 为 true,这些属性将在以下情况下有效:
- 未按下按钮,但鼠标位于MouseArea 内(containsMouse 为 true)。
- 按下并按住一个按钮,即使该按钮已移出该区域。
坐标相对于MouseArea 。
acceptedButtons : Qt::MouseButtons |
该属性包含鼠标区域会做出反应的鼠标按钮。
要指定MouseArea 对多个按钮做出反应,Qt::MouseButtons 标志值可使用"|"(或)运算符组合:
MouseArea { acceptedButtons: Qt.LeftButton | Qt.RightButton }
要表示接受所有可能的鼠标按钮,可以使用特殊值 "Qt.AllButtons":
MouseArea { acceptedButtons: Qt.AllButtons }
默认值是Qt.LeftButton
。
containsMouse : bool |
该属性表示鼠标当前是否位于鼠标区域内。
警告: 如果hoverEnabled 是false
,当鼠标光标在MouseArea 内时按下鼠标,containsMouse
将是true
。但如果在onPressed
处理程序中设置mouse.accepted = false
,containsMouse
将保持false
,因为按下被拒绝。
containsPress : bool |
这是一个方便的属性,等同于pressed && containsMouse
,也就是说,它可以确定当前是否有任何acceptedButtons 被按下,且鼠标当前位于MouseArea 内。
当鼠标在项目范围内按下时,该属性对突出显示项目特别有用。
另请参阅 pressed 和containsMouse 。
cursorShape : Qt::CursorShape |
该属性用于保存该鼠标区域的光标形状。请注意,在不显示鼠标光标的平台上,该属性可能不起作用。
可用的光标形状有
- Qt.ArrowCursor
- Qt.UpArrowCursor 上箭头光标
- Qt.CrossCursor
- Qt.WaitCursor
- Qt.IBeamCursor
- Qt.SizeVerCursor
- Qt.SizeHorCursor
- Qt.SizeBDiagCursor
- Qt.SizeFDiagCursor
- Qt.SizeAllCursor
- Qt.BlankCursor
- Qt.SplitVCursor
- Qt.SplitHCursor
- Qt.PointingHandCursor
- Qt.ForbiddenCursor
- Qt.WhatsThisCursor
- Qt.BusyCursor
- Qt.OpenHandCursor
- Qt.ClosedHandCursor
- Qt.DragCopyCursor
- Qt.DragMoveCursor
- Qt.DragLinkCursor
为了只为一个区域设置鼠标光标形状而不对鼠标事件做出反应,请将acceptedButtons 设置为 none:
MouseArea { cursorShape: Qt.IBeamCursor; acceptedButtons: Qt.NoButton }
默认值为Qt.ArrowCursor
。
注意: 如果cursorShape
属性设置为undefined
,则进入MouseArea
时不会改变现有形状。
另请参阅 Qt::CursorShape 。
drag
提供了一种使项目可拖动的便捷方法。
drag.target
指定要拖动的项目的 id。drag.active
指定目标项目当前是否正在被拖动。drag.axis
指定是否可以水平( )、垂直( )或两者( )拖动。Drag.XAxis
Drag.YAxis
Drag.XAndYAxis
drag.minimum
和 限制目标项可以沿相应轴拖动的距离。drag.maximum
下面的示例显示了一个可以沿 X 轴拖动的Rectangle 。向右拖动时,矩形的不透明度会降低。
Rectangle { id: container width: 600; height: 200 Rectangle { id: rect width: 50; height: 50 color: "red" opacity: (600.0 - rect.x) / 600 MouseArea { anchors.fill: parent drag.target: rect drag.axis: Drag.XAxis drag.minimumX: 0 drag.maximumX: container.width - rect.width } } }
注意: 如果锚定了所需的drag.axis
,则无法拖动项目。例如,如果上例中为rect
设置了anchors.left
或anchors.right
,则无法沿 X 轴拖动。如果在onPressed 处理程序中将锚点值设置为undefined
,就可以避免这种情况。
如果drag.filterChildren
设置为 true,拖动就可以覆盖后代的 MouseAreas。这样,父MouseArea 就可以处理拖动,而子代处理点击:
import 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 Rectangle { color: "yellow" x: 50; y : 50 width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: console.log("Clicked") } } } } }
drag.threshold
决定拖动操作何时开始的阈值(以像素为单位)。默认情况下,该值与平台相关。该属性在 2.2 中添加。Qt Quick
如果drag.smoothed
为true
,则只有在拖动操作开始后才会移动目标。如果设置为false
,目标将直接移动到当前鼠标位置。默认情况下,此属性为true
。该属性在Qt Quick 2.4 中添加。
enabled : bool |
该属性表示项目是否接受鼠标事件。
注: 由于历史原因,该属性并不等同于 Item.enabled。它只影响鼠标事件,其效果不会传播给子项目。
默认情况下,此属性为 true。
hoverEnabled : bool |
该属性用于确定是否处理悬停事件。
默认情况下,只有在响应按钮事件或按下按钮时才会处理鼠标事件。悬停可处理所有鼠标事件,即使没有按下鼠标按钮。
该属性会影响containsMouse 属性以及 onEntered、onExited 和 onPositionChanged 信号。
pressAndHoldInterval : int |
该属性可重置pressAndHold
发出前的经过时间(以毫秒为单位)。
如果未明确设置或重置后,该值将跟随QStyleHints::mousePressAndHoldInterval
。
通常情况下,使用应用程序样式提示全局设置该属性即可。当某些鼠标区域需要不同的时间间隔时,应使用此属性。
另请参见 pressAndHold 。
pressed : bool |
该属性显示acceptedButtons 当前是否被按下。
pressedButtons : MouseButtons |
该属性显示当前按下的鼠标按钮。
它包含以下按位组合
- Qt.LeftButton
- Qt.RightButton
- Qt.MiddleButton
当鼠标右键被按下时,下面的代码会显示 "right":
Text { text: mouseArea.pressedButtons & Qt.RightButton ? "right" : "" horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter MouseArea { id: mouseArea anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton } }
注意: 该属性仅处理acceptedButtons 中指定的按钮。
另请参阅 acceptedButtons 。
preventStealing : bool |
propagateComposedEvents : bool |
该属性表示组成的鼠标事件是否会自动传播到与该MouseArea 重叠但在视觉堆叠顺序中较低的其他 MouseAreas。默认情况下,该属性为 false。
MouseArea 鼠标区域包含多个组成事件: 和 。这些事件由基本鼠标事件组成,如 ,传播方式与基本事件不同。clicked
doubleClicked
pressAndHold
pressed
如果 propagateComposedEvents 设置为 true,那么组成事件将自动传播到场景中相同位置的其他 MouseAreas。每个事件都会按照堆叠顺序传播到它下面的下一个enabled MouseArea ,一直向下传播,直到MouseArea 接受该事件为止。与pressed
事件不同,如果没有处理程序,组成的事件不会被自动接受。
例如,下面是一个包含蓝色Rectangle 的黄色Rectangle 。蓝色矩形是可视堆叠顺序层次结构中最顶层的项目;它将在视觉上呈现在黄色矩形之上。由于蓝色矩形将 propagateComposedEvents 设置为 true,并将所有接收到的clicked
事件的MouseEvent::accepted 设置为 false,因此它接收到的任何clicked
事件都会传播到其下方黄色矩形的MouseArea 。
import QtQuick 2.0 Rectangle { color: "yellow" width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: console.log("clicked yellow") } Rectangle { color: "blue" width: 50; height: 50 MouseArea { anchors.fill: parent propagateComposedEvents: true onClicked: (mouse)=> { console.log("clicked blue") mouse.accepted = false } } } }
单击蓝色矩形将调用其子MouseArea 的onClicked
处理程序;然后该事件将传播到黄色矩形的MouseArea ,从而调用其自身的onClicked
处理程序。
当你想让重叠的 MouseAreas 一起处理组成的事件时,这个属性大大简化了使用情况。例如:希望一个MouseArea 处理clicked
信号,另一个处理pressAndHold
,或者希望一个MouseArea 在大部分时间处理clicked
,但在满足特定条件时将其传递给另一个。
scrollGestureEnabled : bool |
信号文档
canceled() |
clicked(MouseEvent mouse) |
该信号在出现点击时发出。点击被定义为按下后释放,且都在MouseArea 内(按下后移动到MouseArea 外,然后移动回 内并释放也被视为点击)。
mouse 参数提供了有关单击的信息,包括单击释放时的 x 和 y 位置,以及单击是否被保持。
处理此信号时,除非propagateComposedEvents 属性为true
,否则更改mouse 参数的accepted 属性不会有任何影响。
注: 相应的处理程序是onClicked
。
doubleClicked(MouseEvent鼠标) |
entered() |
该信号在鼠标进入鼠标区域时发出。
默认情况下,只有当前按下按钮时才会发出该信号。将hoverEnabled 设置为 true,即使没有按下鼠标按钮也会发出该信号。
注: 相应的处理程序是onEntered
。
另请参阅 hoverEnabled 。
exited() |
当鼠标退出鼠标区域时会发出该信号。
默认情况下,只有当前按键被按下时才会发出该信号。将hoverEnabled 设置为 true,即使没有按下鼠标按钮也会发出该信号。
下面的示例显示了两个 MouseAreas(鼠标区域)之间相当典型的关系,mouseArea2
位于mouseArea1
的顶部。将鼠标从mouseArea1
移入mouseArea2
会导致mouseArea1
发出exited
信号。
Rectangle { width: 400; height: 400 MouseArea { id: mouseArea1 anchors.fill: parent hoverEnabled: true } MouseArea { id: mouseArea2 width: 100; height: 100 anchors.centerIn: parent hoverEnabled: true } }
如果给这两个 MouseAreas 赋予父子关系,从mouseArea1
将鼠标移动到mouseArea2
将不会导致mouseArea1
发出exited
信号。相反,它们将被视为同时悬停。
注: 相应的处理程序是onExited
。
另请参阅 hoverEnabled 。
positionChanged(MouseEvent mouse) |
该信号在鼠标位置发生变化时发出。
mouse 参数提供了有关鼠标的信息,包括 x 和 y 位置,以及当前按下的任何按钮。
默认情况下,只有当前有按钮被按下时才会发出该信号。将hoverEnabled 设置为 true,即使没有按下鼠标按钮也会发出该信号。
处理此信号时,更改mouse 参数的accepted 属性不会产生任何影响。
注: 相应的处理程序是onPositionChanged
。
pressAndHold(MouseEvent mouse) |
长按时(当前为 800ms)会发出该信号。mouse 参数提供了有关按压的信息,包括按压的 x 和 y 位置以及按压的按钮。
处理该信号时,除非propagateComposedEvents 属性为true
,否则更改mouse 参数的accepted 属性不会有任何影响。
注: 相应的处理程序是onPressAndHold
。
pressed(MouseEvent mouse) |
released(MouseEvent mouse) |
wheel(WheelEvent wheel) |
该信号是对鼠标滚轮和触控板滚动手势的响应。
wheel 参数提供了事件的相关信息,包括 x 和 y 位置、当前按下的任何按钮以及滚轮移动的相关信息(包括 angleDelta 和 pixelDelta)。
注: 相应的处理程序是onWheel
。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.