View Full Version : QGraphicsView resize not centered

7th November 2014, 03:13
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

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

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.

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