Important: Please read the Qt Code of Conduct -

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) :
    ui(new Ui::Widget)
    scene = new QGraphicsScene(-200,-200,400,400);
    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackpen(Qt::black);



    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.

  • also one thing to point out, u should not do all of your stuff in the constructor of your class....,

Log in to reply