QT QGraphicsScene将原点设置为左下角

QT QGraphicsScene set origin to bottom left

本文关键字:左下角 设置 原点 QGraphicsScene QT      更新时间:2023-10-16

我正在编写一个场景编辑器来编辑iPhone游戏的关卡。iPhone/ipad环境中的原点是显示屏的左下角。

如何在 QGraphicsScene 对象中正确移动原点?我试过

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

但这行不通。

在不去一个复杂的(至少对我来说)变换矩阵的情况下,我应该如何移动原点?

编辑:

根据要求,以下是我如何创建场景并向其添加图像的简历:

// 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);

这是解决方案,至少就我而言。

将 0,0 放在左下角 -

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

然后,插入到场景中的所有图像都需要调整其Y位置:

pi->setPos(iPosX, -iPosY);

给我带来问题的最后一个小技巧是图像的客户端参考点是图像的中心而不是角落。

感谢所有在这里提供帮助的人。