gpt4 book ai didi

c++ - 理解QT的图形 View 坐标系

转载 作者:行者123 更新时间:2023-11-30 05:34:07 26 4
gpt4 key购买 nike

我有以下代码:

QGraphicsScene* pScene = new QGraphicsScene(this);
ui->graphicsView->setScene(pScene);
pScene->addRect(0, 0, 200, 200);
QGraphicsRectItem* pRect1 = pScene->addRect(40, 40, 100, 100);
QGraphicsRectItem* pRect2 = new QGraphicsRectItem(20, 20, 19, 19, pRect1);
QPointF pf1 = pRect1->pos();
QPointF pf2 = pRect2->pos();
QPointF pf3 = pRect2->mapFromParent(pRect1->pos());
QPointF pf4 = pRect2->mapToParent(pRect2->pos());
QPointF spf1 = pRect1->scenePos();
QPointF spf2 = pRect2->scenePos();

没什么特别的,只是一个带有 QGraphicsScene 和一些 QGraphicsRectItem 的 QGraphicsView。问题:为什么所有的点(pf1、pf2、pf3、pf4甚至spf1、spf2)执行后都等于QPointF(0.0, 0.0)?

我正在使用 Qt 5.4.1。

根据我的理解,spf1 必须是 QPointF(40.0, 40.0),spf2 必须是 QPointF(20.0, 20.0)。

最佳答案

如果您查看文档,您会发现:

QGraphicsRectItem *QGraphicsScene::addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush())

Creates and adds a rectangle item to the scene, and returns the item pointer. The geometry of the rectangle is defined by rect, and its pen and brush are initialized to pen and brush. Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the items coordinate system

.因此,Item 的坐标为 (0,0),但您可以将其设置为:

pRect1->setPos(QPoint(30,30));

看看会发生什么。希望对大家理解QGraphicsScene坐标系有所帮助!

附言:

QGraphicsRectItem* pRect1 = pScene->addRect(0, 0, 100, 100);
QGraphicsRectItem* pRect2 = pScene->addRect(0, 0, 20, 20);
pRect1->setPos(QPoint(40,40));
pRect2->setPos(QPoint(20,20));

关于c++ - 理解QT的图形 View 坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34403745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com