过滤QGraphicsItems类型的items()中的场景

时间:2020-06-20 16:01:43

标签: qt qgraphicsscene qgraphicsitem

我正在寻找一种使用items()从场景中获取所有QGraphicsLineItem的方法。

它给出了所有QGraphicsItem的列表,但是例如,我只想对QGraphicsLine执行操作。如何排序此列表/提取特定类型的项目?

1 个答案:

答案 0 :(得分:1)

您可以浏览列表并使用dynamic_cast测试类型:

// I'm not sure where you want to implement this,
// but it can be in your derived GraphicsScene class.
// Else, just call scene->items() and make a new list
// outside of the scene class.
void CustomScene::foo()
{
    QList<QGraphicsItem*> itemList = items();
    for (int i = 0; i < itemList.size(); ++i) {
         if (auto lineItem{dynamic_cast<QGraphicsLineItem*>(itemList[i])})
             // do a specific action or put this in a QList of QGraphicsLiteItem*s
             // and return that list instead of void
             ;
    }
}

我还没有测试过这段代码,但是这样的事情应该是可能的。

相关问题