Speed up the removal of items from a QGraphicsScene
When using a linear index, all items in a scene are stored in a QList. While adding new items is a constant operation, removal requires a traversal through the entire list. This is especially problematic when the scene contains millions of items and many of them are removed, which requires a linear search for each item, resulting in a very slow operation. Moreover, this behavior is actually inconsistent with the current documentation which states for the linear index: "Adding, moving and removing items, however, is done in constant time." Instead of removing items from the list in the index, this patch just marks the list as invalid. The next time the list is required it will be rebuilt from scratch by traversing all items from the scene. This new behavior more accurately matches the documentation. Testing this change in a scene with over 1 million objects, resulted in a massive speed up, effectively eliminating the overhead of item removal. [ChangeLog][QtWidgets][QGraphicsScene] Speed up the removal of items when using the linear index. Change-Id: I95c7b90b9f1fe426018695b6429138530e6d2f3e Reviewed-by: Andreas Aardal Hanssen <andreas@hanssen.name>
This commit is contained in:
parent
4e7baaaa91
commit
bf2ec0183c
@ -313,6 +313,7 @@ private:
|
||||
friend class QGraphicsEffect;
|
||||
friend class QGraphicsSceneIndex;
|
||||
friend class QGraphicsSceneIndexPrivate;
|
||||
friend class QGraphicsSceneLinearIndex;
|
||||
friend class QGraphicsSceneBspTreeIndex;
|
||||
friend class QGraphicsSceneBspTreeIndexPrivate;
|
||||
friend class QGraphicsItemEffectSourcePrivate;
|
||||
|
@ -70,31 +70,62 @@ class Q_AUTOTEST_EXPORT QGraphicsSceneLinearIndex : public QGraphicsSceneIndex
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QGraphicsSceneLinearIndex(QGraphicsScene *scene = 0) : QGraphicsSceneIndex(scene)
|
||||
QGraphicsSceneLinearIndex(QGraphicsScene *scene = 0) : QGraphicsSceneIndex(scene), m_itemsIsValid(true)
|
||||
{ }
|
||||
|
||||
QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const
|
||||
{ Q_UNUSED(order); return m_items; }
|
||||
{
|
||||
Q_UNUSED(order);
|
||||
return validList();
|
||||
}
|
||||
|
||||
virtual QList<QGraphicsItem *> estimateItems(const QRectF &rect, Qt::SortOrder order) const
|
||||
{
|
||||
Q_UNUSED(rect);
|
||||
Q_UNUSED(order);
|
||||
return m_items;
|
||||
return validList();
|
||||
}
|
||||
|
||||
protected :
|
||||
virtual void clear()
|
||||
{ m_items.clear(); }
|
||||
{
|
||||
m_items.clear();
|
||||
m_itemsIsValid = true;
|
||||
}
|
||||
|
||||
virtual void addItem(QGraphicsItem *item)
|
||||
{ m_items << item; }
|
||||
{
|
||||
if (m_itemsIsValid)
|
||||
m_items << item;
|
||||
}
|
||||
|
||||
virtual void removeItem(QGraphicsItem *item)
|
||||
{ m_items.removeOne(item); }
|
||||
{
|
||||
Q_UNUSED(item);
|
||||
m_itemsIsValid = false;
|
||||
}
|
||||
|
||||
private:
|
||||
QList<QGraphicsItem*> m_items;
|
||||
mutable QList<QGraphicsItem*> m_items;
|
||||
mutable bool m_itemsIsValid;
|
||||
|
||||
QList<QGraphicsItem*>& validList() const
|
||||
{
|
||||
if (!m_itemsIsValid)
|
||||
{
|
||||
m_items.clear();
|
||||
|
||||
QList<QGraphicsItem*> stack = scene()->d_func()->topLevelItems;
|
||||
m_items << stack;
|
||||
while (!stack.isEmpty())
|
||||
{
|
||||
m_items << stack.last()->childItems();
|
||||
stack << stack.takeLast()->childItems();
|
||||
}
|
||||
m_itemsIsValid = true;
|
||||
}
|
||||
return m_items;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // QT_NO_GRAPHICSVIEW
|
||||
|
Loading…
Reference in New Issue
Block a user