#include <QtGui>
{
public:
enum ItemTypes {
ItemType = UserType+5,
ComponentType,
WireType,
};
QRectF boundingRect
() const { return QRectF(-20,
-20,
40,
40);
}
{
p->drawLine(-20,-20,20,20);
}
int type() const { return ItemType; }
};
class Component : public Item
{
public:
int type() const { return Item::ComponentType; }
};
class Resistor : public Component
{
};
class Wire : public Component
{
public:
int type() const { return Item::WireType; }
};
int main(int argc,char *argv[])
{
Resistor * r= new Resistor();
sc.addItem(r);
r->setPos(50,50);
v.setScene(&sc);
v.show();
// The problematic part
Wire *w = qgraphicsitem_cast<Wire*>(r);
if(w != 0l) // this always happens
{
qDebug("WRONGLY CASTED");
Q_ASSERT(0);
}
return app.exec();
};
#include <QtGui>
class Item : public QGraphicsItem
{
public:
enum ItemTypes {
ItemType = UserType+5,
ComponentType,
WireType,
};
Item() : QGraphicsItem(0,0) { }
QRectF boundingRect() const { return QRectF(-20,-20,40,40); }
void paint(QPainter *p,const QStyleOptionGraphicsItem *, QWidget *)
{
p->drawLine(-20,-20,20,20);
}
int type() const { return ItemType; }
};
class Component : public Item
{
public:
int type() const { return Item::ComponentType; }
};
class Resistor : public Component
{
};
class Wire : public Component
{
public:
int type() const { return Item::WireType; }
};
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QGraphicsScene sc(0,0,500,500);
Resistor * r= new Resistor();
sc.addItem(r);
r->setPos(50,50);
QGraphicsView v;
v.setScene(&sc);
v.show();
// The problematic part
Wire *w = qgraphicsitem_cast<Wire*>(r);
if(w != 0l) // this always happens
{
qDebug("WRONGLY CASTED");
Q_ASSERT(0);
}
return app.exec();
};
To copy to clipboard, switch view to plain text mode
template <class T>
inline T qgraphicsitem_cast
(QGraphicsItem *item
) {
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
{
return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks