I’m trying to test QGraphicsView and QGraphicsScene. I’ve created an class, Brick, that inherits QGraphicsItem. The Brick class will draw a brick ar the coordinantes specified when calling constructor. Now i’m trying to fill the first row of the program with bricks. The Brick sprite is 32×32.
Brick class constructor:
Brick::Brick(int x, int y)
{
pixmap = new QPixmap("brickblock.jpeg");
setPos(x, y);
}
Window’s constructor:
for(int i = 0; i <= 768; i+=32){ // 24 bricks
Brick *abrick = new Brick(i, 0); // x = i; y = 0;
scene->addItem(abrick);}
The problem is that the program draws these bricks somewhere in the middle of the screen, not on the first row. The GraphicsView is occupying the entire screen.
Try using:
In short, this will tell the scene and the view that the top-left coordinate is (0, 0), and that the scene is 768 units wide by 768 units tall. Otherwise the scene and view will consider (0, 0) to be the middle coordinate.