Qt mousemoveevent + Qt :: LeftButton

A quick question is why:

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

returns 0 instead of 1 when I hold the left mouse button while moving the cursor around in the graph. In any case, to return this value to 1, I can tell when the user drags the mouse over the graph. Thanks.

+4
source share
3 answers

Although Spyke's answer is correct, you can simply use buttons() ( docs ). button() returns the mouse button that raised the event, therefore returns Qt::NoButton ; but buttons() returns the buttons held at the start of the event that you need.

+8
source

You can find out if the left button is pressed by looking at the buttons property:

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

Hope this helps!

+6
source

The return value is always Qt :: NoButton for mouse move events . You can use an event filter to solve this problem.

try it

 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 do not forget to set this event filter in mainwindow.

 qApplicationobject->installEventFilter(this); 
+1
source

Source: https://habr.com/ru/post/1414662/


All Articles