Position in scene coordinates
-
Hi,
I am learning about graphic view, I have written simple code to check how it behave and I don't know how to get position of items in scene coordinates:/
@Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene(-200,-200,400,400);
ui->graphicsView->setScene(scene);
QBrush redBrush(Qt::red);
QBrush blueBrush(Qt::blue);
QPen blackpen(Qt::black);
blackpen.setWidth(6);
rec=scene->addRect(175,-25,50,50,blackpen,blueBrush);
scene->addRect(-225,-25,50,50,blackpen,blueBrush);
scene->addRect(-25,175,50,50,blackpen,blueBrush);
scene->addRect(-25,-225,50,50,blackpen,blueBrush);ellipse=scene->addEllipse(-50,-50,100,100,blackpen,redBrush); rec->setFlag(QGraphicsItem::ItemIsMovable); qDebug()<<ellipse->scenePos(); qDebug()<<ellipse->pos();
}@
Qt documentation says about pos():
bq. .., If the item has no parent, its position is given in scene coordinates.
The position of the item describes its origin (local coordinate (0, 0)) in parent coordinates; this function returns the same as mapToParent(0, 0).
For convenience, you can also call scenePos() to determine the item's position in scene coordinates,..but qDebug shows for both (pos and scenePos) "QPointF(0, 0)" I suppose its point from local scene.
-
Hi,
When you add the rects with addRect you are defining the size of each rect, but not the position.
The pos() function will give a position relative to the item's parent, which in the case of your code is the scene, so they're the same.
If you create two rects A and B and make B a child of A, then add rect A to the scene, you can move B and see that the functions pos() and scenePos() are different.
Hope that helps ;O)
-
ok, i'll check this:). it sounds good.