Question

I've been working on a game in C++/Qt. My game consists of some custom Classes with a pointer to a custom class picture that inherits from QGraphicsItem. The Picture class also contains a pointer back to the instance of the custom class.

Now when I add the items with the picture class to a QGraphicsScene, I want to get the selected items from that Scene, and read the pointers to their custom classes, but I don't really know a way to do this. I tried using QGraphicsScene->selectedItems() but this only returns a QList of GraphicsItems :S

Could anybody help me with this? Thanks in advance

Was it helpful?

Solution

You can use dynamic_cast to cast the QGraphicsItems to your custom class that inherits from QGraphicsItem when you get a list of selected items in the scene.

Example:

QList<QGraphicsItem*> list = scene->selectedItems();
for(int i = 0; i < list.length(); i++)
{        
    CustomItem *item = dynamic_cast<CustomItem*>(list[i]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top