3

Greetings,

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

I can't find any function to return me the rectangle of my current screen, i searched in all QGraphicsScene and QGraphicsView functions.
There are functions to return the scene rect (it returns a rect 10k X 10k but it's useless) and there are functions to return the mouse position, but i need to move my mouse and click on a menu so the mouse will no longer be inside the scene and the item will be generated outside the visible space...

Thank you very much.

Cristi Constantin
  • 554
  • 1
  • 6
  • 19

1 Answers1

6

You should map your top left corner (0,0) of your viewport to the scene. Width & height are equal to the viewport's rect width & height.

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

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

I assume you can handle it from here.

zkunov
  • 3,362
  • 1
  • 20
  • 17
  • Greetings and thank you for answer. Unfortunately it doesn't work. I tried graphicsView.viewport().pos() and graphicsView.viewport()->rect().center(), that's what i need to position my items on the scene, but it doesn't put them in the center. I am using setTransformationAnchor(QGraphicsView.AnchorUnderMouse) and setResizeAnchor(QGraphicsView.AnchorUnderMouse), but i also tried AnchorViewCenter and it still doesn't position them wright... – Cristi Constantin May 09 '11 at 06:39
  • 2
    @Cristi Constantin: Have you tried graphicsView.mapToScene(graphicsView.viewport()->rect().center()) to get the center point of the current screen, this should do the job. – zkunov May 09 '11 at 19:55