Results 1 to 3 of 3

Thread: Adjusting window to QGraphicsView contents

  1. #1
    Join Date
    Apr 2007
    Posts
    10
    Thanks
    3

    Default Adjusting window to QGraphicsView contents

    In my application, I open a QMainWindow that has a QGraphicsView in order to display an image, but I have a problem as I would like to resize the QMainWindow to the size of the image displayed by the QGraphicsView. Can someone give me some guidelines about the way of doing it?

  2. #2
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adjusting window to QGraphicsView contents

    I think that what you need is http://doc.trolltech.com/4.3/qabstra...umViewportSize (just set the size of the main window to this+bars etc). I'm not sure, so just give it a try..

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adjusting window to QGraphicsView contents

    Do you want a once-only resize, or do you want the window to stay fixed at this new size? That is, do you want the user to be able to resize the window after your resize?

    Most of the time, when you want the viewport to assume a size you can rely on QGraphicsView's sizeHint(), which will default to the size of the scene. Otherwise, you can easily determine the size (bounding rect) of any item on the scene by going

    Qt Code:
    1. void CustomView::keyPressEvent(QKeyEvent *event)
    2. {
    3. // whenever someone hits a key, resize the view to an item's size.
    4. QRect itemRect = mapFromScene(pixmap->boundingRect()).boundingRect().toRect();
    5. resize(itemRect.size() + QSize(frameWidth() * 2, frameWidth() * 2);
    6. }
    To copy to clipboard, switch view to plain text mode 

    The pixmap boundingrect is a bit larger than the pixmap itself; depending on how you display the pixmap you could map just a QRectF(QPointF(), pixmapSize) instead.

    You can also go the opposite way; call QGraphicsView::fitInView() to make the item fit inside the view without actually resizing the view. Keep in mind that this approach uses transformations, though, slow on X11.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

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.