PDA

View Full Version : Multiple Inheritance for QGraphicsItem



pankaj.patil
30th June 2008, 21:58
Hello,
I am using QT 4.4.0 for designing a circuit editor. I am using Qt's Graphics View framework for drawing different elements of circuit, text, pixmaps and even plots (embedding QwtPlotWidget in QGraphicsProxyWidget class).
I want to have these different elements derived from a common class, lets call it base, and move the functionality common to them in base. Base needs to reimplement some of the methods defined in QGraphicsItem class (itemChange() to mention one), and hence has to derive from QGraphicsItem class. However, this design leads to diamond inheritance issue (as the classes deriving from base also have to derive from classes like QGraphicsPixmapItem and QGraphicsProxyWidget which derive from QGraphicsPixmapItem). Unfortunately, none of the classes given by Qt which derive from QGraphicsPixmapItem (like QGraphicsPixmapItem) inherit it virtually, and put a lot of restriction on my design. Can anyone please suggest me a solution or workaround?

Thanks in advance,
Pankaj

Given below is the pseudo code depicting my problem:


class Element : public QGraphicsPixmapItem
{
//Lots of common code
QVariant itemChange ( GraphicsItemChange change, const QVariant & value )
{ //Update certain common data upon state change, common to all}
}

class TextElement : public Element, public QGraphicsTextItem
{
}

class Plot : public QwtPlotWidget, public Element
{
}

class scene : public QGraphicsScene
{
TextElement *t = new TextElement;
Plot *p = new Plot;
addItem( t); // Problem: Ambiguous conversion from TextElement* to QGraphicsItem*
addItem(p);//Problem :Ambiguous conversion from Plot* to QGraphicsItem*
//Also, problem while directly or indirectly accessing itemChange() ( ambiguous access)
}

awhite1159
1st July 2008, 03:01
With multiple inheritance, don't you need to fully qualify:

QGraphicsItem::addItem() <- example.

If both the base classes have the same member function, this is necessary.

pankaj.patil
1st July 2008, 14:49
Hi awhite,
Thanks for your reply. Your are right that if both the base classes have same member function, I would have to fully qualify who's function I want to use.
My problem is more of diamond inheritance issue. TextElement, for example, derives from Element and QGraphicsTextItem, both ultimately derive from QGraphicsItem ( I misspelt class Element : public QGraphicsPixmapItem, rather it should be class Element : public QGraphicsItem, however, even if it was to derive from QGraphicsItem, the same issue would come as it is ultimately deriving from QGraphicsItem through QGraphicsPixmapItem). Hence the conflict comes.
Fully qualified syntax would solve the problem, as would virtual inheritance. The problem with fully qualified syntax is too much of code rewriting, while for virtual inheritance is that QGraphicsTextItem (and QGraphicsPixmapItem in case Element derives from it) does not virtually inherit QGraphicsItem, limiting the use of virtual inheritance.
Is there a workaround for this problem?
-Thanks,
Pankaj