Qt add item to center of scene / view

Hi,

I am trying to add QGraphicsItem exactly to the center of the current screen.
My QGraphicsScene is very large (10k X 10K), so QGraphicsView cannot show it completely, there are scroll bars.

I can’t find any function to return the rectangle of my current screen, I searched all the functions of QGraphicsScene and QGraphicsView.
There are functions that return the rectangle of the scene (it returns the rectangle 10k X 10k, but it is useless), and there are functions to return the position of the mouse, but I need to move my mouse and click on the menu so that the mouse is no longer inside the scene and the element will be generated out of visible space ...

Many thanks.

+3
source share
1 answer

You should display the upper left corner (0,0) of your viewport on the stage. Width and height are equal to the width and height of the viewport.

To get the rectangle of the current screen, try the following:

QRect exposedRect(graphicsView.mapToScene(0,0).toPoint(), graphicsView.viewport()->rect().size());

I assume you can handle this here.

+5
source

All Articles