How to make QGraphicsView / QGraphicsScene shrink to a minimum size

I am using QGraphicsView / QGraphicsScene to build in Qt. Scaling and fit work decently while I continue to draw big things. However, when the size of the rectangle rectangle decreases, the view does not. It just shows the same (or a large area).

Before rearranging the smaller scene in the view, I invoke the following commands:

mpScene->clear(); mpScene->setSceneRect(QRectF()); mpView->setSceneRect(QRectF()); mpView->resetMatrix(); mpView->fitInView(this->sceneRect(),Qt::KeepAspectRatio); 

Alas, the canvas shown by the view continues to grow. What am I missing?

The scene and presentation are configured as follows (for completeness only):

 mpView->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate); mpView->setCacheMode(QGraphicsView::CacheBackground); mpView->setScene(mpScene); mpView->setWindowFlags(Qt::FramelessWindowHint); mpView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); mpView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); mpView->setFrameStyle(QFrame::NoFrame); mpView->setAutoFillBackground(false); mpView->setAttribute(Qt::WA_OpaquePaintEvent, true); mpView->setAttribute(Qt::WA_NoSystemBackground, true); //enabling OpenGl if possible QGLFormat fmt = QGLFormat::defaultFormat(); fmt.setSampleBuffers(true); fmt.setDoubleBuffer(true); fmt.setSamples(256); fmt.setDirectRendering(true); QGLWidget* pGlWidget = new QGLWidget(fmt); if(pGlWidget->isValid()) mpView->setViewport(pGlWidget); else delete pGlWidget; mpView->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate); 

Note: Qt4.8 Version

+6
source share
1 answer

You can control the size of the main scene using setSceneRect . In my application, I have subclassed QGraphicsScene , and in the add / remove functions, I do this:

 setSceneRect(itemsBoundingRect()); 

This computes the new bounding box of the elements in the scene and forces the scene to this size.

+6
source

All Articles