Qt 如何使用QGraphicsItem的局部坐标
我创建了一个自定义QGraphicsRecitem,并将其添加到QGraphicscene中。我想使用自定义矩形的局部坐标向其添加文本(即,添加到0,0会将项目放置在我的矩形的原点,无论它在我的场景中的何处) 当我尝试这样做时,它使用场景的坐标系,文本显示在矩形外。有什么想法吗Qt 如何使用QGraphicsItem的局部坐标,qt,qgraphicsview,qgraphicsitem,qgraphicsscene,Qt,Qgraphicsview,Qgraphicsitem,Qgraphicsscene,我创建了一个自定义QGraphicsRecitem,并将其添加到QGraphicscene中。我想使用自定义矩形的局部坐标向其添加文本(即,添加到0,0会将项目放置在我的矩形的原点,无论它在我的场景中的何处) 当我尝试这样做时,它使用场景的坐标系,文本显示在矩形外。有什么想法吗 PackageRect::PackageRect(QString PackageName, qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
PackageRect::PackageRect(QString PackageName, qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
:QGraphicsRectItem(x, y, w, h, parent)
{
QGraphicsTextItem *text = new QGraphicsTextItem(PackageName, this);
text->setPos(0,0);
}
对于setPos,做0,0,但x,y。 对我来说:
rectText::rectText(QString PackageName, qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent)
:QGraphicsRectItem(x, y, w, h, parent)
{
QGraphicsTextItem *text = new QGraphicsTextItem(PackageName, this);
text->setPos(x, y);
}
文本在框中。您尝试执行的操作通常用于“分组”项目中 有几个
mapTo*
函数,包括:
基本上,从中调用映射函数的上下文会更改映射调用的行为
如果您在QGraphicsItem
的子类中,并调用this->mapToItem(myRect,QPointF(0,0))代码>它将返回一个矩形坐标系的值
同样,如果您在QGraphicsItem
的子类中,那么您可以调用this->mapFromItem(myRect,QPointF(0,0))代码>它将返回一个原始项坐标系的值,其中0,0是矩形的左上角
以下是有关坐标系的更多文档:
但是,如果要在QGraphicsView
中对对象进行分组,请将它们分组,然后移动组
希望有帮助。对于x和y坐标,框在场景中的位置无关紧要,它位于矩形的原点。实际上,它使用场景的坐标系,而不是项目的局部坐标系。
QPointF mapToItem(const QGraphicsItem * item, const QPointF & point) const
QPointF mapFromItem(const QGraphicsItem * item, const QPointF & point) const
QPointF mapFromScene(const QPointF & point) const
QPointF mapToScene(const QPointF & point) const