1

I have implemented the panning view on the QGraphicsView, using the mouse move event using

void View::mouseMoveEvent(QMouseEvent* event) {
pan();
QGraphicsView::mouseMoveEvent(event);
}

and in the scene of this view I have added few items where some of the items are resizable, so I implemented

void Item::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
 if(m_resizeMode)
    {
      resize();
      e->accept();
    }
}

I tried to filter the mouse move not to propagate to any further using e->accept() but my View mouseMove event has been called first , so when ever I tried to resize the item, the view started to pan all the way.

How can I avoid this event propagation from view to scene.

Wagmare
  • 1,354
  • 1
  • 24
  • 58
  • Call your view's base class implementation and check if the event is accepted after that. If not, then call `View::pan()`. – thuga Apr 20 '15 at 09:27
  • thanks for the reply . can you please help me in that .. how i can check the event is accepted ..? – Wagmare Apr 20 '15 at 09:29
  • 1
    call `event->isAccepted()`. – thuga Apr 20 '15 at 09:32
  • You might have to reject the event in `Item::mouseMoveEvent` if `m_resizeMode` is false, because I think the event will be accepted by default. Not sure if the scene will accept it so you will have to check that as well. – thuga Apr 20 '15 at 09:38
  • you mean to say i have to call event->ignore if resizemode failed ..? – Wagmare Apr 20 '15 at 09:41
  • Yes. Just add `else e->ignore();` in `Item::mouseMoveEvent` – thuga Apr 20 '15 at 09:42
  • No Thunga .it is always returning me true. so i did event->setAccepted(false); QGraphicsView::mouseMoveEvent(event); qDebug()<<"event is accepted:"<isAccepted(); and it is started giving me : false – Wagmare Apr 20 '15 at 09:46
  • Interesting. But yeah, works that way too. Though I think better approach would be to do these checks in the `mousePressEvent`. Check if the event is accepted the same way, and if not, set some bool value to `true`. Then you just check this bool in the `View::mouseMoveEvent`. Remember to set this bool value to `false` in the `mouseReleaseEvent`. – thuga Apr 20 '15 at 09:54
  • this method is now fine . im doing in it in the same way in mouse press event. thanks thunga .you can post it as a answer if you want . so the people with same problem in future can refer . – Wagmare Apr 20 '15 at 10:03
  • Ok, I posted an answer with an example how I do something similar in one of my projects. – thuga Apr 20 '15 at 10:12

2 Answers2

1

The view will always receive the mouse event first.

So, in the view, check to see if the mouse is over an item before allowing it to pan by getting the mouse pos in scene coordinates and retrieving the items at that position with QGraphicsScene::items( )

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • thanks Dark Knight. i feel this is also a good way.. but in worst case the view can be zoomed and only the item will be focussed . so in that tome the pan cannot be possible. – Wagmare Apr 20 '15 at 09:31
  • Alternatively, assuming that you're using the left mouse button for both actions, you could use the right mouse button for zooming, or simply the wheel of the mouse. Using the the left mouse button for both resizing an item and zooming sounds like an awkward interface design. – TheDarkKnight Apr 20 '15 at 09:32
1

You can call the base class implementation of the QGraphicsView and check if the event is accepted. However I would do this in the mousePressEvent instead of the mouseMoveEvent. After all this is where you determine if you should resize an item or do some panning. This is how I do something similar in my project:

void View::mousePressEvent(QMouseEvent *event)
{
    ...
    QGraphicsView::mousePressEvent(event);
    if(event->isAccepted())
        move = false;
    else
        move = true;
}

void View::mouseMoveEvent(QMouseEvent *event)
{
    if(!move)
    {
        QGraphicsView::mouseMoveEvent(event);
        return;
    }
    ... // you would do the panning here

    QGraphicsView::mouseMoveEvent(event);
}

void View::mouseReleaseEvent(QMouseEvent *event)
{
    if(!move)
    {
        QGraphicsView::mouseReleaseEvent(event);
        return;
    }
    else
    {
        ...
        move = false;
    }
    QGraphicsView::mouseReleaseEvent(event);
}
thuga
  • 12,601
  • 42
  • 52