Results 1 to 10 of 10

Thread: QGraphicsItem Tracer

  1. #1
    Join Date
    May 2008
    Posts
    7
    Thanks
    4

    Default QGraphicsItem Tracer

    I have a QGraphicsItem (A) and I would like to implement a path tracing functionality for it. (ie as A changes position, a graphical trace is drawn on QGraphicsView).

    I was thinking to create a custom QGraphicsItem (B) with a QPolygonF data member which will hold N points. Then in the paintEvent, use those points to draw N circles, one next the other thus creating the trace I want. My problem is a part of QGraphicsItem documentation which states the following:

    "QGraphicsScene expects all items boundingRect() and shape() to remain unchanged unless it is notified. If you want to change an item's geometry in any way, you must first call prepareGeometryChange() to allow QGraphicsScene to update its bookkeeping.".

    So calling prepareGeometryChange() in every paintEvent is not a solution.

    Any suggestions on what should I do?

    NOTE: I would prefer a solution where the tracer is a child of A.

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

    Default Re: QGraphicsItem Tracer

    Here is a hint: items don't move as a result of paint events. You get notified upon position changes of items through QGraphicsItem::itemChange().
    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
    May 2008
    Posts
    7
    Thanks
    4

    Default Re: QGraphicsItem Tracer

    ok to put it in another way.... How bad is the following?

    Qt Code:
    1. void
    2. TracerGraphicsItem::paint(QPainter* pPainter, const QStyleOptionGraphicsItem* pOption, QWidget* pWidget)
    3. {
    4. m_points.append(scenePos());
    5. if(m_points.count() > 50)
    6. m_points.remove(0);
    7.  
    8. pPainter->setBrush(Qt::blue);
    9. pPainter->setPen(Qt::NoPen);
    10.  
    11. foreach(QPointF p, m_points)
    12. pPainter->drawEllipse(m_rect.translated(p-scenePos()));
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QGraphicsItem Tracer

    Very bad as you don't know how many times your item will be repainted. Especially when things like caching or dragging windows over other windows are involved. Why don't you use itemChange()?
    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
    Join Date
    May 2008
    Posts
    7
    Thanks
    4

    Default Re: QGraphicsItem Tracer

    I'm not using itemChange() because I really can't see how that would help me to implement the tracer I want!
    To make sure we're on the same track, i'm attaching a screenshot which show the results of the previous code I posted (which I know is bad).

    (btw the code above does what I want if and only if the parent of the tracer (ie the box in the picture) is not rotated at all)

    Edit: The only thing I can see doing in itemChange() is to update the points of the path (instead of doing it in paint())
    Attached Images Attached Images
    Last edited by dimixan; 24th January 2010 at 01:29.

  6. #6
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem Tracer

    i read the problem and i think you need use the itemChange() funtion.

    instead of have a QList<Point> to keep track of the tracing point. you need to have a QList<QGraphicsEllipseItem> to keep track. this way each point has you own paint() and no need to call prepareGeometryChange () every time you insert a point in the scene.

    the correct way, or at least the most easy way to catch the item position when tracing the item is in the itemChange() function using the QGraphicsItem::ItemPositionChange. when the position change creata a ellipseItem. make each ellipse item parent to A.


    if you draw the tracing points in the paint() of item A, you need recalculate the boundingRect () of A every time you insert a point in the scene
    Last edited by ecanela; 24th January 2010 at 02:46. Reason: updated contents

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

    Default Re: QGraphicsItem Tracer

    Quote Originally Posted by dimixan View Post
    I'm not using itemChange() because I really can't see how that would help me to implement the tracer I want!
    It would help you keep track of position changes of your item.

    (btw the code above does what I want if and only if the parent of the tracer (ie the box in the picture) is not rotated at all)
    I can break your code in 15 seconds.

    Edit: The only thing I can see doing in itemChange() is to update the points of the path (instead of doing it in paint())
    That's exactly what you should do. The whole "bad" thing with your code is that you update the list from painting routine.

    Edit: Of course you shouldn't draw the points in the paint() routine as well. Either implement it as an effect for your item or as a child or associated item to your item.
    Last edited by wysota; 24th January 2010 at 09:31.
    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.


  8. The following user says thank you to wysota for this useful post:

    dimixan (24th January 2010)

  9. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem Tracer

    You can also consider the following design -
    Have a list of points associated with each graphics item. And use these points to in QGraphicsScene::drawBackground to draw the trails.
    You can also have a pixmap for background, and you need to update the pixmap on itemChange signal . This approach might save you from using item for the trails.

  10. #9
    Join Date
    Jun 2006
    Location
    Colorado
    Posts
    12
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem Tracer

    I have a similar implementation problem.

    I'm developing a 2D earth plot using the Graphics View Framework to represent the flight path of a aircraft (Position in Latitude and Longitude). The background is a image of a Mercator projection of the earth added via (QGraphicView). I already have the means of converting from geographical coordinates to the scene coordinates.

    The user needs to have the ability to add or delete aircraft (0 to 10) hence each aircraft is a reimplementation of QGraphicsItem. Each aircraft stores its initial position (Lat & Lon) and adds its current position to storage "QList" during its flight. I want to show the flight path from take off (New York ) to landing (Paris). The user must have the ability of selecting a aircraft and seeing where it had taken off from even after it has already started its journey.

    I plan on updating the Earth Plot via a QTimer connected to the scene's advance() slot (similar to the Colliding Mice Example) during the aircraft flight.

    In the AircraftItem every time it geographical position has been updated and the QTimer timeout() signal is triggered then in my AircraftItem::advance(int step) function I was planning on calling AircraftItem::itemChange() to change its position translated into the scene's coordinates.

    Ideally the whole flight path is represented by a QPainter::drawPolyline

    When I update the aircraft's position on my plot do I need to change its boundingRect() "Not really sure how to do this, btw" and should I do my drawing in its paint function.

  11. #10
    Join Date
    Jun 2006
    Location
    Colorado
    Posts
    12
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsItem Tracer

    NOTE In my AircraftItem Object when my advance(int step) function is activated by the QTimer::timeout() signal I want to update the aircraft's current position (converted into the scene's coordinates) Now as I draw the flight path from the initial starting point. Does the entire flight path trace need to be within the AIrcraftItem::boundingRect()?

Similar Threads

  1. selection of QGraphicsItem
    By navi1084 in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 06:33
  2. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 15:37
  3. Replies: 2
    Last Post: 28th June 2008, 16:31
  4. Is it possible to use QPainter with QGraphicsItem?
    By william.dias in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2008, 10:19
  5. Beesoft Tracer - remote/local debugging
    By piotrek in forum Qt-based Software
    Replies: 0
    Last Post: 2nd July 2007, 07:38

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.