2

I am writing a scene editor to edit levels for an iphone game. The point of origin within the iphone/ipad environment is the bottom left of the display.

How can I correctly move the origin within the QGraphicsScene object ? I have tried with

// iWidth and iHeight are maximum scene dimension values
pScene->setSceneRect(0, iHeight, iWidth, iHeight);

but this doesn't work.

Without going to a complicated (at least for me) transformation matrix, how should I be moving the origin point ?

EDIT :

As requested, here is a resume of how I am creating the scene and adding images to it :

// Creation of the Scene within a QMainWindow derived class
QGraphicsScene* m_pScene = new QGraphicsScene(this);
ui->levelScene->setScene(m_pScene);

// insertion of the background png image
QPixmap* pm = new QPixmap(strFileName, 0);
QGraphicsPixmapItem* pi = new QGraphicsPixmapItem();
pi->setPixmap(*pm);
m_pScene->addItem(pi);

// adjustment of the layout
m_pScene->setSceneRect(0, iHeight, iWidth, iHeight);

// add a movable png image to the scene above the background
pm = new QPixmap(*g_pChoData->m_pcDirImages + "/" + pChoElem->m_Image, 0);
pi = new QGraphicsPixmapItem();
pi->setPixmap(*pm);
pi->setPos(iPosX, iPosY);
pi->setZValue(iPosZ);
pi->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_pScene->addItem(pi);
Simon
  • 2,208
  • 4
  • 32
  • 47
  • This answer may be of use to you on [how to set QGraphicsScene / QGraphicsView to a specific coordinate system](http://stackoverflow.com/a/10444511/2167797). – Linville Jan 14 '14 at 15:48
  • @Linville: You should post this as an answer! – hmuelner Jan 14 '14 at 17:09
  • @Linville, yes I had already looked at this but when I start adding images into the scene, they do not appear in the correct positions. – Simon Jan 14 '14 at 17:11
  • @Simon, show an example of your code and we can help to work out why the images are not where you expect them to be. – TheDarkKnight Jan 14 '14 at 17:16
  • @Simon, the origin point is not only in a different place but the Graphics View Framework vertical axis is also inverted from your game environment (Graphics View vertical axis grows downward, the vertical axis of the environment you describe grows upward). I suspect the trouble is with iPosY variable (and this can probably be addressed by manipulating iPosY or using QGraphicsView::scale(1, -1)). – Linville Jan 14 '14 at 20:30
  • Have you tried `QGraphicsView::setAlignment`? `QGraphicsView` centers scene content by default. – Pavel Strakhov Jan 14 '14 at 20:53

1 Answers1

1

Here is the solution, at least in my case.

To put 0,0 at the bottom left -

m_pScene->setSceneRect(0, -iHeight, iWidth, iHeight);

Then, all images inserted into the scene need to have their Y position adjusted :

pi->setPos(iPosX, -iPosY);

Last little trick which caused me problems was that the clients reference point for an image is the center of the image and not a corner.

Thank you to all who helped here.

Simon
  • 2,208
  • 4
  • 32
  • 47