PDA

View Full Version : QGraphicsScene Coordinate System



grayfox
4th January 2012, 13:56
I have the following code that render a 10 x 10 grid, I was expecting the grid start at {0, 0}, which should be the top left of the window, but the result is that {0, 0} is the center of the window.



void GameScene::drawBackground(QPainter *painter, const QRectF &rect)
{
int colCount = 10;
int rowCount = 10;
int tileWidth = 32;
int tileHeight = 32;

// clear background
painter->fillRect(rect, QBrush(Qt::black));

//painter->translate(rect.width() / -2.0, rect.height() / -2.0);

painter->setRenderHint(QPainter::Antialiasing, true);

QPen pen;
pen.setStyle(Qt::DotLine);
pen.setColor(QColor(Qt::gray));
painter->setPen(pen);

int x, y, x2, y2;

y = 0; //painter->window().top();
y2 = y + (rowCount * tileHeight);
for(int c = 0; c <= colCount; c++)
{
x = c * tileWidth;
x2 = x;

painter->drawLine(x, y, x2, y2);
}

x = 0; //painter->window().left();
x2 = x + (colCount * tileWidth);// painter->window().right();
for(int r = 0; r <= rowCount; r++){
y = r * tileHeight;
y2 = y;
painter->drawLine(x, y, x2, y2);
}
}

So, is this normal? Or I must translate the painter to have {0,0} be the top left corner?

7231

Lykurg
4th January 2012, 15:09
That is normal. You just have to align your scene at the top left. By default it is centered. See QGraphicsView documentation.

IshanFx
28th November 2015, 06:40
From this code you can set coordinate to top left corner

ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft);


replace graphicsView by your graphics view name :)

d_stranz
28th November 2015, 20:37
I am sure that the original poster will be overjoyed to read this. He has been waiting nearly 4 years for the answer.

WE-B-QT-N
27th February 2016, 20:34
Hello Just joined this forum. Really new to QT. So the original question and answer is helpful. I have been trying to figure out how to get the coordinates from the graphics view/graphics scene but very confusing. Myself I want the cords in center but nice to know you can set them different. So I now am doing searches to see if I can figure out how to get cords from the view HELP!