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
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