Gopala Krishna
23rd December 2006, 08:18
Hello guys,
I am facing some problems with qgraphicsitem_cast which casts in a wrong manner resulting in crashes.
Because of this now I am forced to check manually the value of type() function and then cast. :(
I am using Qt4.2.2 on linux.
The following working example illustrates my problem.
Is there something wrong in my code or is it a Qt bug ?? :eek:
#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();
};
I checked the source of qgraphicsitem_cast but I couldn't understand how it works. Can somebody enligten me ?
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;
}
I am facing some problems with qgraphicsitem_cast which casts in a wrong manner resulting in crashes.
Because of this now I am forced to check manually the value of type() function and then cast. :(
I am using Qt4.2.2 on linux.
The following working example illustrates my problem.
Is there something wrong in my code or is it a Qt bug ?? :eek:
#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();
};
I checked the source of qgraphicsitem_cast but I couldn't understand how it works. Can somebody enligten me ?
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;
}