PDA

View Full Version : Adding Custom Variable To QGraphicsItem, Possible?



steadi
23rd October 2012, 03:45
Hello,

I am looking for a way to create a new variable for a qgraphicsitem. I need to set it for save and load purposes but how would i go about doing this?

The end result should be something along these lines:
QGraphicsItem *temp;
temp = scene->addPixmap([blah blah blah]);
temp->customvariable = 1; //This is the variable i want to create (int) for each time i add the graphicsitem

Thanks in advance and sorry if i did not explain this very well but it is late.
Matt

ChrisW67
23rd October 2012, 05:59
Subclass QGraphicsItem, or one of its existing subclasses (QGraphicsPixmapItem in your example), and go to town. Standard C++ stuff.

steadi
23rd October 2012, 14:29
Thanks for the help man. You have pointed me in the right direction which has helped alot.

class TileItem : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT

public:
int mynewvariable();
void setmynewvariable(int a);
};

Then for the setmynewvariable function:

void TileItem::setmynewvariable(int a)
{
this->mynewvariable = a;
}

But whenever i try to add this item to my graphicsscene:


TileItem *pix;
scene->addItem(pix);


It simply will not allow me. It crashed the whole program. Is there something i am overlooking?

Thanks again for the help,
Matt

wysota
23rd October 2012, 15:03
You created a pointer to a TileItem object but you did not initialize it with a real object. Still standard C++ stuff.