Important: Please read the Qt Code of Conduct -

QGraphicsItem positioning

  • Hi,

    I have a QGraphicsEllipseItem object, which is randomly positioned, and I would like it not to go beyond the QGraphicsView borders, which are the same of the Window.

    I think I should make the QGraphicsScene exactly the same size of the QGraphicsView, but so far I failed to do that, and I am not sure that this is the correct way to do it.

    Does anyone have a suggestion how to do it? I would appreciatte any reference too.


  • @canellas
    how about passing the QGraphicsScene::sceneRect() to QGraphicsView::fitInView() ?

  • Thanks for your time!

    I tried to combine the two methods, but I failed to. Would you have an example?


  • Hi @canellas

    The "extension" of the graphics scene is the combined extension of all its objects (ellipses). In the graphics view you can set the area (rect) of the graphics scene that you want it to show with: setSceneRect(rect);

    So you will have to define a rectangle in which to vary the ellipses and set the view to show this rectangle; and make the widget big enough to show the complete view, otherwise you will get scrollbars.


  • Thanks for your time.

    That's pretty much what I am doing.

    I set the QGraphicsView to show no scroll bars, and the QGraphicsScene width and height to 95% of the QGraphicsView's size.

  • @canellas
    I put an example on github
    is this what you want?

Log in to reply