mouseMoveEvent 函数中判断鼠标那个按键按下

我们可以用

if (event->button() == Qt::LeftButton)
{

TODO:

}

来判断鼠标那个键按下,但是在mouseMoveEvent函数中,event->button()总是返回NoButton,这让这个判断完全失去了意义,

经查找,发现大家都是用这个

if (event->buttons() & Qt::LeftButton)
{
TODO:
}

来代替,需要注意的是event->button()与event->buttons()的不同(有一个后面多了个S)

那么这两个函数有什么区别呢

http://www.cnblogs.com/Jace-Lee/p/5869170.html 

Qt中buttons()和button()的区别,官方解析如下:

Qt::MouseButton QMouseEvent::button() const

返回产生事件的按钮

 

Qt::MouseButton QMouseEvent::buttons() const

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

假设鼠标左键已经按下,
如果移动鼠标,会发生的move事件,button返回Qt::NoButton,buttons返回LeftButton。
再按下了右键,会发生press事件,button返回RightButton,buttons返回LeftButton | RightButton
再移动鼠标,会发生move事件,button返回Qt::NoButton,buttons返回LeftButton | RightButton
再松开左键,会发生Release事件,button返回LeftButton,buttons返回RightButton
也就是说,button返回“哪个按钮发生了此事件”,buttons返回"发生事件时哪些按钮还处于按下状态"

http://blog.csdn.net/guochang7511/article/details/25620221

常用按钮值

 NoButton         = 0x00000000,
   LeftButton       = 0x00000001,
        RightButton      = 0x00000002,
        MidButton        = 0x00000004, // ### Qt 6: remove me
        MiddleButton     = MidButton,
 按下左键时返回1

右键 2

中键 4

左 + 右 3(LeftButton|RightButton 左键按位或上右键)

左 + 中 5

右 + 中 6 

左 + 中 + 右 7

if (event->buttons() & Qt::LeftButton) 是判断左键是否按下了,只要左键按下了就返回真,(也可能右键也同时按下了)

if (event->buttons() & Qt::LeftButton & Qt::LeftButton)是判断只有左键按下

if ((event->buttons() & Qt::LeftButton) == Qt::LeftButton)是判断只有左键按下


猜你喜欢

转载自blog.csdn.net/qq_30126571/article/details/78465607
今日推荐