Results 1 to 5 of 5

Thread: Auto scale a QGraphicsView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Auto scale a QGraphicsView

    Hi,

    I have a QGraphicsScene containing a few items. The corresponding QGraphicsView has no scrollbar so you have to use the mouse to view items which are not visible on the screen.

    I would like to create a button which, when clicked, centers the scene and scales it in order to view all items at a time.

    What's the best way to achieve that?


    Regards,

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,102
    Thanks
    289
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Auto scale a QGraphicsView

    Qt Code:
    1. QGraphicsView::fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
    To copy to clipboard, switch view to plain text mode 

    Answered less than a week ago in another post. Using the forum's search feature could save you a lot of time waiting for a reply.

  3. The following user says thank you to d_stranz for this useful post:

    niluje (5th July 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Auto scale a QGraphicsView

    Thanks a lot for your time.
    I used scene()->itemsBoundingRect() instead of ->sceneRect(), but every works fine now.

    I'm sorry for this topic which seems to be recurrent. I searched on google and here, and I couldn't find anything. Bad keywords I guess !

    Thanks again,

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,102
    Thanks
    289
    Thanked 844 Times in 831 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Auto scale a QGraphicsView

    See the docs - using itemsBoundingRect() could be an expensive operation if there are many items. If you are doing this during a resize event, then resizing might have bad performance problems since the scene would have to calculate the bounding rect for you, then transform everything to redraw it in the new size. Scene rect is basically a constant, so the scene only needs to calculate what is needed for the resize.

    If your scene contents are mostly static (don't change, don't move around), then after populating the scene, you could set the scene rect to the itemsBoundingRect() and avoid the calculations.

  6. #5
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Auto scale a QGraphicsView

    I understood that there itemsBoundingRect() is an expensive operation.
    But my scene is often changing and there isn't so much items in it: one hundred for, and later maximum 10 000. That's not *so* much is it?

Similar Threads

  1. QwtPlot: how to auto-scale two Y scales?
    By paradiza in forum Qwt
    Replies: 4
    Last Post: 24th March 2009, 01:28
  2. 4 axis auto scale..
    By Vincenzo in forum Qwt
    Replies: 0
    Last Post: 22nd March 2009, 01:12
  3. [QGraphicsView] Auto adjusting QGraphicsScene
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2009, 22:31
  4. QGraphicsView scale question.
    By hickscorp in forum Qt Programming
    Replies: 20
    Last Post: 15th April 2007, 02:28
  5. QGraphicsView scale function
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2006, 14:05

Tags for this Thread

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.