Results 1 to 6 of 6

Thread: Resizing QGraphicsView to fit entire contents of QGraphicsScene

  1. #1
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Resizing QGraphicsView to fit entire contents of QGraphicsScene

    Hi,

    I have a program which parses through a .txt file containing photo pathnames and then adds them as QGraphicsPixmapItems to a QGraphicsScene. This works correctly but I am unable to get the QGraphicsView to resize to the size of the pixmap so that the whole image is visible.

    To do this, I have created a class TrainingDataWindow which subclasses QMainWindow. The class contains a function selectImageSource() that correctly obtains the first pixmap from the .txt file and adds it to the QGraphicsScene. In this function, I have tried multiple ways of calling QGraphicsView::resize(....) in order to make the whole image visible. These methods include:

    1.
    Qt Code:
    1. view_image->resize(imgScene.itemsBoundingRect().width(),imgScene.itemsBoundingRect().height());
    To copy to clipboard, switch view to plain text mode 

    2.
    Qt Code:
    1. QSize imgSize = img.size();
    2. view_image->resize(imgSize);
    To copy to clipboard, switch view to plain text mode 

    where view_image is of type QGraphicsView and img is of type QPixmap. Neither of these (or others) have done anything (literally).

    Any ideas on how to fix this? If i do need to reimplement resizeEvent() for the TrainingDataWindow class how would this be affected since I designed the program with Qt Designer?

    Thanks,
    Max

  2. #2
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resizing QGraphicsView to fit entire contents of QGraphicsScene

    Just in case the above post was unclear, my goal is to get qgraphicsview to act like a qlabel when a pixmap is added to it. i.e. automatically change size to fit the entirety of the image. QLabel does this but QGraphicsView does not.

    Just in case the above post was unclear, my goal is to get qgraphicsview to act like a qlabel when a pixmap is added to it. i.e. automatically change size to fit the entirety of the image. QLabel does this but QGraphicsView does not.


    Added after 1 36 minutes:


    To further explain my problem here are some screenshots of my program. The grey region in the initial pic is a qlabel and the white rectangular region under it is a qgraphicsview:

    Screen Shot 2012-09-05 at 12.53.02 PM.png

    When I add an image to the qlabel it resizes to fit the entire pixmap where as the qgraphicsview does not. This next photo illustrates this. The only reason the qgraphicsview seems to change size is because the the resizing of the qlabel forced the view to resize:

    Screen Shot 2012-09-05 at 12.51.23 PM.jpg

    Any ideas on how to get the QGraphicsView to resize like the Qlabel?
    Last edited by mgoldman; 5th September 2012 at 18:58.

  3. #3
    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: Resizing QGraphicsView to fit entire contents of QGraphicsScene

    I think you have your thinking a bit backwards. The view is just the window onto which the items in the QGraphicsScene are visualized. So you don't change the size of the view to fit the scene, you scale the scene (and the items in it) to fit the view. Try this method: QGraphicsView::fitInView() (the version that takes the QGraphicsItem pointer) using the pointer to your pixmap item.

    If your QLabel is squeezing out the view when you load a new image, this is a problem with the way you have specified the layout (and constraints thereon) in your main widget, not a problem with the QGraphicsView.

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

    mgoldman (7th September 2012)

  5. #4
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resizing QGraphicsView to fit entire contents of QGraphicsScene

    I'm new to Qt and its become increasingly clear that what you're saying is the intended usage of QGraphicsView where as my vision of the view resizing based on QgraphicsScene's contents is not.

    However, my program requires something somewhat close to that functionality so let me explain my program briefly and maybe you or other will have some ideas.

    The program is intended to be used for adaboost classification which is one means of achieving object detection. For instance, say I want to train a computer to detect human eyes. I would create a file with thousands of faces and upload them into this program. For each photo I would draw rectangles around the eye region. This would then update an xml file which would contain the information on the photo and the "cropped regions" that were drawn on them. These positive samples would then be used to train the adaboost classifier.

    However, the problem is that each of the face photos may be a different size but I need the view to hold the entire image.

    I am worried that, if I use fitInView(), the image may become distorted if it is forced to become too much larger or smaller even if I keep the aspect ratio constant. Also, this would only change the image visible in the qt program not the actual photo in my file so how would the coordinate system translate to the actual photo since where I draw the rectangles in the program is what gets updated in the xml file?

    Any ideas on how I can use QGraphicsView to obtain something that meets these needs? Is there a way to reimplement resizeEvent for QGraphicsView such that it resizes when the contents of QGraphicsScene changes?
    Last edited by mgoldman; 6th September 2012 at 20:41.

  6. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Resizing QGraphicsView to fit entire contents of QGraphicsScene

    Maybe I misunderstand, but why not set the minimum size of the view to the size of the largest pixmap you have, then center each image in the view when it is added.

    The way you are approaching it, wouldn't your controls move each time you display a different size image?

  7. The following user says thank you to norobro for this useful post:

    mgoldman (7th September 2012)

  8. #6
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resizing QGraphicsView to fit entire contents of QGraphicsScene

    Yes, the buttons would all move when uploading an image but this application is primarily for use in my research lab so that isn't the biggest concern for us. However, I think you guys are right. I think the best way to do this is to set some arbitrary minimum size for the view and use fitInView() to make all images fit the view.

    Thanks guys.

Similar Threads

  1. Replies: 1
    Last Post: 8th September 2012, 05:08
  2. Replies: 2
    Last Post: 12th August 2011, 22:50
  3. QTableView resizing to contents.
    By chris_helloworld in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2010, 13:43
  4. QGraphicsView and showing entire item in it
    By T4ng10r in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2010, 11:44
  5. QToolBox Resizing to contents??
    By kerwim in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2008, 08:28

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.