Results 1 to 2 of 2

Thread: Problem with QGraphicsItem Casting

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Problem with QGraphicsItem Casting

    I have a problem with type casting. I have developed a base class from QGraphicsItemGroup that will have an arbitrary number of children that are an instance of a subclass of the base class. I want to be able to perform base class functions on all of the children so I need a list of pointers to the children. The problem is that the children are of varying types (of subclass) and I cant seem to properly cast from QGraphicsItem* to Base* for instances of the subclasses. I think it is a problem with casting to a middle level of inheritance. I have provided a simplified example (which I have not actually compiled and tested, but should illustrate my problem):
    Qt Code:
    1. #include <QGraphicsItem>
    2. #include <QGraphicsItemGroup>
    3.  
    4. class Base : public QGraphicsItemGroup
    5. {
    6. public:
    7. Base(){}
    8. enum { Type = QVariant::UserType + 1000 };
    9. int type() const { return Type; }
    10. static bool isBasedOffChild(QGraphicsItem *item);
    11. QList<Base *> childBaseItems();
    12. void test(){}
    13. };
    14.  
    15. class SubClass1 : public Base
    16. {
    17. public:
    18. SubClass1(){}
    19. enum { Type = Base::Type + 1 };
    20. int type() const { return Type; }
    21. };
    22.  
    23. class SubClass2 : public Base
    24. {
    25. public:
    26. SubClass2(){}
    27. enum { Type = Base::Type + 2 };
    28. int type() const { return Type; }
    29. };
    30.  
    31. bool Base::isBasedOffChild(QGraphicsItem *item)
    32. {
    33. if(item->type() > Base::Type)
    34. return true;
    35. else
    36. return false;
    37. }
    38.  
    39. QList<Base *> Base::childBaseItems()
    40. {
    41. QList<QGraphicsItem *> items = childItems();
    42. QList<Base *> baseItems;
    43. for(int i = 0; i < items.size(); i++)
    44. {
    45. if(isBasedOffChild(items.at(i)))
    46. {
    47. baseItems.append(qgraphicsitem_cast<Base *>(items.at(i)));
    48. }
    49. }
    50. return baseItems;
    51. }
    To copy to clipboard, switch view to plain text mode 

    If i execute the following code, I get memory access faults.
    Qt Code:
    1. Base *parent = new Base();
    2. parent->addToGroup(new SubClass1());
    3.  
    4. QList<Base *> items = parent->childBaseItems();
    5.  
    6. for(int i = 0; i < items.size(); i++)
    7. {
    8. //Causes memory faults
    9. items.at(i)->test();
    10. }
    To copy to clipboard, switch view to plain text mode 

    It would appear the problem is that I cannot do this:
    Qt Code:
    1. QGraphicsItem * item = new SubClass1();
    2. qgraphicsitem_cast<Base *>(item);
    To copy to clipboard, switch view to plain text mode 

    Is it possible to cast to a middle layer of the inheritance hierarchy like this? I don't want the subclasses to have to deal with the details of type casting and the parent class should not need to know anything about the child classes so therefore can't do any type casting from them. Does anybody have any suggestions? Thanks.

  2. #2
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problem with QGraphicsItem Casting

    I seem to have found the problem. Doing the following works:
    Qt Code:
    1. QGraphicsItem * item = new SubClass1();
    2. dynamic_cast<Base *>(item);
    To copy to clipboard, switch view to plain text mode 

    The qgraphicsitem_cast documnetation states:
    Note: To make this function work correctly with custom items, reimplement the type() function for each custom QGraphicsItem subclass.
    Since I am not casting to the exact type defied by qgraphicsitem::type(), but to my base class instead, it would appear that I need to use a dynamic cast.

Similar Threads

  1. Problem with type casting
    By qtDave in forum Newbie
    Replies: 7
    Last Post: 9th November 2009, 20:57
  2. Problem regaridng Type casting QVariant
    By sudheer168 in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 06:02
  3. Problem with type casting?
    By Jyoti.verma in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2009, 05:24
  4. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 15:37
  5. Casting QGraphicsItem
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2007, 23:56

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.