Qt mousemoveevent + Qt :: LeftButton

Jus*_*tin 5 qt mouseevent qgraphicsscene

快速问题,为什么:

void roiwindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ 
    QGraphicsScene::mouseMoveEvent(event);
    qDebug() << event->button();
}
Run Code Online (Sandbox Code Playgroud)

当我在图形中移动光标的同时按住鼠标左键时,返回0而不是1.反正有没有让它返回1所以我可以告诉用户何时在图形切片上拖动鼠标.谢谢.

cma*_*t85 11

虽然Spyke的答案是正确的,但你可以使用buttons()(docs). button()返回导致事件的鼠标按钮,这就是它返回的原因Qt::NoButton; 但是buttons()在事件被触发时返回按下的按钮,这就是你所追求的.


Fèl*_*lué 7

您可以通过查看buttons属性来了解是否按下了左键:

if ( e->buttons() & Qt::LeftButton ) 
{
  // left button is held down while moving
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!