Qt mousemoveevent + Qt::LeftButton
Asked Answered
T

3

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.

Tortoiseshell answered 28/5, 2012 at 3:8 Comment(0)
P
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.

Pleura answered 28/5, 2012 at 7:15 Comment(0)
I
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!

Isomerous answered 28/5, 2012 at 7:29 Comment(1)
Ah, I read cbambers response first but I appreciate the post.Tortoiseshell
S
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);
Scabious answered 28/5, 2012 at 4:25 Comment(1)
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.Tortoiseshell

© 2022 - 2024 — McMap. All rights reserved.