1

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 !

navderm
  • 799
  • 2
  • 11
  • 33

2 Answers2

2

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.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Yeah thats exactly what came to my mind but since I want a mouse movement to dictate how any point moves in the polygon. I didn't want to end up drawing everything again and again as the mouse moved. SO I was wondering if there is a way to draw only 1 polygon (with new points) removed the previous instance of that very polygon (old points) and nothing else. – navderm Oct 24 '13 at 18:51
  • By updating the points of the Polygon and calling update() on its Widget, only the widget and its contents will be redrawn. Video games used to use the same 'dirty rectangle' method until it was realised that it was just quicker and easier to redraw the whole screen with a back-buffer. Qt does the same, so just let Qt take care of it by calling update on the widget. If you're seeing problems redrawing, then you can look at how to optimise your code. – TheDarkKnight Oct 25 '13 at 07:42
0

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

Thadeux
  • 146
  • 1
  • 8
  • Can you please provide how do I invalidate a particular region. That in anyways still doesn't solve me having to draw other polygons as the area I invalidate might contain other polygons too. – navderm Oct 24 '13 at 18:52