6

Quick question, why does:

void roiwindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ 
    QGraphicsScene::mouseMoveEvent(event);
    qDebug() << event->button();
}

return 0 instead of 1 when I'm holding down the left mouse button while moving the cursor around in a graphicscene. Is there anyway to get this to return 1 so I can tell when a user is dragging the mouse across the graphicscene. Thanks.

JustinBlaber
  • 4,629
  • 2
  • 36
  • 57

3 Answers3

14

Though Spyke's answer is correct, you can just use buttons() (docs). button() returns the mouse button that caused the event, which is why it returns Qt::NoButton; but buttons() returns the buttons held down when the event was fired, which is what you're after.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
10

You can know if the left button was pressed by looking at the buttons property:

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

Hope that helped!

felixgaal
  • 2,403
  • 15
  • 24
1

The returned value is always Qt::NoButton for mouse move events. You can use Event filter to solve this.

Try this

bool MainWindow::eventFilter(QObject *object, QEvent *e)
{

 if (e->type() == QEvent::MouseButtonPress && QApplication::mouseButtons()==Qt::LeftButton)
 {
  leftbuttonpressedflag=true;
 }

  if (e->type() == QEvent::MouseMove)
 {
   QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
   if(leftbuttonpressedflag && mouseEvent->pos()==Inside_Graphics_Scene)
   qDebug("MouseDrag On GraphicsScene");
 }

 return false;

}

And also don't forget to install this event filter in mainwindow.

qApplicationobject->installEventFilter(this);
ScarCode
  • 3,074
  • 3
  • 19
  • 32
  • Thanks for the response. Even though this isnt the simplest solution I'm still going to look at how eventFilters work just in case I need it for some other potential problem I have. Thanks again. – JustinBlaber May 28 '12 at 13:33