View Full Version : Resizing QGraphicsView to fit entire contents of QGraphicsScene

5th September 2012, 07:14

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:


view_image->resize(imgScene.itemsBoundingRect().width(),imgSce ne.itemsBoundingRect().height());


QSize imgSize = img.size();

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?


5th September 2012, 19:58
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:


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:


Any ideas on how to get the QGraphicsView to resize like the Qlabel?

6th September 2012, 16:42
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.

6th September 2012, 20:41
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?

7th September 2012, 01:27
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?

7th September 2012, 03:42
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.