Advice needed - creating items
I need to create several items and add them to a scene. But for each item I need to set some properties.
Ex.: item.color , item.speed , item.power , etc...
Should I make a class, say "Item", give it those properties and then create a QGraphicsItem and add to the scene, or should I subclass a QGraphicsItem, add those properties and add it to the scene?
Is there any difference in these two approaches? Is one "better" than the other?
Re: Advice needed - creating items
I'm not sure what you mean or what the difference is you want to describe.
However, some solutions are personal taste. If it works for you and you feel happy with a solution, and it doesn't bother you, nobody will tell you it is wrong.
On the other hand, always keep your classes and their implementations as clean and simple as possible. If you can skip an extra object, do so. And keep in mind that if you want to make your program open source so other people can work on it too, you want to make the source as easy to read as possible and close to common standards.
Re: Advice needed - creating items
I mean:
This:
Code:
class Item{
int eficiency;
int power;
}
Versus this:
Code:
int eficiency;
int power;
}
Re: Advice needed - creating items
This is "inheritance vs composition". Depending on whom you ask and what your specific case is, it could go either way (rather both ways) as far as what is 'right'. I encourage you to pick up a book on the subject and/or google "inheritance vs composition" and decide for yourself.
Re: Advice needed - creating items
I'll just go with inheritance.
Re: Advice needed - creating items
In the first case since the item doesn't inherit QGraphicsItem, you need some way to make the classes cooperate. For instance you need a way to map a QGraphicsItem to its Item counterpart as Graphics View operates in terms of QGraphicsItems.