How do add I rectangle to any position in a QGraphicsView?
-
Hello.
I am trying to make a clone of the game Snake. To draw game elements, I was planning on using a QGraphicsView using a UI file. I was able to draw a single rectangle, however, I cannot figure out how to place it to any coordinate I would like.
# Code I wrote. scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QPen col(Qt::white); QRect rect(200, 200, 25, 25); # Shouldn't this place the rectangle at 200 at x and y? scene->addRect(rect, col);
Output:
The alignment of the QGraphicsView window is set to the top-left, however, I don't want to use this property. I want to be able to place the rectangle anywhere within this view, regardless of the alignment, using code. Please, how would I be able to do this? Thank you.
-
-
Never mind, I found out I needed to add to use setSceneRect() on the QGraphicsView window. I also found out that using Items instead of regular QRects allow moving allows moving the rectangle easily.