Search code examples
qtqpolygon

QGraphicsPolygonItem - How to change Points coordinates


I'm working on QT since last week and I'm new to graphics management.

I have a polygon that represents 4 pressure graphically: How can I refresh the polygon with the new pos position?

double deltaPos_A = vshp->cpu_v.paramCpuToVisu.liftData[0].pos - vshp->cpu_v.paramCpuToVisu.lift.hMin;
double deltaPos_B = vshp->cpu_v.paramCpuToVisu.liftData[1].pos - vshp->cpu_v.paramCpuToVisu.lift.hMin;
double deltaPos_C = vshp->cpu_v.paramCpuToVisu.liftData[2].pos - vshp->cpu_v.paramCpuToVisu.lift.hMin;
double deltaPos_D = vshp->cpu_v.paramCpuToVisu.liftData[3].pos - vshp->cpu_v.paramCpuToVisu.lift.hMin;

double kPos_X = 180 / (vshp->cpu_v.paramCpuToVisu.lift.hMax - vshp->cpu_v.paramCpuToVisu.lift.hMin);
double kPos_Y = 295 / (vshp->cpu_v.paramCpuToVisu.lift.hMax - vshp->cpu_v.paramCpuToVisu.lift.hMin);

graphic_Press->polygon()[0].setX(-deltaPos_A*kPos_X);
graphic_Press->polygon()[0].setY(-deltaPos_A*kPos_Y);

graphic_Press->polygon()[1].setX(deltaPos_B*kPos_X);
graphic_Press->polygon()[1].setY(-deltaPos_B*kPos_Y);

graphic_Press->polygon()[2].setX(deltaPos_C*kPos_X);
graphic_Press->polygon()[2].setY(deltaPos_C*kPos_Y);

graphic_Press->polygon()[3].setX(-deltaPos_D*kPos_X);
graphic_Press->polygon()[3].setY(deltaPos_D*kPos_Y);

graphic_Press->polygon()[4].setX(-deltaPos_A*kPos_X);
graphic_Press->polygon()[4].setY(-deltaPos_A*kPos_Y);

Here are the declarations ("graphic_Press_Polygon" is a QPolygonF with 5 points):

QGraphicsPolygonItem* graphic_Press;

graphic_Press = mSceneMSPE->addPolygon(graphic_Press_Polygon,greenPen,trasparentBrush);

The new polygon doesn't refresh into the scene. How can I do?


Solution

  • To set a new polygon for your QGraphicsPolygonItem you need to call setPolygon().

    Note that calling polygon gives you a copy of the polygon used by the QGraphicsPolygonItem. Modifying this copy does not have any effect on the QGraphicsPolygonItem. After your modifications you need to call setPolygon().