Results 1 to 6 of 6

Thread: reimplementation of collidesWithItem()

  1. #1

    Default reimplementation of collidesWithItem()

    Hi,

    I'm pretty new to Qt (and C++ also) and I hope I'm at the right place to ask this question

    I'm trying to code a small Billiard game and wanted to reimplement collidesWithItem() as it is suggested in the Qt Documentation.

    My problem is, that once I reimplemented the function, it doesn't seem to be called by Qt. It still uses the "default" one. The default one works fine, that's not the problem. It's purely for educational purposes that I'd like to know why it's not called. I did reimplement functions like paint() and they worked perfectly.

    Here is my function:

    Qt Code:
    1. bool Ball::collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode) const
    2. {
    3. if (this==ball) {return false;}
    4.  
    5. double epsilon = 100; // I put it this high, so I can see if it works. The Balls change color when colliding.
    6. double distance = sqrt(pow(pos().x()- ball->pos().x(),2) + pow(pos().y()-ball->pos().y(),2));
    7.  
    8. if(distance-radius-ball->radius < epsilon) {return true;}
    9. else {return false;}
    10. }
    To copy to clipboard, switch view to plain text mode 

    Is it linked to the fact that I'm not using QGraphicsItem as the first argument, but Ball? Shouldn't it be overloading in this case?

    Here is the Ball.h:

    Qt Code:
    1. #ifndef BALL_H
    2. #define BALL_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6.  
    7. class Ball : public QGraphicsItem
    8. {
    9. public:
    10. Ball();
    11. Ball(double x, double y);
    12. Ball(double x, double y, double angle);
    13. ~Ball();
    14.  
    15. QRectF boundingRect() const;
    16. QPainterPath shape() const;
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    18. QWidget *widget);
    19.  
    20. bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
    21. qreal getRadius() const;
    22. void setRadius(const qreal &value);
    23.  
    24. protected:
    25. void advance(int step);
    26.  
    27. private:
    28. qreal angle;
    29. double position[2];
    30. qreal speed[3];
    31. qreal radius;
    32. qreal mass;
    33. QColor color;
    34.  
    35. };
    36.  
    37. #endif // BALL_H
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance for your answer

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: reimplementation of collidesWithItem()

    You got it almost right, but you have a signature mismatch. The problem is, your "collidesWithItem" is not a re-implementation of the base class method, you just simply introduced a new function to your class. In order to override a method, it needs to have exactly the same signature as in the base class:
    Qt Code:
    1. bool QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
    2. // above signature will work ok, your method:
    3. bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
    To copy to clipboard, switch view to plain text mode 
    Btw. if you are using C++11, you can take advantage of the "override" specifier, for example:
    Qt Code:
    1. #ifndef BALL_H
    2. #define BALL_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6.  
    7. class Ball : public QGraphicsItem
    8. {
    9. public:
    10. Ball();
    11. Ball(double x, double y);
    12. Ball(double x, double y, double angle);
    13. ~Ball();
    14.  
    15. QRectF boundingRect() const override;
    16. QPainterPath shape() const override;
    17. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    18. QWidget *widget) override;
    19.  
    20. // this should give a compilation error, as the base class does not declare a virtual method with this signature
    21. bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const override;
    22.  
    23.  
    24. ...
    25.  
    26. };
    27.  
    28. #endif // BALL_H
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: reimplementation of collidesWithItem()

    Oh ok, I understand. Thank you.

    The Problem when I use:

    Qt Code:
    1. bool QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
    To copy to clipboard, switch view to plain text mode 

    is that a QGraphicsItem doesn't have a "radius" as member. So when I do

    Qt Code:
    1. if(distance-radius-other->radius < epsilon) {return true;}
    2. else {return false;}
    To copy to clipboard, switch view to plain text mode 

    it gives me obviously a compiler error. How can I circumvent this? Or is there any other way to do this?

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

    Default Re: reimplementation of collidesWithItem()

    You have to cast the object to your item class. E.g. using qgraphicsitem_cast (see the docs how to use it).
    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

    Default Re: reimplementation of collidesWithItem()

    Thanky you wysota, the casting seems to work now. My first problem still remains apparently, I don't know what I'm doing wrong:

    Qt Code:
    1. #ifndef BALL_H
    2. #define BALL_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6.  
    7. class Ball : public QGraphicsItem
    8. {
    9. public:
    10. Ball();
    11. Ball(double x, double y);
    12. Ball(double x, double y, double angle);
    13. ~Ball();
    14.  
    15. // Reimplementations
    16. QRectF boundingRect() const;
    17. QPainterPath shape() const;
    18. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    19. QWidget *widget);
    20. bool collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const;
    21. int type() const; // for qgraphicsitem_cast
    22.  
    23. // Setters and Getters
    24. qreal getRadius() const;
    25. void setRadius(const qreal &value);
    26.  
    27. // for type() reimplementation
    28. enum { Type = UserType + 1 };
    29.  
    30. protected:
    31. void advance(int step);
    32.  
    33. private:
    34. qreal angle;
    35. double position[2];
    36. qreal speed[3];
    37. qreal radius;
    38. qreal mass;
    39. QColor color;
    40.  
    41. };
    42.  
    43. #endif // BALL_H
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. bool Ball::collidesWithItem( const QGraphicsItem * other, Qt::ItemSelectionMode mode /*= Qt::IntersectsItemShape*/ ) const
    2. {
    3. Ball const* otherBall = qgraphicsitem_cast<Ball const*>(other);
    4. Ball const* thisBall = qgraphicsitem_cast<Ball const*>(this);
    5. std::cout<<"test"<<endl;
    6.  
    7. double epsilon = 100;
    8. double distance = sqrt(pow(thisBall->pos().x()- otherBall->pos().x(),2) + pow(thisBall->pos().y()-otherBall->pos().y(),2));
    9. if(distance-thisBall->getRadius()-otherBall->getRadius() < epsilon) {return true;}
    10. else {return false;}
    11. }
    To copy to clipboard, switch view to plain text mode 

    "collidesWithItem" is still not called somehow, although the signature should be the right one (I even inserted it with the Qt Creator automatically). What am I not seeing?

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

    Default Re: reimplementation of collidesWithItem()

    You'd have to prepare a minimal compilable example reproducing the problem. Or at least show us how you call collidesWithItem().
    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.


Similar Threads

  1. QPlainTextEdit reimplementation
    By qt_developer in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2012, 15:22
  2. QApplication::notify reimplementation
    By sa5webber in forum Newbie
    Replies: 0
    Last Post: 30th August 2012, 23:00
  3. QHeaderView paintSection reimplementation
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2008, 17:16
  4. keyPressEvent() reimplementation
    By aurelius in forum Newbie
    Replies: 5
    Last Post: 30th October 2008, 16:23
  5. have anyone used collidesWithItem?
    By rajesh in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2007, 21:15

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.