PDA

View Full Version : QGraphicsPixmapItem to QGraphicsScene



strateng
26th March 2010, 02:16
Hey,

I've been reading through some example coding online such as the following:




scene = new QGraphicsScene;
QPixmap pixmap = QPixmap::fromImage(image);
QGraphicsPixmapItem pi;
pi.setPixmap (pixmap);
scene.addItem(pi);



The problem I'm having is that it does not compile with the following error "error: no matching function for call to ‘QGraphicsScene::addItem(QGraphicsPixmapItem&)".

Thanks,
Strateng

aamer4yu
26th March 2010, 04:21
From the docs - void QGraphicsScene::addItem ( QGraphicsItem * item )
addItem takes a pointer, not a reference. Also its better to create the item on heap rather than on stack. Because once the scope goes out, your item will get deleted and erased from the scene !

strateng
26th March 2010, 04:28
Hey,

Oh I never knew that but I'm not sure as to how you mean create the item on a heap. Would you be able to provide me with an example coding?

Thanks,
Strateng


From the docs - void QGraphicsScene::addItem ( QGraphicsItem * item )
addItem takes a pointer, not a reference. Also its better to create the item on heap rather than on stack. Because once the scope goes out, your item will get deleted and erased from the scene !

aamer4yu
26th March 2010, 04:45
Creating a object on heap means creating with new operator.
So instead of QGraphicsPixmapItem pi; you do something like - QGraphicsPixmapItem *pi = new QGraphicsPixmapItem();
If you are using QGraphicsPixmapItem pi; make sure the scope is well defined. ie. something like member variable.

strateng
26th March 2010, 04:55
Oh I thought I did that before but it seems I didn't. Thanks for the help.


Creating a object on heap means creating with new operator.
So instead of QGraphicsPixmapItem pi; you do something like - QGraphicsPixmapItem *pi = new QGraphicsPixmapItem();
If you are using QGraphicsPixmapItem pi; make sure the scope is well defined. ie. something like member variable.

JohannesMunk
27th March 2010, 01:50
Whats the problem with directly using



QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(img));


as we did before?

http://www.qtcentre.org/threads/29236-Displaying-a-QImage-through-pixels-and-QGraphicsView

Johannes