Results 1 to 3 of 3

Thread: QGraphicsView resize not centered

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsView resize not centered

    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 :
    Qt Code:
    1. void CEditorNodeView::resizeEvent( QResizeEvent* event )
    2. {
    3. // Base class.
    4. QGraphicsView::resizeEvent( event );
    5.  
    6. // Get the old size.
    7. const QSize OldSize = event->oldSize();
    8.  
    9. // Map to scene.
    10. const QPointF CenterPos = mapToScene( OldSize.width() / 2, OldSize.height() / 2 );
    11.  
    12. // Center on the center pos.
    13. centerOn( CenterPos );
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Alundra; 7th November 2014 at 02:26.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView resize not centered

    You could also try calling this in your view constructor.

    Qt Code:
    1. QGraphicsView::setResizeAnchor( QGraphicsView::AnchorViewCenter );
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView resize not centered

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

Similar Threads

  1. QGraphicsView resize and zoom
    By giostau in forum Qt Programming
    Replies: 0
    Last Post: 29th May 2013, 14:03
  2. Replies: 3
    Last Post: 11th December 2011, 10:09
  3. Replies: 7
    Last Post: 1st December 2011, 16:24
  4. QGraphicsView won't update until resize
    By stevel in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2009, 21:45
  5. getting QGraphicsView to resize
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 22nd January 2008, 03:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.