PDA

View Full Version : Painting problem for QGraphicsItem



vivekpurohit
9th August 2011, 07:00
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:

Polygon::Polygon(QList<QPointF> *polygonPointList, qreal minX, qreal maxX, qreal minY, qreal maxY, QColor color)
{
_pen = new QPen();
_pen->setColor(color);
_rectWidth = maxX-minX;
_rectHeight = maxY-minY;
_rectX = -(_rectWidth)/2.0;
_rectY = -(_rectHeight)/2.0;

_polygonPath = new QPainterPath();
_polygonPath->moveTo(polygonPointList->takeAt(0));

while(!polygonPointList->isEmpty()){
_polygonPath->lineTo(polygonPointList->takeAt(0));
}
}

QRectF Polygon::boundingRect() const
{
qreal adjustment = 2.000;
return QRectF(_rectX-adjustment,_rectY-adjustment,_rectWidth+2*adjustment,_rectHeight+2*a djustment);
}

void Polygon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(*_pen);
painter->drawPath(*_polygonPath);
return;
}

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??

MarekR22
9th August 2011, 10:55
Why reinvent the weel? Just use QGraphicsPolygonItem.