In ScrollHandDrag mode of QGraphicsView, How to stop movement of QGraphicsItems on scene?
Asked Answered
M

2

7

I have multiple QGraphicsItems in scene spread across different parts of scene. In application there are different modes in one of mode user can scroll the scene (palm drag mode). To achieve scrolling over scene I set dragMode of QGraphicsView to ScrollHandDrag.

But the problem is when user try to scroll over scene by dragging (MousePress and MouseMove) on any of QGraphicsItem instead of scrolling scene it moves QGraphicsItem.

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

Any Solution or any pointers will help.

NOTE : There are very large number of QGraphicsItems and are of various type. So It is not possible to install event filter on QGraphicsItems.

Mirellamirelle answered 17/10, 2012 at 6:12 Comment(0)
O
6

Instead of modifying the item flags I set the whole view not interactive while in ScrollHandDrag Mode. The problem is, that you need to have an additional interaction type (i.e. Control Key, Other Mouse Button etc) to enable it.

setDragMode(ScrollHandDrag);
setInteractive(false);
Osuna answered 22/4, 2013 at 15:7 Comment(0)
M
0

Solved !!

Please refer Question I asked on 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 );

}
Mirellamirelle answered 17/10, 2012 at 13:22 Comment(5)
I did this by checking in each QGraphicsItem subclass - checking in the view is a much nicer way. But I do wonder if that could cause any other side effects?Irreclaimable
This seems to not work, for example if ( dragMode() != QGraphicsView::ScrollHandDrag ) { QGraphicsView::mouseReleaseEvent( aEvent ); } means that dragging around the view won't work anymore as it can't get the events??Irreclaimable
You always have to pass mouse event to parent view. But before passing this events check if there is any GraphicsItem present at clicked position set its ITEMISSELECTABLE and ITEMISMOVABLE flags to false. And after that restore flags to previous state. MousePressEvent{ 1. // Check/Get item at clicked position 2. // Disable selectable and movalble flag 3. parent::mousePressEvent(..) 4. // Restore flags of items }Mirellamirelle
Does this still work for you? In Qt 4.8 it seems that I can never make the items movable again with the above code? Perhaps a Qt bug?Irreclaimable
Yes its still working with Qt 4.8. You can make items movable again.Mirellamirelle

© 2022 - 2024 — McMap. All rights reserved.