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,
再按下了右鍵,會發生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)是判斷只有左鍵按下