如何获得可见的项目边界框?

发布于 2025-01-31 13:55:24 字数 560 浏览 2 评论 0 原文

我有一个场景,其中包含许多 qgraphicSitem 项目(大约25000个项目),当我隐藏无用的项目时,我如何获得所有可见的项目bounding-box,以便我可以使用 fitinview 确保仅在视图中心中的可见项目。

我的场景

想要获取可见的物品bounding-box

I have a scene that contains many QGraphicsItem items(about 25000 items) , When I hide useless items, How Can I get all visible items bounding-box, so that I can use fitInView ensure visible item just in the view center.

My Scene
enter image description here

Wanted get visible items bounding-box
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2025-02-07 13:55:24

我认为没有任何功能可以限制仅获得可见项目的矩形。我将使用蛮力,对所有可见项目进行迭代并计算总边界矩形。例如,查看 qgraphicsScene :: itemboundingRect()的实现。此处https://code.woboq.org/qt5/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp.html#_ZNK14QGraphicsScene17itemsBoundingRectEv and add visibility check to it 。

QRectF visibleItemsBoundingRect() const
{
    QRectF boundingRect;
    const auto items_ = items();
    for (QGraphicsItem *item : items_)
    {
        if (item->isVisible()) // Note: this line was added to original implementation
            boundingRect |= item->sceneBoundingRect();
    }
    return boundingRect;
}

如果由于大量的看不见的物品,这种蛮力的性能并不令人满意,那么您可能应该在某处保留一些仅可见物品的列表,但您需要注意维护并每次显示或hide或hide或添加或添加或删除时进行更新现场的任何东西。然后,您可以更快地迭代如此较小的项目列表。但是,除非蛮力方法的性能真的很差,否则我不会这样。

I do not think there is any function for bounding getting rect of only visible items. I would use brute force, iterating over all visible items and calculating the total bounding rect. For example have a look at the implementation of QGraphicsScene::itemsBoundingRect() here https://code.woboq.org/qt5/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp.html#_ZNK14QGraphicsScene17itemsBoundingRectEv and add visibility check to it.

QRectF visibleItemsBoundingRect() const
{
    QRectF boundingRect;
    const auto items_ = items();
    for (QGraphicsItem *item : items_)
    {
        if (item->isVisible()) // Note: this line was added to original implementation
            boundingRect |= item->sceneBoundingRect();
    }
    return boundingRect;
}

If this brute force performance is not satisfactory due to large number of invisible items, then you should probably keep some list of only visible items somewhere but you need to take the care to maintain it and update it everytime you show or hide or add or remove anything to the scene. Then you could iterate over such smaller list of items much faster. But I would not go this way unless the brute force method performance is really bad.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文