Question

So, I draw a QPolygonF in an area which I define. The area inherits from QGraphicsView.

What I want to be able to do is let user move around the control points of QPolygonF and alter the polygon even after it has been created. I couldn't really find references to how I could do this.

Since I draw lines to close and denote the polygon, and there can me a lot more things in the drawable area, clearing it and drawing it over and over doesn't seem right.

Does anyone know how I could solve this ?

Thanks !

Was it helpful?

Solution

I'm a little confused as to why you describe that your 'area' inherits from QGraphicsView, but then discuss QPolygonF. If you're using QGraphicsView, that implies that you have a QGraphicsScene and would be using QGraphicsPolygonItem.

However, with the assumption that the QGraphicsView is irrelevant here, QPolygonF is simply a class that inherits from a vector of points; QVector.

You should be able to iterate through the points and just move them wherever you want. You can get a point like this: -

QPolygonF poly; // assume it has been given a number of points
QPointF& point = poly[index]; // where index is the index into the QVector of points

Then you can move the point: -

point.SetX(xPos);
point.SetY(yPos);

Redrawing the item as a point is moved should not be a problem.

OTHER TIPS

Clearing it and redrawing the whole scene is probably the most effective way to do this. The only other option would be to try to calculate the min bounding rect of the polygon when it changes and try to invalidate only that region, but it's effectively the same thing. Qt drawing is pretty darn fast, especially when you take advantage of the opengl integration. When I start worrying about redrawing the whole scene, I remind myself that video games do it all the time. :P

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top