[Interest] QGraphicsView stays big

Waitman Gobble uzimac at da3m0n8t3r.com
Thu Jun 7 15:22:56 CEST 2012


Andre Haupt <andre at bitwigglers.org> wrote ..
> On Thu, Jun 07, 2012 at 05:27:27AM -0700, Waitman Gobble wrote:
> > Till Oliver Knoll <till.oliver.knoll at gmail.com> wrote ..
> > > 2012/6/7 Waitman Gobble <uzimac at da3m0n8t3r.com>:
> > > > ...
> > > > When I load an image into QGraphicsScene and add the scene to QGraphicsView,
> > > the QGraphicsView expands to image size, but if I clear and load another image
> > > which is smaller, it stays the same size.
> > > 
> > > That is by design and documented:
> > > 
> > >   http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#sceneRect-prop
> > > 
> > > "...largest bounding rect of all items on the scene since the scene
> > > was created (i.e., a rectangle that grows when items are added to or
> > > moved in the scene, but never shrinks)."
> > > 
> > > Try setting it to a "null" QRectF after removing the previous and
> > > loading the new image: to my understanding the next call to
> > > sceneRect() should then re-calculate the bounding rectangle, which
> > > should then be just as large as the new image item in the scene.
> > > 
> > > Cheers, Oliver
> > > _______________________________________________
> > > Interest mailing list
> > > Interest at qt-project.org
> > > http://lists.qt-project.org/mailman/listinfo/interest
> > 
> > Thank you.
> > 
> > This seems to work:
> > 
> > QPixmap pm(path);
> > gs->clear();
> > gs = new QGraphicsScene(this); //new
> > gs->addPixmap(pm);
> > gv->setScene(gs);
> > gv->update();
> 
> That creates a memory leak every time you call that function.
> What Oliver meant is to call gs->setSceneRect(QRectF()) after clearing
> the scene and before adding a new pixmap to the scene. I have not tested
> this though.
> 
> regards,
> 
> Andre

Hi Andre,

yeah I suspect it does create a memory leak, but it does seem to solve my size issue with QGraphicsScene;


Per recommendation I've just tried this:

gs->clear();
gs->setSceneRect(QRectF());
gs->addPixmap(pm);

..and unfortunately this does not appear to resize (shrink) the QGraphicsScene.

I agree it's probably better to run without a memory leak, so I'll keep trying.

Thank you,

-- 
Waitman Gobble
San Jose California USA



More information about the Interest mailing list