PDA

View Full Version : QGraphicsObject rewrite boundingRect() and shape()



赵洪武
17th December 2013, 03:08
hi,my problem is that 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 do not work.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;
}

Does anything go wrong?

wysota
17th December 2013, 08:44
Thus far it is ok. What about the rest of the code? :)

赵洪武
18th December 2013, 04:48
Car item1;
item1.setPos(95,66);
Car item2;
item2.setPos(300,66);
scene.addItem(&item1);
scene.addItem(&item2);
if(item1.collidesWithItem(&item1))
qDebug()<<"items collides with item2";

Hera is my test code.actually two two cars don't collides with each other,but it prints "items collides with item2".I think it is my shape() problem.

ChrisW67
18th December 2013, 05:34
You should use
... tags around code.


Car item1;
item1.setPos(95,66);
Car item2;
item2.setPos(300,66);
scene.addItem(&item1);
scene.addItem(&item2);
if(item1.collidesWithItem(&item1))
qDebug()<<"items collides with item2";

Line 7 asks if item1 collides with itself. It does, so you print the misleading message.