QHoverEvent Class

QHoverEvent 类包含描述鼠标事件的参数。更多

头文件: #include <QHoverEvent>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui
继承: QSinglePointEvent

公共函数

QHoverEvent(QEvent::Type type, const QPointF &scenePos, const QPointF &globalPos, const QPointF &oldPos, Qt::KeyboardModifiers modifiers = Qt::NoModifier, const QPointingDevice *device = QPointingDevice::primaryPointingDevice())
QPoint oldPos() const
QPointF oldPosF() const

详细说明

当鼠标光标移入、移出或在部件内移动时,如果部件具有Qt::WA_Hover 属性,就会发生鼠标事件。

函数 pos() 会给出当前光标的位置,而oldPos() 则会给出鼠标的旧位置。

事件QEvent::HoverEnterQEvent::HoverLeave 与事件QEvent::EnterQEvent::Leave 有一些相似之处。不过,它们略有不同,因为我们在 HoverEnter 和 HoverLeave 的事件处理程序中执行了 update()。

QEvent::HoverMove 与 也略有不同。让我们考虑一个包含子窗口 B 的顶层窗口 A,子窗口 B 又包含子窗口 C(所有窗口都启用了鼠标跟踪功能):QEvent::MouseMove

现在,如果将光标从 A 中间的顶部移到底部,就会发生以下QEvent::MouseMove 事件:

  1. A::MouseMove
  2. B::MouseMove
  3. C::MouseMove

QEvent::HoverMove 也会收到相同的事件,只是无论事件是否被接受,事件都会传播到顶层。它只会在Qt::WA_NoMousePropagation 属性下停止传播。

在这种情况下,事件将以下列方式发生:

  1. A::HoverMove
  2. A::HoverMove, B::HoverMove
  3. A::HoverMove, B::HoverMove, C::HoverMove

成员函数文档

QHoverEvent::QHoverEvent(QEvent::Type type, const QPointF &scenePos, const QPointF &globalPos, const QPointF &oldPos, Qt::KeyboardModifiers modifiers = Qt::NoModifier, const QPointingDevice *device = QPointingDevice::primaryPointingDevice())

构造一个源自device 的悬停事件对象。

type 参数必须是QEvent::HoverEnterQEvent::HoverLeaveQEvent::HoverMove

scenePos 是当前鼠标光标相对于接收窗口或场景的位置,oldPos 是其之前的位置,globalPos 是绝对坐标中的鼠标位置。modifiers 保存事件发生时所有键盘修改器的状态。

QPoint QHoverEvent::oldPos() const

返回鼠标光标相对于接收事件的部件的前一个位置。如果没有前一个位置,oldPos() 将返回与 pos() 相同的位置。

QEvent::HoverEnter 事件中,该位置将始终是QPoint(-1,-1)。

另请参见 pos().

QPointF QHoverEvent::oldPosF() const

返回鼠标光标相对于接收事件的 widget 的前一个位置。如果没有前一个位置,oldPosF() 将返回与 posF() 相同的位置。

QEvent::HoverEnter 事件中,该位置将始终是QPointF(-1,-1)。

另请参见 posF().

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