Important: Please read the Qt Code of Conduct -

What is best approach to not draw items outside the QGraphicsView?

  • I have implemented both QGraphicsView and QGraphicsScene.

    I wanted to not draw the part of the QGraphicsItem which is outside the screen rectangle.

    I wanted something like this:

    But currently I am getting this:

    In short, according the above couple of photos, I wanted the red rectangle to not to be drawn outside the screen rectangle which is determined by the White Rectangle at the time of initialization.

  • You can make all your items children of a single custom QGraphicsItem or QGraphicsWidget, which is the only item directly inserted in the scene. If you set the flag ItemClipsChildrenToShape, no item will draw beyond the boundingRect of that parent item. Then, you need to keep the size of the item up-to-date if your visible area changes.

  • @Asperamanca Thanks, it is working the way I wanted.

Log in to reply