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.

    Thanks



  • @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?

    Thanks



  • 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.

    -Michael.



  • 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
 

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