QPixmap and QGraphicsScene
-
Hello,
I'd like to draw hexagons for a gameboard. Now I was finally able to see some background images but not I face theproblem that the image in the 2nd line has not the correct image displayed. It's moved somehow.
Moreover, how to fill QPolygonF with the next hexPoints again?Here is here code:
QGraphicsScene *scene = new QGraphicsScene(this); QPixmap my_small; my_small.load("ore.png"); // load first image and next scale it to 70x70 size my_small = my_small.scaled(QSize(70,70)); QBrush *myblacck = new QBrush(my_small); // set the image as brush QPixmap my_small_wood; my_small_wood.load("wood.png"); // secound image same size 70x70 my_small_wood = my_small_wood.scaled(QSize(70,70)); QBrush *Brush_wood = new QBrush(my_small_wood); QPen outlinePen(Qt::black); ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft); QVector<QPointF> hexPoints; 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,*myblacck); hexPoints.clear(); hexPoints << QPointF(70,50) <<QPointF(35,70) <<QPointF(35,100) << QPointF(70,120) <<QPointF(105,100) <<QPointF(105,70); my_polygon.clear(); QPolygonF new_poly(hexPoints); //my_polygon.???(hexPoints); scene->addPolygon(new_poly,outlinePen,*Brush_wood); // this one fails the wood image.. hexPoints.clear(); hexPoints << QPointF(105,0) <<QPointF(70,20) <<QPointF(70,50) << QPointF(105,70) <<QPointF(140,50) <<QPointF(140,20); my_polygon.clear(); QPolygonF new_poly2(hexPoints); scene->addPolygon(new_poly2,outlinePen,*Brush_wood); ui->graphicsView->setScene(scene); // write it all in QGraphicsView
I read about QGraphicsPixmapItem, should I use this? If yes, how?
-
I only see you adding one polygon in your code, but I see three polygons in your screenshot. Is this all the code?
-
@Lunarix said in QPixmap and QGraphicsScene:
That should be all of it. I call scene->addPolygon three times with different hexpoints so this should draw all 3 hexagons
Sorry, I missed the scroll bar.
The way you do it, you apply your pixmap as a texture to your object. How does Qt decide where to place the texture? Simple: By default, the top-left of your pixmap starts at the position - pos() - of your GraphicsItem.
All your polygons have a default position, which is 0,0. They draw in different places, because you add the polygon points elsewhere. Think of your polygons like three sheets of paper: On the first sheet, you draw the polygon in the top left corner. On the second sheet to draw the polygon further to the right. And on the third sheet, you draw it further down, and somewhat to the right. Then you stack the sheets exactly on top of each other. That means that the origin of the texture is exactly the same.
Solution:
- Create only one QPolygonF (this one: QPointF(70,20) <<QPointF(35,0) <<QPointF(0,20) << QPointF(0,50) <<QPointF(35,70) <<QPointF(70,50))
- Apply the same QPolygonF to all your GraphicsItems, but move them where you want using setPos
-
@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? -
@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.