PDA

View Full Version : How to find out what physical pixels of an image in a QGraphicsItem are displayed



dcole
21st January 2011, 21:56
lets say I have a QGraphicsView that is 50 x 50, and a QGraphicsItem that is 100x100.

Obviously the whole image will not fit in the view at any given time.

I can pan the QGraphicsItem around, scale it, etc etc in the window.

Is there any way to derive which physical pixels of the original image are being displayed at any given time?

For example - if I have panned all the way to the right, it might be the pixels 50-100 in the x direction, and 0-50 in the Y direction. If I have zoomed out some, it may be that ALL of the image is now in the viewer.

totem
22nd January 2011, 01:07
(1) You can compute pixel metric. That is, in QGraphicsItem's local coordinates "1 pixel is x (qreal) long and y (qreal) tall" (I wonder if it's not as simple as 1=1.0)
Then, according to your image dimensions (in pixels), you know that a QPointF given in item's coordinate match with a pixel in your image (apply pixel metric and cast it to QPoint).

(2) Now, given QPoints in view's coordinate system (for instance its viewport corners), map it to the GraphicsItem local coordinate system and make (1). This should give you the corresponding image rect currently visible.

(3) As you said, zooming out might make the image fully visible in the viewer (and maybe surrounded with 'empty' space); In this case mapping your viewport to GraphicsItem local coordinates will compute out-of-bounds pixel indices. But this is the idea

wysota
22nd January 2011, 17:37
What do you need this information for?

dcole
22nd January 2011, 18:52
I am trying to basically show lat/long coordinates along with a map that is displayed. If you zoom in on the map, you are now viewing a smaller geographic region, zoom out, bigger region

SixDegrees
22nd January 2011, 19:38
I am trying to basically show lat/long coordinates along with a map that is displayed. If you zoom in on the map, you are now viewing a smaller geographic region, zoom out, bigger region

Have a look at QGraphicsView.sceneRect().

wysota
23rd January 2011, 12:27
I am trying to basically show lat/long coordinates along with a map that is displayed. If you zoom in on the map, you are now viewing a smaller geographic region, zoom out, bigger region

Still I don't see what you need such an information for. Anyway:

QRectF visibleRect = view->mapToScene(view->viewport()->rect());