Results 1 to 9 of 9

Thread: Cast from base class to derived, multiple inheritance

  1. #1
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cast from base class to derived, multiple inheritance

    Hi, I have a class defined like this:
    class GraphicsPCRectItem : public QGraphicsRectItem, public GraphicsPCItem{...

    QGraphicsRectItem inherits from QAbstractGraphicsShapeItem and QGraphicsItem, while GraphicsPCItem does not inherit from any class.

    In a function in a subclass I have of QGraphicsView I want to access a GraphicsPCRectItem at a certain position in the view, so what I try is ('v' is a pointer to my QGraphicsView subclass, loc is the position relative to the QGraphuicsView subclass):

    QGraphicsItem *baseItem = (v->itemAt(loc)); //ok
    QGraphicsRectItem *rectItem = dynamic_cast<QGraphicsRectItem*>(baseItem); //ok
    GraphicsPCRectItem *item = dynamic_cast<GraphicsPCRectItem*>(rectItem); //NULL

    How can I cast correctly and access the item which is actually of type GraphicsPCRectItem?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cast from base class to derived, multiple inheritance

    Dynamic cast is correct. You're obviously not doing what you say you are.

    this works for me:
    Qt Code:
    1. #include <iostream>
    2.  
    3. class Base
    4. {
    5. public:
    6. virtual void foo(){}
    7. };
    8.  
    9. class Mixin
    10. {
    11. };
    12.  
    13. class Middle : public Base, public Mixin
    14. {
    15. };
    16.  
    17.  
    18. class Der : public Middle
    19. {
    20. };
    21.  
    22. using std::cout;
    23. using std::cin;
    24.  
    25. int main()
    26. {
    27. Base* base = new Der();
    28.  
    29. Middle* middle = dynamic_cast<Middle*>(base);
    30.  
    31. Der* der1 = dynamic_cast<Der*>(base);
    32. Der* der2 = dynamic_cast<Der*>(middle);
    33. Der* der3 = static_cast<Der*>(base);
    34.  
    35. cout << der1 << '\n';
    36. cout << der2 << '\n';
    37. cout << der3 << '\n';
    38.  
    39. cin.get();
    40. }
    To copy to clipboard, switch view to plain text mode 

    By the way, 'up casting' is sometimes a sign of missing/poor design.
    Last edited by amleto; 17th August 2013 at 18:34.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cast from base class to derived, multiple inheritance

    Hmm, thank you, better check my code again then... I know it is the correct QGraphicsItem I try to cast, I tested by giving the GraphicsPCRectItem a tooltip in its constructor, and printing the tooltip of the 'baseItem'. Does the order I inherit have anything to say, or anything regarding virtual stuff?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Cast from base class to derived, multiple inheritance

    Does the order I inherit have anything to say, or anything regarding virtual stuff?
    No. However, if your compiler has an RTTI (run-time type information) flag, make sure it is set to true (or "on"). dynamic_cast<> depends on it. In most modern compilers, I think it is now on by default.

    I think your problem is that the instance you are trying to cast really isn't what you think it is, otherwise amleto's example would work for your code.

    ----Edit ----

    Ah, no never mind. It is your code that is wrong.

    Qt Code:
    1. QGraphicsItem *baseItem = (v->itemAt(loc)); //ok
    2. QGraphicsRectItem *rectItem = dynamic_cast<QGraphicsRectItem*>(baseItem); //ok
    3. GraphicsPCRectItem *item = dynamic_cast<GraphicsPCRectItem*>(rectItem); //NULL
    To copy to clipboard, switch view to plain text mode 

    The problem is the last line: "rectItem" is not a pointer to a GraphicsPCRectItem, it is a pointer to a QGraphicsRectItem. QGraphicsRectItem does not inherit from anything that can be cast to a GraphicsPCRectItem, so the dynamic_cast<> is doing exactly what it is supposed to, telling you that, "No, can't cast this to that".

    Remove the second line, and change "rectItem" to "baseItem" in the dynamic cast to GraphicsPCRectItem, and everything will be fine.
    Last edited by d_stranz; 17th August 2013 at 22:28.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cast from base class to derived, multiple inheritance

    d_stranz, re: your edit: isn't that exactly my example where first middle is cast from base, and then der2 is cast from middle? What you have written seems to suggest that dynamic_cast cannot up-cast, which it can.

    and to be precise with the language, from the op's code we don't know what the pointer is pointing to so we cannot say what type the instance is.
    Last edited by amleto; 18th August 2013 at 02:02.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jul 2008
    Location
    Norway, Trondheim
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Buy Generic Cleocin Medicine Terbon

    Thank you all, it works now by casting directly from the baseItem to GraphicsPCRectItem. In addition, there was a problem with only the top item at a certain position being returned, which sometimes was not the correct item. So what I do now is:

    QList<QGraphicsItem*> baseItems = v->items(loc);
    for(int i=0;i<baseItems.size();i++){
    QGraphicsItem *baseItem = baseItems[i];

    if(!baseItem){
    continue;
    }

    GraphicsPCRectItem *item = dynamic_cast<GraphicsPCRectItem*>(baseItem);
    ..............

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Buy Generic Cleocin Medicine Terbon

    Quote Originally Posted by ivareske View Post
    Thank you all, it works now by casting directly from the baseItem to GraphicsPCRectItem. In addition, there was a problem with only the top item at a certain position being returned, which sometimes was not the correct item. So what I do now is:

    QList<QGraphicsItem*> baseItems = v->items(loc);
    for(int i=0;i<baseItems.size();i++){
    QGraphicsItem *baseItem = baseItems[i];

    if(!baseItem){
    continue;
    }

    GraphicsPCRectItem *item = dynamic_cast<GraphicsPCRectItem*>(baseItem);
    ..............
    bolded is the main problem here, nothing to do with casting issues. The cast returned null because the object isn't one of your PC classes.

    NB Please use code tags - your code is unreadable. Also baseItem will never be null so having this block...
    Qt Code:
    1. if(!baseItem){
    2. continue;
    3. }
    To copy to clipboard, switch view to plain text mode 
    ...is pointless.
    Last edited by amleto; 18th August 2013 at 15:52.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Buy Generic Cleocin Medicine Terbon

    Something very strange here - except for amleto's last reply, none of the CODE-tagged items are appearing when I look at this thread. Anyone else have the same problem? (I'm looking at this in Firefox on a new Windows PC - I'll have to boot up my old PC and see if the same thing happens).

    Not only that, but starting a couple of replies back, the thread title was hacked - "Buy Generic Cleocin Medicine Terbon". Looks to me like there's a security problem on Qt Centre or ivareske's machine.

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default re: Cast from base class to derived, multiple inheritance

    I had to add extra newlines after the closing [ / code] to get it to show when I wrote it (yes, I have the same problem). The board was full of spam this morning so title change just a result of replying to spam message (now deleted).

    I 'fixed' the reply title - 'hacking' not needed
    Last edited by amleto; 18th August 2013 at 23:43.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2012, 09:46
  2. Replies: 3
    Last Post: 6th April 2012, 16:44
  3. Replies: 3
    Last Post: 27th December 2008, 19:34
  4. Can not cast into the derived class from QScrollBar
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 10:23
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Tags for this Thread

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.