In ScrollHandDrag QGraphicsView mode, how do I stop the QGraphicsItems from moving in the scene?

I have several QGraphicsItem in the scene, spreading in different parts of the scene. The application has different modes in one of the modes, the user can scroll through the scene (palm drag mode). To achieve scrolling through the scene, I set dragMode from QGraphicsView to ScrollHandDrag .

But the problem is that the user is trying to scroll the scene by dragging ( MousePress and MouseMove ) onto any of the QGraphicsItem instead of scrolling the scene that he moves the QGraphicsItem .

How can I stop the QGraphicsItem movement and scroll the scene , but I still want to select QGraphicsItem s?

Any solution or any pointers will help.

NOTE. There are a very large number of QGraphicsItem and have a different type. Therefore, it is not possible to set an event filter on QGraphicsItem s.

+6
source share
2 answers

Instead of changing the flags of the elements, I found that the whole view is not interactive, in ScrollHandDrag mode. The problem is that its inclusion requires an additional type of interaction (for example, the Control key, Other Mouse Button, etc.).

 setDragMode(ScrollHandDrag); setInteractive(false); 
+7
source

Solved !!

Please refer to the question I asked on the Qt forum: Click here

Solution / Example:

 void YourQGraphicsView::mousePressEvent( QMouseEvent* aEvent ) { if ( aEvent->modifiers() == Qt::CTRL ) // or scroll hand drag mode has been set - whatever condition you like :) { QGraphicsItem* pItemUnderMouse = itemAt( aEvent->pos() ); if ( pItemUnderMouse ) { // Track which of these two flags where enabled. bool bHadMovableFlagSet = false; bool bHadSelectableFlagSet = false; if ( pItemUnderMouse->flags() & QGraphicsItem::ItemIsMovable ) { bHadMovableFlagSet = true; pItemUnderMouse->setFlag( QGraphicsItem::ItemIsMovable, false ); } if ( pItemUnderMouse->flags() & QGraphicsItem::ItemIsSelectable ) { bHadSelectableFlagSet = true; pItemUnderMouse->setFlag( QGraphicsItem::ItemIsSelectable, false ); } // Call the base - the objects can't be selected or moved by this click because the flags have been un-set. QGraphicsView::mousePressEvent( aEvent ); // Restore the flags. if ( bHadMovableFlagSet ) { pItemUnderMouse->setFlag( QGraphicsItem::ItemIsMovable, true ); } if ( bHadSelectableFlagSet ) { pItemUnderMouse->setFlag( QGraphicsItem::ItemIsSelectable, true ); } return; } } // --- I think This is not required here // --- as this will move and selects the item which we are trying to avoid. //QGraphicsView::mousePressEvent( aEvent ); } 
0
source

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


All Articles