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

QGraphicsView scale Scene to only visible elements



  • I have a QGraphicsScene with some elements in it.

    I want the QGraphicsView scale to the content of QGraphicScene.

    This works well with:

    mGraphicsView->fitInView(mGraphicsView->scene()->sceneRect(),
                                Qt::KeepAspectRatio);
    

    Now in the QGraphicsScene some of its elements are not visible with

    setVisible(false)
    

    Is there a way to ignore the not visible objects and only fit to the visible ones?



  • @sandro4912
    I know nothing about this(!) But you pass mGraphicsView->scene()->sceneRect() as the rectangle for fitInView(), if you want to exclude non-visible objects can't you calculate the necessary rectangle accordingly and pass that instead?



  • yeah of course i could do that. I guess I could iterate over the visible objects and calc the visible rectangle out of that. I just thought maybe Qt already has some build in method for that to not invent the wheel new.


Log in to reply