Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't move QGraphicsEllipseItem



  • Hi, I'm trying to draw a circle, and the move it 150 pixels on the X axis. Here is the simplified code:

    QGraphicsScene scene;
    QGraphicsEllipseItem circle;
    
    scene.addItem(&circle);
    circle.setRect(150,-0,200,200);    
    circle.setPos(150,0);
    circle.moveBy(150,0);
    circle.setX(150);
    

    The value 150 in the four lines do nothing. What's the correct way to move the item ?


  • Moderators

    circle.moveBy(150,0);

    This moves your circle so x == 300.

    circle.setX(150);

    This moves it back to x == 150.

    So it all works correctly (unless I misunderstood you).



  • @sierdzio The last four lines are my attempts to move the circle, but none work. The circle is still in the center of the scene.


  • Moderators

    All look correct. Can you show relevant parts of your real code? Perhaps there is something wrong there.



  • @sierdzio I just found out the problem. I was only experimenting with a single circle, and it seems that after the translation, the whole scene is moved so that its contents are centered.
    When adding a second circle at 0,0 I now can notice the first one is indeed translated. The center of the scene is now the point between the circles.
    Is there a way to fix the scene, so that it stops centering its contents ?



  • @cpper

    Set a static sceneRect to your QGraphicsScene.

    graphicsView->scene()->setSceneRect(0, 0, graphicsView->width(), graphicsView->height());
    


  • @Pl45m4 said in Can't move QGraphicsEllipseItem:

    graphicsView->scene()->setSceneRect(0, 0, graphicsView->width(), graphicsView->height());

    Thanks guys. That works fine.


Log in to reply