Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 711
JustinBlaber Avatar asked May 28 '12 03:05

JustinBlaber


3 Answers

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.

like image 180
cmannett85 Avatar answered Nov 11 '22 07:11

cmannett85


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!

like image 31
felixgaal Avatar answered Nov 11 '22 06:11

felixgaal


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);
like image 1
ScarCode Avatar answered Nov 11 '22 07:11

ScarCode