Find out which part of QGraphicsItem is visible in QGraphicsView

I am trying to figure out how to make a comparison between what pixels I currently see in QGraphicsView and the actual pixel of the image, which is QGraphicsItem.

For example, if my viewer is 50x50 and my image is 100x100, then when the image is uploaded and untouched, 0.0 pixel of the image corresponds to 0.0 pixel of the viewer.

Now, if I dragged the image to the right 10 pixels, then the top left 0.0 pixel of the image is now at (10.0), and I can only see columns 10-40 of the original image.

In addition, if the zoom scale is reduced to reduce the size of the image in the QGraphicsView area, perhaps 0.0 of the image will be displayed to 10.0 viewers, but 100 of 100 images can only be displayed at 40.40 in the viewer, because I scaled it so few.

I know that there are many display functions for QGraphicsItem, but I'm just not sure how to determine which parts are visible, and how to make this mapping between Item, Scene and View.

Thanks for any help

For reference. Far, here's what I tried: Get the shape of the element, display the shape on the mape scene, that from the scene, using the view, get the bounding rectangle of the path and find the upper left corner of this

Finished using the following:

    QRect portRect = ui->graphicsView->viewport()->rect();
    QRectF sceneRect = ui->graphicsView->mapToScene(portRect).boundingRect();
    QRectF itemRect = item->mapRectFromScene(sceneRect);

    QRectF isec = itemRect.intersected(item->boundingRect());

Thanks Stephen chu

+3
source share
1 answer
QRect portRect = yourGraphicsView->viewPort()->rect();
QRectF sceneRect = yourGraphicsView->mapToScene(portRect);
QRectF itemRect = yourItem->mapRectFromScene(sceneRect);

, , , .

+2

Source: https://habr.com/ru/post/1787010/


All Articles