我们可以用
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
如果移动鼠标,会发生的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返回”发生事件时哪些按钮还处于按下状态”
LeftButton = 0x00000001,
RightButton = 0x00000002,
MidButton = 0x00000004, // ### Qt 6: remove me
MiddleButton = MidButton,
按下左键时返回1
LeftButton|RightButton 左键按位或上右键)
if (event->buttons() & Qt::LeftButton) 是判断左键是否按下了,只要左键按下了就返回真,(也可能右键也同时按下了)
if (event->buttons() & Qt::LeftButton & Qt::LeftButton)是判断只有左键按下
if ((event->buttons() & Qt::LeftButton) == Qt::LeftButton)是判断只有左键按下
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!