zoukankan      html  css  js  c++  java
  • Qt中关于QMouseEventbuttons()和QMouseEventbutton()的使用注意

    在进行QT程序开发中经常需要响应鼠标事件,在QWidget或QMainWindow的子类中可以重载如下鼠标事件实现自己需要的效果。

             virtual void

    mouseDoubleClickEvent(QMouseEvent *event)

    virtual void

    mouseMoveEvent(QMouseEvent *event)

    virtual void

    mousePressEvent(QMouseEvent *event)

    virtual void

    mouseReleaseEvent(QMouseEvent *event)

    QMouseEvent中的buttons()和button()返回按下的鼠标按键,是Qt:: MouseButton类型,包含Qt::NoButton、Qt::LeftButton、Qt:: RightButton、Qt:: MiddleButton等常见鼠标按键。

    返回值为以下几种类型:

    Qt::NoButton            0x00000000

    Qt::LeftButton          0x00000001

    Qt::RightButton        0x00000002

    Qt::MidButton          0x00000004

    我在mouseMoveEventmouseReleaseEvent事件中通过QMouseEvent:: button()获取鼠标按键信息,结果一直返回Qt::NoButton,非常纳闷!结果一查看Qt官方文档,针对这块有特别的解释。

            

    Qt::MouseButton QMouseEvent::button() const

    返回产生事件的按钮。

    需要注意的是,对于鼠标移动事件,该函数返回值总是Qt::NoButton

     

    Qt::MouseButtons QMouseEvent::buttons()const

    返回产生事件的按钮状态,函数返回当前按下的所有按钮,按钮状态可以是Qt::LeftButton, Qt::RightButton, Qt::MidButton 或运算组合。

    For mouse move events, this is all buttons that are pressed down. For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.

    鼠标左键已经处在按下的状态,此时如果移动鼠标,会产生鼠标的move事件,button()返回Qt::NoButton,buttons()返回LeftButton。再按下鼠标右键,会触发鼠标的press事件,button返回RightButton,buttons()返回LeftButton | RightButton再移动鼠标,会发生move事件,button()返回Qt::NoButton,button()s返回LeftButton | RightButton再松开左键,会发生Release事件,button()返回LeftButton,buttons()返回RightButton。

    简而言之,button返回“发生了此事件的按钮”,buttons返回“发生事件时还处于按下状态的按钮”。

    现在就明白了,在move事件中,就需要使用buttons()来获取还处于按下状态的按钮了!

     

    常用的判断条件(Event->buttons() & Qt::LeftButton)

    如果鼠标左键处于按下状态,则结果为真,如果不考虑左右键都处于按下状态,则 与event->buttons() == Qt::LeftButton在结果上是相同的。

    参考https://blog.csdn.net/liu_jiale1/article/details/83105226

  • 相关阅读:
    Html中列表显示
    Html中图片参数
    Html如何加入图片或超链接
    html常用标签
    mysql 几个坑
    现有.NET 开源框架浅析
    winform 界面加载慢原因分析
    FineUI 框架,RIA 富客户端应用的选择
    Hadoop Default Port
    docker issue
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10636126.html
Copyright © 2011-2022 走看看