PDA

View Full Version : QGraphicsItem ambiguity in subclass



Talei
6th April 2014, 00:51
Hello,
I have a question regarding design of multi subclasses item on the scene. I know that's not Qt question but C++ but because of the context I posted it in Qt section, if that's wrong section please move this thread.

What I want to do is to have base class with all common code for the item like this:


class Base : public virtual QGraphicsItem
{
// common code for all items on the scene
};
Next I want some object that are i.e. QGraphicsPixmapItem, QGrapthicsTextItem etc, i.e..

class ItemA : public Base, public QGraphicsPixmapItem
{
QRectF boundingRect() const {
return QGraphicsPixmapItem::boundingRect();
}

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {

painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
QGraphicsPixmapItem::paint( painter, option, widget);
}

// Only pixmap related code
};
Now I want to control all these objects so I store pointers to objects like:

QList <Base*> itemList;
The problem is ambiguity of QGraphicsItem (in Base) due to the fact that i.e. QGraphicsPixmapItem (and all Qt item classes) are declared as:

class Q_WIDGETS_EXPORT QGraphicsPixmapItem : public QGraphicsItem

My question is how would I go about designing classes that do subclass from i.e. QGraphicsPixmapItem

anda_skoa
7th April 2014, 10:53
There are a couple of options:

1) share the common code via delegation instead of derivation, i.e. have a class that is used by all your items
2) use a base class that is not a QGraphicsItem but which can return a pointer to the graphics item
3) use a template as a base class that derives from the template type

Cheers,
_