Results 1 to 4 of 4

Thread: QGraphicsObject rewrite boundingRect() and shape()

  1. #1
    Join Date
    Dec 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default QGraphicsObject rewrite boundingRect() and shape()

    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?

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

    Default Re: QGraphicsObject rewrite boundingRect() and shape()

    Thus far it is ok. What about the rest of the code?
    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
    Dec 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: QGraphicsObject rewrite boundingRect() and shape()

    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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsObject rewrite boundingRect() and shape()

    You should use [code]...[/code] tags around code.
    Qt Code:
    1. Car item1;
    2. item1.setPos(95,66);
    3. Car item2;
    4. item2.setPos(300,66);
    5. scene.addItem(&item1);
    6. scene.addItem(&item2);
    7. if(item1.collidesWithItem(&item1))
    8. qDebug()<<"items collides with item2";
    To copy to clipboard, switch view to plain text mode 
    Line 7 asks if item1 collides with itself. It does, so you print the misleading message.

Similar Threads

  1. animating a QGraphicsObject
    By tommy22 in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2013, 11:20
  2. protected QGraphicsObject
    By stefan in forum Qt Programming
    Replies: 6
    Last Post: 19th August 2011, 17:04
  3. how to rewrite QStyle
    By trytoremeber963 in forum Newbie
    Replies: 3
    Last Post: 28th April 2009, 08:18
  4. How Can I rewrite qwtDial Class
    By validator in forum Qwt
    Replies: 4
    Last Post: 21st February 2008, 06:25
  5. How to avoid calling boundingRect() & shape()
    By nileshsince1980 in forum Qt Programming
    Replies: 7
    Last Post: 8th October 2007, 18:10

Tags for this Thread

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.