PDA

View Full Version : Get a QGraphicsPixmapItem from a Scene and modify(change the image).



vitaR
21st March 2014, 21:10
Hi, I have to update every second (QTimer) the QGraphicsView, the QGraphicsView only have QGraphicsPixmapItems at specific positions in the whole frame.

I have to modify every time like five or seven images on this QGraphicsView, but I don't know how to do it.

Here's an example of my main code.

QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(0.0,0.0,400.0,400.0);
for(int i=0;i<5;i++){
QGraphicsPixmapItem *image = new QGraphicsPixmapItem();
image->setPixmap(icon);
scene->addItem(image);
image->setPos(i*icon.width(),0);
}
QGraphicsPixmapItem *image2 = new QGraphicsItem(); // with this default QGraphicsPixmapItem I'm trying to get the item at specific position, and set to this value (image2).
//I know I'm not doing anything now, I just deleted the things I tried to do before posting this thread.

QGraphicsView *view = new QGraphicsView();
view->setScene(scene);
view->show();

I want to change the image located in a specific position for another one, and move this image to the next position (left,right,top,bot).
I tried using QGraphicsScene::itemat(); But I think Dont work with QGraphicsPixmapItem.

If my question is vague, please let me know, I can explain it better, just that I don't have time right now.
Thanks for your time.

anda_skoa
22nd March 2014, 10:42
I tried using QGraphicsScene::itemat(); But I think Dont work with QGraphicsPixmapItem.


Why should it not work with QGraphicsPixmapItem?

Can you show the code that you have tried?

Cheers,
_

vitaR
22nd March 2014, 22:58
Why should it...
Well I just said that... I'm bad I know, that function returns an item, but not a PixmapItem right?
And anyways I couldn't call the function cause I never worked with QTranform before, so I don't know what to place there.

The line number eleven should be something like this:
image2->setPixmap(scene->itemAt(180,0,deviceTransform));

Well I guess is not gonna work anyways, cause the function is not returning a Pixmap, and anyways the variable deviceTransform is not working, like I said, I don't know what to place there. Sorry for not answering early, I was busy. Thanks for the reply.

stampede
23rd March 2014, 03:13
Use qgraphicsitem_cast on pointer returned by itemAt method.


QGraphicsPixmapItem * pixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(pos));
if (pixmap){
....
}

vitaR
23rd March 2014, 07:45
Use qgraphicsitem_cast on pointer...

Thanks for the reply and Thanks for your time, everything works fine now FINALLY.

anuj_anii
18th January 2018, 06:06
Use qgraphicsitem_cast on pointer returned by itemAt method.


QGraphicsPixmapItem * pixmap = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene->itemAt(pos));
if (pixmap){
....
}


I have multiple QGraphicsPixmapItems in the QGraphicsScene and this is for an HMI application.
In order to write the unit test to verify the QPixmaps and their locations i need to fetch the QGraphicsPixmapItems.

I tried exactly as suggested by stampede but the problem is that pixmap returns NULL and if(pixmap) is always false.

Please suggest.

Thanks
Anuj

Ginsengelf
18th January 2018, 13:51
Hi, a common problem is that the position is wrong or in the wrong coordinate system so depending on where you got the value for pos you might have to convert it to scene coordinates.

Ginsengelf