Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force QGraphicsView/QGraphicsScene to shrink back to minimal size

Tags:

c++

qt

2d

I use QGraphicsView/QGraphicsScene for plotting in Qt. Scale and fit work decently as long as i keep plotting bigger things. However, when the boundign rectangle size reduces the View does not. It just shows the same (or a bigger area).

Before I replot a smaller scene on the view I call 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 still keeps on growing. What am I missing?

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

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: Version is Qt4.8

like image 679
Martin Avatar asked Dec 26 '13 21:12

Martin


1 Answers

You can control the size of the underlying scene via setSceneRect. In my application I've subclassed QGraphicsScene, and in the add/remove functions I do this:

setSceneRect(itemsBoundingRect());

This calculates the new bounding rectangle of the items in the scene, and forces the scene to that size.

like image 183
Chris Arguin Avatar answered Sep 26 '22 19:09

Chris Arguin