1

I created my own classes (view and scene) to display image and objects I added to it, even got zoom in/out function implemented to my view, but now I have to add new functionality and I don't even know how to start looking for it.

  • Whenever I press the scroll button of my mouse and hold it - I wish to move around the scene, to see different parts of it - just like I would with sliders. It is supposed to be similar to any other program allowing to zoom in/out to image and move around zoomed picture to see different parts of it.

Unfortunately - I don't even know how to look for some basic, because "moving" and similar refer to dragging objects around.

EDIT 1

void CustomGraphicView::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::MidButton)
    {
        setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
        translate(event->x(),event->y());
    }
}

Tried this - but it is working in reverse.

Thalia
  • 13,637
  • 22
  • 96
  • 190
Arker
  • 169
  • 2
  • 14
  • Please add meaningful code and a problem description here. Posting a [Minimal, Complete, and Verifiable example (MCVE)](http://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. For more info, see [Something on my web site doesn't work. Can I just paste a link to it?](http://meta.stackexchange.com/questions/125997/) Thanks! – Raju Mar 08 '16 at 10:45
  • MY problem is that i dont know how to call that. – Arker Mar 08 '16 at 14:32
  • It is answered here : http://stackoverflow.com/questions/4753681/how-to-pan-images-in-qgraphicsview – vcp Mar 08 '16 at 15:05

1 Answers1

5

I suppose you know how to handle events using Qt.

So, to translate (move) your view use the QGraphicsView::translate() method.

EDIT

How to use it:

void CustomGraphicsView::mousePressEvent(QMouseEvent* event)
{
    if (e->button() == Qt::MiddleButton)
    {
        // Store original position.
        m_originX = event->x();
        m_originY = event->y();
    }
}

void CustomGraphicsView::mouseMoveEvent(QMouseEvent* event)
{
    if (e->buttons() & Qt::MidButton)
    {
        QPointF oldp = mapToScene(m_originX, m_originY);
        QPointF newP = mapToScene(event->pos());
        QPointF translation = newp - oldp;

        translate(translation.x(), translation.y());

        m_originX = event->x();
        m_originY = event->y();
    }
}
Tomas
  • 2,170
  • 10
  • 14
  • Yes, i know. So i jsut have to use mouseEven in my GraphicView and call that function, yes? – Arker Mar 08 '16 at 14:44
  • I made my attempt -> Edit 1 – Arker Mar 08 '16 at 14:58
  • I tried your code - works simialr to my attempt, but still got same error - View move in opposite dirrection than my mouse does – Arker Mar 11 '16 at 07:43
  • Opposite in both x and y direction? – Tomas Mar 11 '16 at 09:01
  • be aware this overrides the default behavior of the parent for the events, so you might want to pass the event to the parent when the middleButton is not pressed. For example using ```QGraphicsView::mousePressEvent(event);``` – maxbachmann Aug 08 '19 at 08:12
  • 3
    in my case, also `setTransformationAnchor(QGraphicsView::NoAnchor)` was required to make things work. – Nikolai Saiko Jun 01 '20 at 20:22