HoverHandler QML Type

鼠标和平板悬停处理程序。更多

Import Statement: import QtQuick
Inherits:

SinglePointHandler

属性

信号

  • canceled(eventPoint point)
  • grabChanged(PointerDevice::GrabTransition transition, eventPoint point)

详细说明

HoverHandler 可检测悬停的鼠标或平板手写笔光标。

hovered 属性绑定是在光标进入或离开parent 项目时做出反应的最简单方法。point 属性提供了更多细节,包括光标位置。acceptedDevicesacceptedPointerTypesacceptedModifiers 属性可用于缩小行为范围,以检测特定类型设备的悬停或按住修饰符键时的悬停。

cursorShape 属性允许在hovered 变为true 时更改光标。

另请参阅 MouseArea,PointHandler, 和Qt Quick 示例 - 指针处理程序

属性文档

acceptedDevices : flags

可激活指针处理程序的指向设备类型。

默认情况下,该属性设置为PointerDevice.AllDevices 。如果将其设置为设备类型的 OR 组合,则会忽略来自非匹配设备的指针事件。

例如,可以用两种处理程序分别响应鼠标悬停和触控笔悬停:

import QtQuick

Rectangle {
    width: 150; height: 50; radius: 3
    color: mouse.hovered ? "goldenrod" : stylus.hovered ? "tomato" : "wheat"

    HoverHandler {
        id: stylus
        acceptedDevices: PointerDevice.Stylus
        cursorShape: Qt.CrossCursor
    }

    HoverHandler {
        id: mouse
        acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
        cursorShape: Qt.PointingHandCursor
    }
}

可用的设备类型如下:

常量说明
PointerDevice.Mouse鼠标
PointerDevice.TouchScreen触摸屏
PointerDevice.TouchPad触摸板或触控板。
PointerDevice.Stylus绘图板上的触控笔。
PointerDevice.Airbrush绘图板上的喷笔。
PointerDevice.Puck绘图板上的带十字准线的数字转换器。
PointerDevice.AllDevices任何类型的指向设备。

注意: 并非所有平台都能区分鼠标和触摸板,在能区分的平台上,你通常希望鼠标和触摸板的行为相同。

另请参阅 QInputDevice::DeviceType


acceptedModifiers : flags

如果设置了该属性,只有按下给定的键盘修饰符时,才会处理悬停事件。不按键盘修饰符时,事件将被忽略。

该属性默认设置为Qt.KeyboardModifierMask ,因此无论是否按下任何修改键,都会处理悬停事件。

例如,一个Item 可以有两个相同类型的处理程序,其中一个仅在按下所需键盘修饰符时启用:

import QtQuick

Rectangle {
    width: 150; height: 50; radius: 3
    color: control.hovered ? "goldenrod" : shift.hovered ? "wheat" : "beige"

    HoverHandler {
        id: control
        acceptedModifiers: Qt.ControlModifier
        cursorShape: Qt.PointingHandCursor
    }

    HoverHandler {
        id: shift
        acceptedModifiers: Qt.ShiftModifier
        cursorShape: Qt.CrossCursor
    }
}

可用的修改器如下:

常量说明
Qt.NoModifier不允许按修改键。
Qt.ShiftModifier必须按键盘上的 Shift 键。
Qt.ControlModifier必须按键盘上的 Ctrl 键。
Qt.AltModifier必须按下键盘上的 Alt 键。
Qt.MetaModifier必须按下键盘上的 Meta 键。
Qt.KeypadModifier必须按下键盘上的按键。
Qt.GroupSwitchModifier必须按下键盘上的 Mode_switch 键。仅限 X11(除非在 Windows 上通过命令行参数激活)。
Qt.KeyboardModifierMask处理程序忽略修改键。

另请参见 Qt::KeyboardModifier


acceptedPointerTypes : flags

可激活指针处理程序的指向工具类型(通用、手写笔、橡皮擦等)。

默认情况下,该属性设置为PointerDevice.AllPointerTypes 。如果将其设置为设备类型的 OR 组合,则会忽略来自非匹配事件的事件。

例如,您可以根据手写笔或橡皮是否悬停在图形板上来改变光标,从而提供反馈:

import QtQuick

Rectangle {
    id: rect
    width: 150; height: 150

    HoverHandler {
        id: stylus
        acceptedPointerTypes: PointerDevice.Pen
        cursorShape: Qt.CrossCursor
    }

    HoverHandler {
        id: eraser
        acceptedPointerTypes: PointerDevice.Eraser
        cursorShape: Qt.BlankCursor
        target: Image {
            parent: rect
            source: "images/cursor-eraser.png"
            visible: eraser.hovered
            x: eraser.point.position.x
            y: eraser.point.position.y - 32
        }
    }
}

可用的指针类型如下:

常量说明
PointerDevice.Generic鼠标或模拟鼠标的设备。
PointerDevice.Finger触摸屏上的手指(悬停检测不太可能)。
PointerDevice.Pen绘图板上的手写笔。
PointerDevice.Eraser绘图板上的橡皮擦。
PointerDevice.Cursor图形处理器上带有十字准线的数字转换器。
PointerDevice.AllPointerTypes任何类型的指向设备。

另请参阅 QPointingDevice::PointerType


active : bool [read-only]

只要该输入处理程序成功独占一个或多个eventPoints 点,就会保持true 。这意味着该处理程序会根据这些事件点的移动更新其属性,并积极操作其target (如有)。


blocking : bool [since 6.3]

该处理程序是否阻止其后面的其他项目或处理程序同时悬停。该属性默认为false

此属性在 Qt 6.3 中引入。


cursorShape : Qt::CursorShape

hoveredtrue 且无其他处理程序覆盖时,该属性将保存光标形状。

可用的光标形状有

  • 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

此属性的默认值未设置,这允许同一父项上的任何活动处理程序决定光标形状。通过将该属性设置为undefined ,可将其重置为初始状态。

如果任何已定义cursorShape 的处理程序为active ,则会出现该光标。否则,如果HoverHandler 已定义为cursorShape ,则会出现该光标。否则,将显示父项cursor

注意: 当该属性未设置或已设置为undefined 时,如果读取该值,将返回Qt.ArrowCursor

另请参阅 Qt::CursorShapeQQuickItem::cursor()。


enabled : bool

如果PointerHandler 被禁用,它将拒绝所有事件,也不会发出任何信号。


grabPermissions : flags

该属性指定了当该处理程序的逻辑决定接管独占抓取时,或当它被其他处理程序要求批准抓取接管或取消时的权限。

常数说明
PointerHandler.TakeOverForbidden此处理程序既不从任何类型的项目或处理程序获取抓取权限,也不向其提供抓取权限。
PointerHandler.CanTakeOverFromHandlersOfSameType该处理程序可以从另一个同类处理程序处获取独家抓取权限。
PointerHandler.CanTakeOverFromHandlersOfDifferentType此处理程序可以从任何类型的处理程序获取独占抓取权限。
PointerHandler.CanTakeOverFromItems该处理程序可以从任何类型的项目中获取独占抓取权。
PointerHandler.CanTakeOverFromAnything该处理程序可以从任何类型的项目或处理程序中获取独占抓取权。
PointerHandler.ApprovesTakeOverByHandlersOfSameType该处理程序允许其他同类处理程序使用抓取。
PointerHandler.ApprovesTakeOverByHandlersOfDifferentType该处理程序允许任何类型的处理程序抓取。
PointerHandler.ApprovesTakeOverByItems该处理程序允许任何类型的物品抓取。
PointerHandler.ApprovesCancellation此处理程序允许其抓取设置为空。
PointerHandler.ApprovesTakeOverByAnything该处理程序允许任何类型的项目或处理程序抓取。

默认值为PointerHandler.CanTakeOverFromItems | PointerHandler.CanTakeOverFromHandlersOfDifferentType | PointerHandler.ApprovesTakeOverByAnything ,允许大多数接管情况,但可避免两个 PinchHandler 争夺同一个触摸点。


hovered : bool [read-only]

只要任何指向设备(鼠标或平板电脑)的光标位于parent 项目的范围内,并由margin 扩展(如有),该值就会保持为真。


margin : real

parent 项目边界外的余量,在此范围内,eventPoint 可以激活此处理程序。例如,在PinchHandlertarget 也是parent )上,将其设置为至少为一般用户手指宽度一半的距离是非常有用的,这样,如果parent 已缩放至非常小的尺寸,仍可使用捏合手势。或者,如果基于TapHandler 的按钮放置在屏幕边缘附近,则可用于遵守菲茨定律:即使按钮与屏幕边缘在视觉上相差几个像素,也会对屏幕边缘的鼠标点击做出反应。

默认值为 0。


parent : Item

Item 是处理程序的作用域;也就是在其中声明的项目。处理程序将代表该 Item 处理事件,这意味着如果指针事件中至少有一个eventPoints 出现在该 Item 的内部,那么该指针事件就是相关的。最初target() 是相同的,但可以重新分配。

另请参阅 targetQObject::parent()。


point : handlerPoint [read-only]

当前正在处理的eventPoint 。如果当前没有正在处理的点,该对象将重置为默认值(所有坐标均为 0)。


target : Item

该处理程序将处理的项目。

默认情况下,它与parent 相同,即处理程序所声明的 Item。不过,有时也可以将目标设置为不同的项目,以便处理一个项目中的事件,但同时操作另一个项目;或者将目标设置为null ,禁用默认行为并执行其他操作。


信号文档

canceled(eventPoint point)

如果该处理程序已经抓取了给定的point ,那么当抓取被其他指针处理程序或项盗用时,就会发出该信号。

注: 相应的处理程序是onCanceled


grabChanged(PointerDevice::GrabTransition transition, eventPoint point)

当抓取发生与该处理程序相关的某种变化时,就会发出该信号。

transition (动词)说明发生了什么。point (对象)是被抓取或未被抓取的点。

transition 的有效值是

常量说明
PointerDevice.GrabExclusive该处理程序承担了处理point 的主要责任。
PointerDevice.UngrabExclusive该处理程序已放弃先前的独家抓取。
PointerDevice.CancelGrabExclusive该处理程序的独占抓取已被接管或取消。
PointerDevice.GrabPassive此处理程序已获得被动抓取,以监控point
PointerDevice.UngrabPassive此处理程序已放弃先前的被动抓取。
PointerDevice.CancelGrabPassive该处理程序之前的被动抓取异常终止。

注: 相应的处理程序是onGrabChanged


© 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.