JustinBlaber
JustinBlaber

Reputation: 4650

Qt mousemoveevent + Qt::LeftButton

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.

Upvotes: 6

Views: 8709

Answers (3)

felixgaal
felixgaal

Reputation: 2423

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!

Upvotes: 10

cmannett85
cmannett85

Reputation: 22346

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.

Upvotes: 14

ScarCode
ScarCode

Reputation: 3094

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);

Upvotes: 1

Related Questions