PDA

View Full Version : QGrapghicsView updating



hayzel
21st October 2009, 12:14
Here is a simple re-implementation of mouse events for a QGraphicsView:


void kadeView::mousePressEvent (QMouseEvent * event )
{
if (event->button() == Qt::MidButton) //pan
{
isPanning=1;
saved_cursor=viewport()->cursor();
setCursor(Qt::ClosedHandCursor);
mouse_pos=mapToScene(event->pos());
event->accept();
return;
};
QGraphicsView::mousePressEvent(event);
};

void kadeView::mouseMoveEvent ( QMouseEvent * event )
{
if(isPanning==1)
{
QPointF delta(mouse_pos-mapToScene(event->pos()));
QRectF r1(sceneRect());
r1.translate(delta.x(),delta.y());
setSceneRect(r1);
//translate(delta.x(),delta.y());
//ensureVisible(pan_rect.x()-line.dx(),pan_rect.y()-line.dy(),pan_rect.width(),pan_rect.height());
//centerOn(pan_rect.center().x()-line.dx(),pan_rect.center().y()-line.dy());
event->accept();
return;
};
QGraphicsView::mouseMoveEvent(event);
};

void kadeView::mouseReleaseEvent ( QMouseEvent * event )
{
if(isPanning==1)
{
isPanning=0;
setCursor(saved_cursor);
event->accept();
return;
};
QGraphicsView::mouseReleaseEvent(event);
};

It pans the view on middle mouse click and drag.
Can someone explain me why the only command that works is the setSceneRect() in mouseMoveEvent, and not the commented ones?
If I uncomment and replace setSceneRect with any of the translate(), centerOn(), ensureVisible() nothing is happening in the view...

Thank you in advance

scascio
21st October 2009, 14:27
Why are you changing the scene rect?
Don't you need QGraphicsView::fitInView instead?

Be carefut that QGraphicsView::sceneRect does not returns the visible rect in the view.

I have same needs and I didn't find better to get it than :

mapToScene(viewport()->rect())

How is update your variable pan_rect?

Be aware than when you code will work, the return coordinates of
mapToScene(event->pos())
will have same value in mousePress and mouseMove,
by definition of the pan : the pixel coordinates change, but scene coordinates are attach to the mouse!!

And last but not least : are you aware of QGraphicsView::setDragMode with QGraphicsView::ScrollHandDrag?

hayzel
21st October 2009, 14:38
The problem is not the coordinates. All the coords are checked to be fine.

The problem is that the calls
translate()
or
centerOn()
didn't move the view...
For example If I put in mouseMoveEvent the call translate(10,10) it will not move the view.
The only call that I have found to move the view is the setSceneRect(), although in documentation it implies that this is not the view piece of the scene, it's the entire scene. In practise it seems to be the viewable rect of the scene.

Am I missing something about the QGraphicsView structure?
When I call translate, rotate etc etc , view is not updated? Why? And how can I do it?

I am at the begining of a project. It's my first use of QGraphicsView/Scene and I have to clear out all the above, before I move on with my work...

scascio
21st October 2009, 14:50
Ok I think I see what is happening

You cannot set the view of the scene outside the sceneRect.
The result of the view changes will be block to the closest part of the rect scene.

That is why, when you change the scene rect, you can move.

Try to set your scene Rect with big values at initialization and dont call sceneRect in mouse event

hayzel
21st October 2009, 14:54
You 're great!
I will try it right away!
Thank you, I lost 1 or 2 days just for that :-)