[Qt-interest] update qgraphicsscene

Murphy, Sean M. sean.murphy at gd-ais.com
Mon May 24 22:46:57 CEST 2010


You should never have to delete a QGraphicsPixmapItem yourself unless
you are explicitly calling QGraphicsScene::removeItem(), when you call
QGraphicsScene::addItem() the scene takes ownership of the item, so when
you delete the scene (or when Qt does it via the parent/child
mechanism), the item will get deleted.

 

http://doc.trolltech.com/4.6/qgraphicsscene.html#addItem

 

From: caius ligarius [mailto:caius9090 at gmail.com] 
Sent: Monday, May 24, 2010 4:21 PM
To: Murphy, Sean M.
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] update qgraphicsscene

 

Thanks it works. I was trying to "setSceneRect" using QGraphicsVIew
dimension and not QImage.

Yes, you are right this was meant to be pseudo-code and I am acutally
using QGraphicsPixmapItem and not QGraphicsItem - apologies!

Do you know I can delete QGraphicsPixmap item in the destructor?

Thanks,
Caius

On Mon, May 24, 2010 at 12:58 PM, Murphy, Sean M.
<sean.murphy at gd-ais.com> wrote:

> I have the need to update a QGraphicsView widget with a new QImage
everytime I call a certain function
> ("populateScene"). Currently I create  a new QGraphicsScene instance
everytime I call this function, is
> there some better way to update the scene without creating a new
instance of QGraphicsScene every time?
> (I am assuming this would slow things down and also I will not be able
to delete the QGraphicsScene
> pointer at the end since this would clear the image from the
viewport).  My code below-
>
> void populateScene(QImage* image)
> {
>   QGraphicsScene* scene = new QGraphicsScene;
>   QPixmap np = QPixmap::fromImage(*image);
>  
>   qGraphicsItem->setPixmap(np);
>   scene->addItem(qGraphicsItem);
>
>   qgraphicsView->setPixmap(np);
>  
> }

It looks like what you've got above is pseudo code since a few of your
lines won't compile.  QGraphicsItem doesn't have setPixmap(), that's
QGraphicsPixmapItem, and QGraphicsView also doesn't have a setPixmap()
function.

> qGraphicsItem: QGraphicsItem, initialized in the constructor

> qgraphicsView: QGRaphicsView, initialized in constructor

Anyways, what you want to do is move your scene initialization to the
constructor, as well as your QGraphicsPixmapItem initialization.  From
what you've told us, you'll only need one of each for the lifetime of
the application.

The just make populateScene be:

void populateScene(QImage* image)
{
 qGraphicsPixmapItem->setPixmap(QPixmap::fromImage(*image));
 scene->setSceneRect(0,0,image->width(), image->height());
}

You could add a qgraphicsView->centerOn(x,y) if you want to guarantee
the new image is centered.
Sean

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100524/ff45f038/attachment.html 


More information about the Qt-interest-old mailing list