0

I have a class inherited from QGraphicsObject,and I have a private QPixmap variable car. All I want to do is draw this car image in the scene. I want to draw many cars like this. And I need to know whether two cars collides with each other. I rewrite two functions boundingRect() and shape(), but it does not work. For example, here is my code:

QRectF Car::boundingRect() const
{
   return QRectF(QPointF(0,0),car.size());
}

QPainterPath Car::shape() const
{
   QPainterPath path;
   path.addRect(boundingRect());
   return path;
 }

In the main.cpp, I test it like this:

Car item(Car::RIGHT,10);
item.setPos(QPoint(95,52));
Car item1(Car::RIGHT,10);
item1.setPos(QPoint(95,54));
//item.start();
scene.addItem(&item);
scene.addItem(&item1);
//qDebug()<< item.boundingRect();
if(item.collidesWithItem(&item1)){
     qDebug()<<"item collides with item1";
}

But it dose not print "item collides with item1" I supposed. Does anything go wrong?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Jackie
  • 385
  • 1
  • 3
  • 10
  • Override the paint function in Car and draw the path returned from shape() and the rect from boundingRect. Do you see them and do they overlap? – TheDarkKnight Dec 17 '13 at 09:08
  • Yes,I override the paint function void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->drawPixmap(0,0,car); } But if I use this shape() function for collision detection,it does not work. – Jackie Dec 17 '13 at 09:24
  • You say you override the paint function, but your code does not show drawing the returned path from the shape function. – TheDarkKnight Dec 17 '13 at 09:36

0 Answers0