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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With