PDA

View Full Version : QGraphicsView issues with panning and scaling



almo
9th May 2012, 18:54
Hi!

I'm coming from experience with Cocoa and Winforms, and I'm just starting with Qt. I have set up a subclass of QGraphicsView. I'll show the relevant functions below. The issue I'm having is twofold:

If I click item 1 and drag, item 2 moves. If I click item 2 and drag, item 1 moves. :confused:

If I use the mousewheel to zoom, the mouseMoveEvent starts being called whenever the mouse moves even if a button is not held.

I've been staring at this for over an hour, having tried different things, like forcing mouseTracking to false at the end of the wheelevent, which didn't change anything.


EditorView::EditorView(QWidget* parent) :
QGraphicsView(parent)
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

QGraphicsScene* scene = new QGraphicsScene(this);
setScene(scene);
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);

QGraphicsEllipseItem* item1 = new QGraphicsEllipseItem(0, scene);
item1->setRect(250.0, 250.0, 50.0, 50.0);
item1->setFlag(QGraphicsItem::ItemIsMovable);

QGraphicsEllipseItem* item2 = new QGraphicsEllipseItem(0, scene);
item2->setRect(150.0, 150.0, 50.0, 50.0);
item2->setFlag(QGraphicsItem::ItemIsMovable);

setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
}

void EditorView::mousePressEvent(QMouseEvent* event)
{
QGraphicsView::mousePressEvent(event);

m_lastDragPos = event->pos();
}

void EditorView::mouseMoveEvent(QMouseEvent* event)
{
QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
PanView(delta);
m_lastDragPos = event->pos();
}

void EditorView::wheelEvent(QWheelEvent* event)
{
int delta = event->delta() / 120;
ScaleView(delta);
}

void EditorView::PanView(QPointF delta)
{
QPoint viewCenter(viewport()->width() / 2 - delta.x(), viewport()->height() / 2 - delta.y());
QPointF newCenter = mapToScene(viewCenter);
centerOn(newCenter);
}

void EditorView::ScaleView(int delta)
{
bool zoomIn = (delta > 0);
delta = abs(delta);
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
for(int i = 0; i < delta; ++i)
{
if(zoomIn)
{
scale(1.10, 1.10);
}
else
{
scale(0.90, 0.90);
}
}
setTransformationAnchor(oldAnchor);
}

almo
10th May 2012, 19:47
Quite a few people looked in here, thanks for that. So I'll post what was going on now that I've figured it out.

It turns out in mousePressEvent I was calling the superclass version of that, and it was triggering the default panning. So clicking item1 and dragging moved item1, but panned at the same time. So I would see item2 move, and item1 stay still. I figured this out by adding item3, and I'd see both items 2 and 3 moving when click-dragging item1.

About the attaching to the cursor, it seems to have been related to the use of the AnchorUnderMouse thing. Setting it off that at the bottom of the function does not appear to have worked properly. I've changed how the zoom-while-keeping-the-point-under-the-mouse-still thing works, and that part is fixed as well.

Now to get all this working AND get the default dragging behavior from the items, which I have now lost. :)