Results 1 to 7 of 7

Thread: regarding qgraphicsitem_cast

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default regarding qgraphicsitem_cast

    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 ??

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Item : public QGraphicsItem
    4. {
    5. public:
    6. enum ItemTypes {
    7. ItemType = UserType+5,
    8. ComponentType,
    9. WireType,
    10. };
    11.  
    12. Item() : QGraphicsItem(0,0) { }
    13.  
    14. QRectF boundingRect() const { return QRectF(-20,-20,40,40); }
    15.  
    16. void paint(QPainter *p,const QStyleOptionGraphicsItem *, QWidget *)
    17. {
    18. p->drawLine(-20,-20,20,20);
    19. }
    20. int type() const { return ItemType; }
    21. };
    22.  
    23. class Component : public Item
    24. {
    25. public:
    26. int type() const { return Item::ComponentType; }
    27. };
    28.  
    29. class Resistor : public Component
    30. {
    31. };
    32.  
    33.  
    34. class Wire : public Component
    35. {
    36. public:
    37. int type() const { return Item::WireType; }
    38. };
    39.  
    40. int main(int argc,char *argv[])
    41. {
    42. QApplication app(argc,argv);
    43. QGraphicsScene sc(0,0,500,500);
    44.  
    45. Resistor * r= new Resistor();
    46.  
    47. sc.addItem(r);
    48. r->setPos(50,50);
    49. v.setScene(&sc);
    50. v.show();
    51.  
    52. // The problematic part
    53. Wire *w = qgraphicsitem_cast<Wire*>(r);
    54. if(w != 0l) // this always happens
    55. {
    56. qDebug("WRONGLY CASTED");
    57. Q_ASSERT(0);
    58. }
    59.  
    60. return app.exec();
    61. };
    To copy to clipboard, switch view to plain text mode 

    I checked the source of qgraphicsitem_cast but I couldn't understand how it works. Can somebody enligten me ?

    Qt Code:
    1. template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
    2. {
    3. return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
    4. || (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Gopala Krishna; 23rd December 2006 at 08:38. Reason: changed inappropriate title

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.