1

I'm working on a Qt project, the user draws points with mouse clicks. I'm trying to print the positions of the points using the following code. The program always prints the position as QPointF(0, 0).

QList<QGraphicsItem *> list = scene->items();
foreach( QGraphicsItem * item, list )
{
  qDebug()<<item->pos();
}
Harald Scheirich
  • 9,676
  • 29
  • 53
sh ze
  • 169
  • 2
  • 5
  • 14

2 Answers2

3

The documentation for QGraphicsScene::addEllipse states: -

Creates and adds an ellipse item to the scene, and returns the item pointer. The geometry of the ellipse 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).

The function is defined as: -

QGraphicsEllipseItem * QGraphicsScene::addEllipse ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

The rect passed to the function provides the local coordinates of the item and the item is positioned at (0,0).

You need to set the position of the item using the pointer returned from addEllipse and calling setPos()

QGraphicsEllipseItem* pEllipse = scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,QPen(), QBrush(Qt::SolidPattern));

pEllipse->setPos(xPos, yPos); // where xPos and yPos are the scene coordinates

When retrieving the item's position, although the function QGraphicsItem::pos will return the item's parents coordinates which may be scene coordinates if no parent exists, you'd be better off calling QGraphicsScene::scenePos to guarantee that the coordinates returned are actually scene coordinates, even if you add parent items at a later stage.

QList<QGraphicsItem *> list = scene->items();
foreach( QGraphicsItem * item, list )
{
  qDebug() << item->scenePos();
}
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Maybe I misunderstood something but I wrote same thing, isn't it? Same code I posted in comments when communicate with OP. – Jablonski Dec 08 '14 at 13:29
  • @Chernobyl, I believe you were trying to convey the same information (+1), but it is acceptable in SO to provide a similar answer if you believe that the answers already present are not complete or lack clarity. I assume the OP agreed here, which is why this was marked as the correct answer to the question. – TheDarkKnight Dec 08 '14 at 13:50
  • @Merlin069 I just thought that I did some mistake in my answer and you posted correct solution, but I couldn't figure out where is my mistake. But now I understood, so +1 for you too. – Jablonski Dec 08 '14 at 13:56
  • 1
    @Chernobyl, no worries. If I had spotted a mistake, I would have mentioned it, rather than answering. – TheDarkKnight Dec 08 '14 at 14:22
2

You should also setPos() to item and in this case these methods will give you correct output. I suppose that now your code is something like:

scene->addEllipse(x, y, w,h);

If you add ellipse, rect or something else you should to know that:

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

There is information about this in documentation. So you should setPos() by yourself after usage of addSomeItem()

You can check also this good answer.

As doc said:

returns the position of the item in parent coordinates. 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, regardless of its parent.

http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#pos

Community
  • 1
  • 1
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • I tried to use it but i still have the same problem. – sh ze Dec 08 '14 at 12:01
  • I've already set the position using this: scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,QPen(), QBrush(Qt::SolidPattern)); – sh ze Dec 08 '14 at 13:02
  • @shahdewai read my answer more careful and link to another answer read too. Pos is 0,0 , your output is correct, you need setPos `QGraphicsItem * it = scene->addEllipse(0, 0, rad*2.0, rad*2.0,QPen(), QBrush(Qt::SolidPattern));` `it->setPos(pt.x()-rad,pt.y()-rad);` – Jablonski Dec 08 '14 at 13:09