How to get the coordinate of QGraphicsEllipseItem's object ?

  • Hi..
    I try to get the coordinate of a moving circle that was made using QGraphicsEllipseItem on the scene, but the result always (0,0). I appreciate if there is any comment or suggestion, thank you...
    ... QGraphicsEllipseItem *myCircleItem = new QGraphicsEllipseItem(
    myCircleItem->setFlags (QGraphicsItem::ItemIsMovable);

    posisiCircleX = myCircleItem->pos().x();
    posisiCircleY = myCircleItem->pos().y();
    qDebug() << "coordinate myCircelItem (x,y) : " << posisiCircleX <<" , "<< posisiCircleY;


  • Moderators

    and you're calling pos() also after it has been moved?

  • There's two things here:
    The position - pos(), which you change using setPos. This is the position of the whole item relative to it's parent.

    And the rectangle - rect() which you change using setRect. This contains both the offset and size of the ellipse, relative to the position given.
    In your case, you only specify a rectangle, therefore the position is always zero.

    How to use correctly:
    Think about where you'd like to have the "origin point" of your ellipse or circle. In the center? In this case, use something like this:
    pEllipseItem->setRect(-20.0, -20.0, 40.0, 40.0);
    pEllipseItem->setPos(50.0, 80.0);@

    Now the center of the circle (in this case) will be at 50.0 / 80.0

  • Thanks that is solved using setRect and setPos as your suggestion.
    Now, the coordinate not (0,0) again. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.