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:
// above signature will work ok, your method:
bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
bool QGraphicsItem::collidesWithItem(const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
// above signature will work ok, your method:
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:
#ifndef BALL_H
#define BALL_H
#include <QGraphicsItem>
{
public:
Ball();
Ball(double x, double y);
Ball(double x, double y, double angle);
~Ball();
QRectF boundingRect
() const override;
// this should give a compilation error, as the base class does not declare a virtual method with this signature
bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const override;
...
};
#endif // BALL_H
#ifndef BALL_H
#define BALL_H
#include <QGraphicsItem>
class Ball : public QGraphicsItem
{
public:
Ball();
Ball(double x, double y);
Ball(double x, double y, double angle);
~Ball();
QRectF boundingRect() const override;
QPainterPath shape() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
// this should give a compilation error, as the base class does not declare a virtual method with this signature
bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const override;
...
};
#endif // BALL_H
To copy to clipboard, switch view to plain text mode
Bookmarks