Results 1 to 19 of 19

Thread: QGraphicsItem leaves junk on screen

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QGraphicsItem leaves junk on screen

    Hello,
    I am developing an electric cad software with qt4.2 and obviously using QGraphicsView framework
    The problem I am facing is the Wire item - subclass of QGraphicsItem leaves junk on screen when its length or path of wire change rapidly. (Junk ,in the sense, it leaves behind trails of lines on screen). The screenshot attached tells it better.
    Actually this happens when component is moved madly fast. But since my system is pretty high end and sincethere is every possibility of lots of items to be on view, I don't wan't to take a chance.

    The length/path of wire changes when any component connected to wire moves which makes it necessary for the wire to regrow.
    Currently I am achieving this by calling Wire::rebuild in component's itemChange()
    I guess QGraphicsItem:: prepareGeometryChange() is the culprit behind this. Or may be I am doing something wrong in boundingRect() or shape().
    How can I avoid this ?

    Qt Code:
    1. void Wire::rebuild(const QPointF& s, const QPointF& e)
    2. {
    3. //QList<QLineF*> m_lines is member variable
    4. if(!m_lines.isEmpty())
    5. {
    6. qDeleteAll(m_lines);
    7. m_lines.clear();
    8. }
    9.  
    10.  
    11. QPointF st = mapFromScene(s);
    12. QPointF en = mapFromScene(e);
    13.  
    14. if(st.x() == en.x() || st.y() == en.y())
    15. {
    16. m_lines.append(new QLineF(st,en));
    17. prepareGeometryChange();
    18. return;
    19. }
    20.  
    21. QPointF inter = QPointF(st.x(),en.y());
    22. m_lines.append(new QLineF(st,inter));
    23. m_lines.append(new QLineF(inter,en));
    24. prepareGeometryChange();
    25. }
    26.  
    27. QRectF Wire::rectForLine(const QLineF& line) const
    28. {
    29. qreal x = qMin(line.p1().x() , line.p2().x());
    30. qreal y = qMin(line.p1().y() , line.p2().y());
    31. qreal w = qAbs(line.p1().x() - line.p2().x());
    32. if(w < 1.0)
    33. w = 1.0;
    34. qreal h = qAbs(line.p1().y() - line.p2().y());
    35. if(h < 1.0)
    36. h = 1.0;
    37. return QRectF(x,y,w,h);
    38. }
    39.  
    40. QRectF Wire::boundingRect() const
    41. {
    42. QRectF rect(0.0,0.0,0.0,0.0);
    43. foreach(QLineF* line, m_lines)
    44. rect |= rectForLine(*line);
    45. return rect.adjusted(-1.0,-1.0,1.0,1.0);
    46. }
    47.  
    48. QPainterPath Wire::shape() const
    49. {
    50. if(m_lines.isEmpty())
    51. return path;
    52. foreach(QLineF *line, m_lines)
    53. path.addRect(rectForLine(*line));
    54. return path;
    55. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Last edited by Gopala Krishna; 14th December 2006 at 17:24. Reason: updated contents

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.