PDA

View Full Version : How to design?



zgulser
28th August 2009, 09:17
Hi,

I have a common QGraphicsItem which I use it in several other classes. The question is;

Does these classes should be QGraphicsItem too or not? Which is better than the other?


Thanks.

Lykurg
28th August 2009, 11:38
:confused: can you please be a little bit more precise, because I don't get your question. How do your classes look like? (= important are the base classes of them)


MyItem : public QGraphicsItem

...and then?

zgulser
28th August 2009, 12:29
Hi again,

Sure I can,

you have an item like;


class LineItem : public QGraphicsItem { ... }

and you need to use this LineItem for example in a class to form different type of shapes such as rectangle, triangle or more complex shapes like a star. So how should we implement the shape class( f.e.: RectangleItem, StarItem)? Should they inherit QGraphicsItem as well?

Hope you catch the point now.

navi1084
28th August 2009, 12:35
you better have a abstract class (may be CShapeitem) which is derived QGraphicsItem and put all the common code which are part of its descendant. from there u can derive ur respective classes and in main ui, it will be very convenient you to access objects of derived class using CShapeItem.

Lykurg
28th August 2009, 12:53
Ok, as I understand you now, you want to build a class like rectangleItem out of four of your LineItems.

If so (which is in most cases not the best way) you have two options:


Your class rectangleItem has no base class. But then you have to set a pointer to the scene and add the lines manually to the scene, since you can't add rectangleItem to it since it is no real graphics item.
Use QGraphicsItem as a base class for rectangleItem. Now when creating the line items use the instance of rectangleItem as parent. Then the lines will automatically move, if you move your rectangleItem. (<- this way I would prefer, if you don't want to use navi1084 variant and do the "line drawing" in each shape in the corresponding class itself.)

zgulser
28th August 2009, 13:47
What about using QGraphicsItemGroup to be derived by the rectangleItem? And if I use QGraphicsItemGroup method( if it's appropriate), do I need extra doing for the rectangleItem's shape() and boundingRect() functions which I reimplemented both of them in LineItem?