Results 1 to 13 of 13

Thread: help dynamic_cast

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default help dynamic_cast

    I have a problem to make the
    Qt Code:
    1. dynamic_cast<Ostacolo *>(ostacolo)
    To copy to clipboard, switch view to plain text mode 
    see the following code snippet:
    Qt Code:
    1. QList<QGraphicsItem *> collisioniOstacolo = scene()->collidingItems(this);
    2. QList<QGraphicsItem *>::Iterator iter;
    3. for(iter = collisioniOstacolo.begin(); iter != collisioniOstacolo.end(); ++iter)
    4. {
    5. QGraphicsItem *ostacolo = *iter;
    6. if((*iter)->collidesWithItem(this))
    7. {
    8. Ostacolo *pOstacolo = dynamic_cast<Ostacolo *>(ostacolo);
    9. qDebug() << "Toccato un " << pOstacolo->objectName();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    I get this error when compiling:
    Qt Code:
    1. C:\Qt\4.8.3\src\corelib\global\qglobal.h:2505: error: 'dynamic_cast_will_always_fail_because_rtti_is_disabled' is not a member of 'Ostacolo*'
    To copy to clipboard, switch view to plain text mode 
    What did I do wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    You have RTTI disabled Even if you didn't, your code would instantly crash (unless all items in the list are Ostacolo instances but then you wouldn't need dynamic_cast at all).

    Use qgraphicsitem_cast instead.

    Besides, this code doesn't have any sense -- first you're getting a list of items colliding with "this" and then you're checking again if each item in the list collides with "this" -- since you got that item from the list of items colliding with "this", the condition will always be true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    thanks wysota, you're right, the code does not make much sense. Unfortunately I have the ideas a little confused.
    What I want to do is from the list of items that collide with this (ie Robot) to verify which of these items is an Ostacolo and obtain its coordinates. Could you help me to write this piece of code ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    Qt Code:
    1. class X : public Scene {
    2. public:
    3. // ...
    4. Ostacolo* addObstacle(....) {
    5. Ostacolo *ost = new Ostacolo(...);
    6. // ...
    7. listOfObstacles << ost;
    8. return ost;
    9. }
    10. protected:
    11. QList<QGraphicsItem*> collidingObstacles() const {
    12. QList<QGraphicsItem*> colls = robotItem->collidingItems();
    13. QList<QGraphicsItem*> obstacles;
    14. foreach(QGraphicsItem *item, colls) { if(listOfObstacles.contains(item)) obstacles << item; }
    15. return obstacles;
    16. }
    17.  
    18. private:
    19. QList<QGraphicsItem*> listOfObstacles;
    20. Robot *robotItem;
    21. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    Can you explain me the code, I did not understand ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    Which part you don't understand?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    I tried your code but I do not detect collisions against Robot Obstacle. The class Robot call his method collisioniRobot(), see code:
    Qt Code:
    1. void Robot::timerEvent(QTimerEvent *event)
    2. {
    3. event->timerId();
    4.  
    5. // individua eventuali collisioni del robot contro ostacolo
    6. collisioniRobot();
    7. }
    8.  
    9. void Robot::collisioniRobot()
    10. {
    11. // individuazione base collisioni
    12. if(scene()->collidingItems(this).isEmpty())
    13. {
    14. // not collision
    15. }
    16. else
    17. {
    18. // collision
    19. }
    20. ...
    To copy to clipboard, switch view to plain text mode 
    in the code (else) I would call your function collidingObstacles that returns a list QList<QGraphicsItem*> I would call collisions. In fact here is not put nothing.
    I'm
    Qt Code:
    1. class Robot : public QGraphicsSvgItem
    To copy to clipboard, switch view to plain text mode 
    e
    Qt Code:
    1. class Ostacolo : public QGraphicsSvgItem
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    I don't see how your code is related to what I have written before. First of all my code should be put in the scene, not in the item, because it is the scene that knows all the items. If you want to pass pointers to every possible object in the scene to the robot object so that you can compare if the robot has bumped into any of them, then that's probably not a very good design.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    OK, then I have to review my project and transfer control of the item on the scene. In my project I have several instances of Robot and Ostacolo and for each of them (Robot) checks for a collision that may occur against an obstacle (Ostacolo) or against another Robot. How do you advise me is this possible ?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    In my opinion you should do that in the scene and only if an interesting collision occurs, pass this information to the interested object. Otherwise if robots A and B collided, you would check this condition twice -- once in A and once in B but since by checking it in A you already know that B collided with A, there is no point in checking it again. The code is much cleaner and much faster.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    waiting for your answer, I rewrote the code according to your advice. It presents a new problem due to the istance of the Robot (I create 2 Robot and 1 Ostacolo). Wanting to implement "in the scene and only if an interesting collision occurs, pass this information to the interested object" how should I do ?
    I'm sorry if I'm so boring, but for me it is important to be able to solve the problem.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help dynamic_cast

    Quote Originally Posted by giorgik View Post
    Wanting to implement "in the scene and only if an interesting collision occurs, pass this information to the interested object" how should I do ?
    I'm sorry if I'm so boring, but for me it is important to be able to solve the problem.
    Take a piece of paper and develop an algorithm of how it should work. Then take a look at API offered by the Graphics View Framework and implement your algorithm with it. If you need some functionality that is missing in the API, come here and ask about it and we'll help you find a solution. Remember that to use Qt you need a decent knowledge of C++. If you lack that knowledge, it's better to learn it now than to struggle at every step.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: help dynamic_cast

    I managed to solve the problem by following your advice and structuring the program better. thanks

Similar Threads

  1. qobject_cast vs dynamic_cast
    By mike_c in forum Qt Programming
    Replies: 5
    Last Post: 19th April 2010, 18:51
  2. dynamic_cast not working
    By kloffy in forum Newbie
    Replies: 2
    Last Post: 11th October 2007, 23:07
  3. dynamic_cast and templates
    By KShots in forum General Programming
    Replies: 7
    Last Post: 7th August 2007, 20:01
  4. Why can't I make dynamic_cast work properly?
    By pir in forum General Programming
    Replies: 13
    Last Post: 18th July 2006, 16:17
  5. Problem with dynamic_cast
    By vratojr in forum General Programming
    Replies: 7
    Last Post: 12th April 2006, 13:45

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
  •  
Qt is a trademark of The Qt Company.