Results 1 to 2 of 2

Thread: Painting problem for QGraphicsItem

  1. #1
    Join Date
    Jun 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Painting problem for QGraphicsItem

    I have to draw polygons on a scene. The polygon coordinates (in meters) are defined in a file like this:

    POLYGON LAYER 3
    21510.021 -77875.788 21767.976 -78175.582 22040.392 -78262.410
    22762.365 -78488.670 23023.361 -78840.834 23401.474 -78810.424
    23574.386 -78526.898 23190.596 -78121.994 22981.393 -77899.388
    22236.109 -77426.189 21510.021 -77875.788

    A Polygon resides in layers which can be any from 1 to 10.

    I create Layers which are QGraphicsItem and add them to scene using scene.additem().
    Then i create Polygons which are also QGraphicsItem and set the corresponding layer as its parent item.

    Code Polygon.cpp:
    Qt Code:
    1. Polygon::Polygon(QList<QPointF> *polygonPointList, qreal minX, qreal maxX, qreal minY, qreal maxY, QColor color)
    2. {
    3. _pen = new QPen();
    4. _pen->setColor(color);
    5. _rectWidth = maxX-minX;
    6. _rectHeight = maxY-minY;
    7. _rectX = -(_rectWidth)/2.0;
    8. _rectY = -(_rectHeight)/2.0;
    9.  
    10. _polygonPath = new QPainterPath();
    11. _polygonPath->moveTo(polygonPointList->takeAt(0));
    12.  
    13. while(!polygonPointList->isEmpty()){
    14. _polygonPath->lineTo(polygonPointList->takeAt(0));
    15. }
    16. }
    17.  
    18. QRectF Polygon::boundingRect() const
    19. {
    20. qreal adjustment = 2.000;
    21. return QRectF(_rectX-adjustment,_rectY-adjustment,_rectWidth+2*adjustment,_rectHeight+2*adjustment);
    22. }
    23.  
    24. void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    25. {
    26. painter->setPen(*_pen);
    27. painter->drawPath(*_polygonPath);
    28. return;
    29. }
    To copy to clipboard, switch view to plain text mode 

    Problem:
    The polygons get painted on the scene but when I scroll the view they don't get repainted correctly.I think
    this may be due to incorrect boundingRects. What is the problem with the code above??
    Last edited by wysota; 9th August 2011 at 08:02. Reason: missing [code] tags

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Painting problem for QGraphicsItem

    Why reinvent the weel? Just use QGraphicsPolygonItem.

Similar Threads

  1. specific painting for just one QGraphicsItem
    By jano_alex_es in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2011, 01:12
  2. QGraphicsItem - painting problem for zoomin
    By nileshsince1980 in forum Qt Programming
    Replies: 0
    Last Post: 26th March 2010, 09:02
  3. [SOLVED] Painting QGraphicsItem constant size
    By JovianGhost in forum Qt Programming
    Replies: 7
    Last Post: 22nd March 2010, 01:25
  4. Replies: 7
    Last Post: 21st March 2010, 03:11
  5. Force the painting of a QGraphicsItem
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2007, 21:28

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.