Qt鼠标按钮
Qt Mouse Buttons
我正在使用Qt一段时间,有一些事情我正在做的工作,但我不明白为什么,像这样:
(event->buttons() & Qt::MiddleButton)
这将返回true
时,按下MiddleButton,但我的问题是语法,Qt说Qt::MiddleButton的值为4,所以作为一个布尔值将始终返回真,所以这意味着表达式等于这个:(event->buttons())
…这也不合逻辑……有人能解释一下吗?
From Qt docs
Qt::LeftButton 0x00000001 ---> 00000001b
Qt::RightButton 0x00000002 ---> 00000010b
Qt::MiddleButton 0x00000004 ---> 00000100b
最右边的列是二进制表示!
Left+Right --> buttons(): 3
Left+Middle --> buttons(): 5
.......
然后(buttons() & Qt::MiddleButton)
测试与MiddleButton相关的位是否设置
Left+Right --> 00000011 --> 00000011 & 00000100 = 00000000 --> FALSE
Left+Middle --> 00000011 --> 00000101 & 00000100 = 00000100 --> TRUE
问题是您将逻辑运算符&&
与位运算符和&
运算符混合在一起。它们不一样。例如
100 && 010 = True (both numbers are not 0)
100 & 010 = False (gives 0)
检查仅bool(event->buttons() == 0)
将给你true
,如果没有按钮按下和false
,如果任何按钮按下。要检查特定的按钮,需要使用按位'&'操作符。
相关文章:
- 如何通过拖动鼠标QT检查按钮?鼠标移动事件
- 以编程方式在任何按钮上突出显示 Qt 鼠标悬停按钮
- 单击wx切换按钮时不捕获鼠标
- 当鼠标越过按钮时,我可以更改按钮背景颜色吗?
- C Win32 API在按钮区域内通知鼠标
- 如何实现一致的方法来检测鼠标按钮被按住
- Qt按住鼠标按钮和计时器CPP
- SDL_MOUSEBUTTONUP甚至在释放鼠标按钮之前发生
- QT-随着鼠标悬停按钮的信息
- 将左鼠标按钮向上发送后,检测左键按钮
- 什么可能导致低级鼠标挂钩错过按钮启动事件?
- 当我将鼠标悬停在qt中的不同按钮上时,如何更改标签文本
- 当鼠标按钮按下时,WM_LBUTTONDOWN不会立即发送
- 如何从第三方程序中通过鼠标悬停获取按钮文本
- QCHARTVIEW,橡皮筋和右鼠标按钮行为
- 双击(触摸)按屏幕按钮的原因可能是什么,该按钮通过单击鼠标按下
- 模拟鼠标按钮按QT 5.3仅第一次工作
- 如何检测鼠标右键点击按钮
- 当我在C++/SFML中按下鼠标按钮时,如何画一个球
- SDL UI 菜单按钮 - 鼠标悬停