PDA

View Full Version : Scene Coordinates



Corny
25th July 2017, 01:19
I’m attempting to get a better handle on the Qt coordinate system by creating a scene that is 600 by 600 with the top left corner having an x value of -300 and a y value of 300. Whereas the bottom right corner x value would be 300 and the y value would be -300. The reason for doing this, is to have the center point x and y values be 0,0.


qreal xTL(-300);
qreal yTL(300);
QSizeF rectSize(600, 600);
QPointF sceneTopLeft(xTL, yTL);
QRectF sceneRect(sceneTopLeft, rectSize);

pScene = new QGraphicsScene();
pScene->setSceneRect(sceneRect);
ui->graphicsView->setScene(pScene);

QPointF rectMiddle = sceneRect.center();
QPointF rectTopLeft = sceneRect.topLeft();
QPointF rectBotRight = sceneRect.bottomRight();

qreal rectWidth = sceneRect.width();
qreal rectHeight = sceneRect.height();

When de-bugging I see the following values. I would expect the Y values for rectMiddle and rectBotRight to be 0 and -300 respectivly, yet they are double that!

Debug ouput...


Locals
parent 0x0 QWidget*
rectBotRight (300.0, 900.0) QPointF
rectHeight 600.0 qreal
rectMiddle (0.0, 600.0) QPointF
rectSize (600.0, 600.0) QSizeF
rectTopLeft (-300.0, 300.0) QPointF
rectWidth 600.0 qreal
sceneRect 600.0x600.0-300.0+300.0 QRectF
sceneTopLeft (-300.0, 300.0) QPointF
this @0x28fcbc MainWindow
xTL -300.0 qreal
yTL 300.0 qreal


So, what am I missing? Can anyone provide an explanation of this?

Thanks in advance.

I’m attempting to get a better handle on the Qt coordinate system by creating a scene that is 600 by 600 with the top left corner having an x value of -300 and a y value of 300. Whereas the bottom right corner x value would be 300 and the y value would be -300. The reason for doing this, is to have the center point x and y values be 0,0.


qreal xTL(-300);
qreal yTL(300);
QSizeF rectSize(600, 600);
QPointF sceneTopLeft(xTL, yTL);
QRectF sceneRect(sceneTopLeft, rectSize);

pScene = new QGraphicsScene();
pScene->setSceneRect(sceneRect);
ui->graphicsView->setScene(pScene);

QPointF rectMiddle = sceneRect.center();
QPointF rectTopLeft = sceneRect.topLeft();
QPointF rectBotRight = sceneRect.bottomRight();

qreal rectWidth = sceneRect.width();
qreal rectHeight = sceneRect.height();

When de-bugging I see the following values. I would expect the Y values for rectMiddle and rectBotRight to be 0 and -300 respectivly, yet they are double that!

Debug ouput...


Locals
parent 0x0 QWidget*
rectBotRight (300.0, 900.0) QPointF
rectHeight 600.0 qreal
rectMiddle (0.0, 600.0) QPointF
rectSize (600.0, 600.0) QSizeF
rectTopLeft (-300.0, 300.0) QPointF
rectWidth 600.0 qreal
sceneRect 600.0x600.0-300.0+300.0 QRectF
sceneTopLeft (-300.0, 300.0) QPointF
this @0x28fcbc MainWindow
xTL -300.0 qreal
yTL 300.0 qreal


So, what am I missing? Can anyone provide an explanation of this?

Thanks in advance.

Added after 14 minutes:

I guess I'm pretty obtuse :confused: when it comes to Qt's Y scale decreasing in the down direction, but I think that was my problem. Needed the top left corner to be -300, -300 not 300, -300.

Hopefully someone else may find this helpful.

d_stranz
25th July 2017, 02:17
I’m attempting to get a better handle on the Qt coordinate system by creating a scene that is 600 by 600 with the top left corner having an x value of -300 and a y value of 300. Whereas the bottom right corner x value would be 300 and the y value would be -300. The reason for doing this, is to have the center point x and y values be 0,0.

Needed the top left corner to be -300, -300 not 300, -300.

Well, these two statements are in conflict. You can't have a bottom right corner where the y coordinate is -300 if the y coordinate of the top left corner is also -300. This gives a width of 0.

I presume you meant that the top left should be (-300, -300) and bottom right (300, 300). This puts the center at (0,0). But you could also have had the same center if top left was (-300, 300) and bottom right was (300, -300). You couldn't use the constructor that you did (QPointF, QSizeF), because this assumes positive-going coordinates in screen orientation (positive y is down). However, the QRectF constructor that takes two QPointF arguments would let you invert the rectangle so positive y is up, like it is in a geometrical coordinate system.

Remember that the coordinate system for QGraphicsScene is independent of the views and can be in any coordinate scale you want. It is the mapping to the QGraphicsView that determines how it appears on screen.