开发者

finding out what portion of a QGraphicsItem is visible in a QGraphicsView

I am trying to figure out how to do the mapping between what pixels i can currently see in a QGraphicsView and the actual pixel in the image that is a QGraphicsItem.

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

Now, if I drag the image to the right 10 pixels, that 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.

Also, if I scale to zoom out, to make the image smaller that the QGraphicsView area, perhaps 0,0 of the image maps to 10,0 of the viewer, but 100,100 of the image might only map to 40,40 in the viewer, because i scaled it so small.

I know there are a lot of mapping functions for a QGraphicsItem, but I am just not sure how to detect whichs parts are visible, and how to do that mapping between the Item, Scene, and View.

Thanks for any help

For reference..so far, here is waht I have tried: Get item shape map the shape to the scene mape that from scene using the view get the bounding rect of the path and find the top left corner of that

Ended up using something like the following:

    QRect portRect = ui->graphicsView->viewport()->rect();
    QRectF sceneRect = ui->graphicsView->mapToScene(portRect).boundingRect();
    QRectF itemRect = item->mapRectFromScen开发者_StackOverflow中文版e(sceneRect);

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

Thanks stephen chu


QRect portRect = yourGraphicsView->viewPort()->rect();
QRectF sceneRect = yourGraphicsView->mapToScene(portRect);
QRectF itemRect = yourItem->mapRectFromScene(sceneRect);

You may want to do an intersection of the resulting rect with your item's actual rect since the viewport may bt larger than the item.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜