Important: Please read the Qt Code of Conduct -

QGraphicsView should show a Rect as large as possible

  • Hello,

    I have a simple status display, I implemented as a QGraphicsScene. Basically a bunch of light (QGraphicsEllipseItems) with different colours and texts. Now I want to show this display in a QGraphicsView.

    I want to:

    • keep the aspect ratio of the scene
    • view all the items of the status display at once
    • want the display to be as large as possible (= zoom into the scene as far as possible without any items leaving the view)
    • always keep these constraints (even on resize events and such).

    I do not want:

    • manual zooming by the user.
    • scrollbars

    I thought this task should be simple. Like: ok there is this rect (a,b,w,h) and this should be shown entirely but as large as possible. However I didn't find a simple way to do so. Or no reliable way, at all. I tried fitInView (called once after setting up the scene and adding it to the view and after each resize event), but while it ensures that all items are visible, it doesn't ensure they are as large as possible. Or even reasonable large. I had occasions where my display populated less than 10% of the QGraphicsView. So I tried to zoom into the scene with a large factor and then call fitInView. This works in most cases, but sometimes, for some strange reason, some items are not displayed. Additionally it is kind of an awful hack.
    Any ideas or comments?



Log in to reply