QMouseEvent Class
QMouseEvent 类包含描述鼠标事件的参数。更多内容...
| 头文件: | #include <QMouseEvent> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Gui) target_link_libraries(mytarget PRIVATE Qt6::Gui) |
| qmake: | QT += gui |
| 继承自: | QSinglePointEvent |
- 所有成员列表,包括继承的成员
- 已弃用的成员
- QMouseEvent 属于“事件类”范畴。
公共函数
| QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPointingDevice *device = QPointingDevice::primaryPointingDevice()) | |
| QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &scenePos, const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPointingDevice *device = QPointingDevice::primaryPointingDevice()) | |
| Qt::MouseEventFlags | flags() const |
受保护函数
| QMouseEvent(QMouseEvent &&) | |
| QMouseEvent & | operator=(QMouseEvent &&) |
| QMouseEvent & | operator=(const QMouseEvent &other) |
详细说明
当在小部件内按下或释放鼠标按钮,或者移动鼠标光标时,会发生鼠标事件。
除非已通过 `QWidget::setMouseTracking()` 启用了鼠标跟踪,否则鼠标移动事件仅在按下鼠标按钮时才会发生。
当小部件内部按下鼠标按钮时,Qt 会自动捕获鼠标;小部件将持续接收鼠标事件,直到最后一个鼠标按钮被释放为止。
鼠标事件包含一个特殊的接受标志,用于指示接收者是否需要该事件。如果鼠标事件未由您的控件处理,则应调用ignore()。鼠标事件会向上传播到父控件链,直到某个控件通过accept() 接受它,或者某个事件过滤器消耗它为止。
注意:如果 鼠标事件传播到已设置Qt::WA_NoMousePropagation 的widget ,则该鼠标事件将不会继续向上传播至父控件链。
可以通过调用从 `QInputEvent` 继承的 `modifiers()` 函数来获取键盘修饰键的状态。
position() 函数返回相对于接收鼠标事件的小部件或项的光标位置。如果因鼠标事件而移动小部件,请使用globalPosition() 返回的全局位置,以避免出现抖动现象。
QWidget::setEnabled() 函数可用于启用或禁用小部件的鼠标和键盘事件。
重写QWidget 事件处理程序,即QWidget::mousePressEvent()、QWidget::mouseReleaseEvent()、QWidget::mouseDoubleClickEvent()和QWidget::mouseMoveEvent(),以便在您自己的控件中接收鼠标事件。
另请参阅 QWidget::setMouseTracking()、QWidget::grabMouse() 和QCursor::pos()。
成员函数文档
QMouseEvent::QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPointingDevice *device = QPointingDevice::primaryPointingDevice())
构建一个源自device 的鼠标事件对象。
type 参数必须为QEvent::MouseButtonPress 、QEvent::MouseButtonRelease 、QEvent::MouseButtonDblClick 或QEvent::MouseMove 。
localPos 表示鼠标光标相对于接收控件或项目的相对位置。 光标在屏幕坐标系中的位置由globalPos 指定。窗口位置被设置为与localPos 相同的值。触发该事件的button 以Qt::MouseButton 枚举中的一个值表示。如果事件type 为MouseMove ,则该事件对应的按钮为Qt::NoButton 。buttons 表示事件发生时所有按钮的状态,modifiers 表示所有键盘修饰键的状态。
QMouseEvent::QMouseEvent(QEvent::Type type, const QPointF &localPos, const QPointF &scenePos, const QPointF &globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, const QPointingDevice *device = QPointingDevice::primaryPointingDevice())
构建一个鼠标事件对象。
type 参数必须为QEvent::MouseButtonPress 、QEvent::MouseButtonRelease 、QEvent::MouseButtonDblClick 或QEvent::MouseMove 。
参数localPos 、scenePos 和globalPos 分别指定鼠标光标相对于接收控件或项目、窗口以及屏幕或桌面的位置。
触发该事件的button 作为Qt::MouseButton 枚举中的一个值返回。如果事件type 为MouseMove ,则该事件对应的按钮为Qt::NoButton 。buttons 表示事件发生时所有按钮的状态,modifiers 表示所有键盘修饰键的状态。
[protected delete] QMouseEvent::QMouseEvent(QMouseEvent &&)
通过移动构造创建一个QMouseEvent 的实例。该函数已被删除。
Qt::MouseEventFlags QMouseEvent::flags() const
返回鼠标事件标志。
鼠标事件标志提供了有关鼠标事件的附加信息。
另请参阅 Qt::MouseEventFlag 和QGraphicsSceneMouseEvent::flags()。
[protected delete] QMouseEvent &QMouseEvent::operator=(QMouseEvent &&)
将other 按移动赋值方式赋值给此QMouseEvent 实例。该函数已被删除。
[noexcept protected default] QMouseEvent &QMouseEvent::operator=(const QMouseEvent &other)
将 `other ` 复制并赋值给此 `QMouseEvent ` 实例。
© 2026 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.