PDA

View Full Version : QGraphicsView resize not centered



Alundra
7th November 2014, 03:13
Hi,
I have a QGraphicsView linked to a node graph, all works fine but one problem is there : the resize.
The resize works good but when you resize, the QGraphicsView is not centered anymore.
The behavior needed is to have the same view but with more space around.
Is it possible to have that ? How can it be achieved ?
Thanks for the help

EDIT :
Here one solution I have found if someone has the same problem, reimplement the resizeEvent of QGraphicsView :


void CEditorNodeView::resizeEvent( QResizeEvent* event )
{
// Base class.
QGraphicsView::resizeEvent( event );

// Get the old size.
const QSize OldSize = event->oldSize();

// Map to scene.
const QPointF CenterPos = mapToScene( OldSize.width() / 2, OldSize.height() / 2 );

// Center on the center pos.
centerOn( CenterPos );
}

d_stranz
7th November 2014, 03:56
You could also try calling this in your view constructor.


QGraphicsView::setResizeAnchor( QGraphicsView::AnchorViewCenter );

I don't know if it will have the effect you want, but you shouldn't need a resize event handler in that case.

Alundra
9th November 2014, 19:00
That gives the same effect, I have removed the resizeEvent to use "setResizeAnchor( QGraphicsView::AnchorViewCenter );".
Thanks for the tip.