Solved QPixmap and QGraphicsScene
-
@Asperamanca
thank you for your answer.
Currently I can not find any function "setPos".hexPoints << QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50); QPolygonF my_polygon(hexPoints); scene->addPolygon(my_polygon,outlinePen,*Brush_wood); // scene->addPolygon(my_polygon,outlinePen,*Brush_ore); this right from the first hex // scene->addPolygon(my_polygon,outlinePen,*Brush_wood); this at the lower right side ui->graphicsView->setScene(scene);
So now I have only one Polygon now. But how do I move it? Neither QPolygonF, QGraphicsView nor QGraphicsScene have a function setPos.
Thank you for your help :) -
Hi,
addPolygon returns a pointer to a QGraphicsPolygonItem. That's the element you want to call setPos on.
-
Helo @SGaist,
So I added this code:QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem; my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood); my_poly->setPos(70,70); // scene->addPolygon(my_poly,outlinePen,*Brush_wood); // ui->graphicsView->setScene(scene);
But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
Or do I have to add it to the scene again?
Thank you for your help -
@Lunarix said in QPixmap and QGraphicsScene:
QGraphicsPolygonItem * my_poly = new QGraphicsPolygonItem;
Why do you create a new instance here? It is not needed and you leak memory. Just do:
QGraphicsPolygonItem *my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood); my_poly->setPos(70,70);
"But now - who to add the QGraphicsPolygonItem to the ui->graphicsView->xxx?
Or do I have to add it to the scene again?" - isn't it already added to the scene if you call scene->addPolygon? -
@jsulm
Your Right, but my_poly is not set to the scene yet.
Currently I see only one hex, the one in the origin, but not the one which I moved -
@Lunarix Is my_poly visible?
From documentation (http://doc.qt.io/qt-5/qgraphicsscene.html#addPolygon):
"If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.". -
@jsulm
No it is not.
That is the result atm.
hexPoints << QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50); QPolygonF my_polygon(hexPoints); QGraphicsPolygonItem * my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood); my_poly->setPos(70,70); ui->graphicsView->setScene(scene);
and this is the code. The current hex is stored in scene and this is viewed, so i think my_poly has no influence yet.
-
ui->graphicsView->setScene(scene); my_poly->setPos(70,70);
works now. Because, as you said, it has to be shown before.
If I'd like to add 50+ hex, I have to make 50+ QGraphicsPolygonItem? Because it seems like one QGraphicsPolygonItem can change position of one item. -
@Lunarix Change x, y and my_polygon as needed inside the loop
for (int i = 0; i < 50); ++i) { QGraphicsPolygonItem *my_poly = scene->addPolygon(my_polygon,outlinePen,*Brush_wood); my_poly->setPos(x,y); }
Yes, each polygon is a polygon item, but you can reuse my_poly variable.
-
Works! Thanks a lot to everyone of you :)
Have a nice day