summaryrefslogtreecommitdiffstats
path: root/src/gui/itemmodels/qstandarditemmodel.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-05-05 22:45:01 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-09-02 22:51:42 +0200
commit39e07ebf64e115460e2a7573c13dde014c8e33ca (patch)
tree3d35b2b4ce3db83eba1f46b5bcb49c1f96f9faa5 /src/gui/itemmodels/qstandarditemmodel.h
parent19874d6a6386b55fe502e3a36a9adb6813b315dc (diff)
Long live QAIM::multiData!
Views / delegates absolutely *adore* hammering data(). A simple QListView showing a couple of dozens entries can call data() a hundred of times on the first show. Back of the hand calculation, * 2 times per visible item (sizeHint() + paint()), * times 9 roles used by the default delegate, * times 20 visible items = 360 as a bare minimum, assuming the view doesn't redraw twice accidentally. Move the mouse over the view, and that'll cause a full update with certain styles: 360 calls to data() per update. This has an overhead visible in profilers. The model's data() has to re-fetch the index from its data structure and extract the requested field every time. Also, QVariant is used for the data interexchange, meaning anything that won't fit in one is also a memory allocation. This problem will likely be gone in Qt6Variant as that will store sizeof(void*) * 3, meaning QImage/QPixmap and similar polymorphic classes will fit in a QVariant now... So I'm trying to to remove part of that overhead by allowing views to request all the data they need in one go. For now, one index a a time. A view might also store the data returned. The idea is that the same role on different indexes will _very likely_ return variants of the same type. So a model could move-assign the data into the variant, avoiding the memory allocation /deallocation for the variant's private. This patch: 1) Introduces QModelRoleData as a holder for role+data. 2) Introduces QModelRoleDataSpan as a span over QModelRoleData. The idea of a span type is twofold. First and foremost, we are in no position to choose which kind of container a view should use to store the QModelRoleData objects for a multiData() call; a span abstracts any contiguous sequence, leaving the view free to do whatever it wants (statically allocate, use a vector, etc.). It also solves the problem of efficient passing the roles and gathering the returned variants from multiData(). 3) Add multiData(), which populates a span of roles for a given model index. The main advantage here is that a model can fetch all the needed information for a given index just once, then iterate on the span and provide data for each requested role. Cf. this with data(), where every call has to re-fetch the information for the index. A couple of models have been ported to multiData(), as well as QStyledItemDelegate. [ChangeLog][QtCore][QModelRoleData] New class. [ChangeLog][QtCore][QModelRoleDataSpan] New class. [ChangeLog][QtCore][QAbstractItemModel] Added the multiData() function. Change-Id: Icce0d108ad4e156c9fb05c83ce6df5f58f99f118 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/gui/itemmodels/qstandarditemmodel.h')
-rw-r--r--src/gui/itemmodels/qstandarditemmodel.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/gui/itemmodels/qstandarditemmodel.h b/src/gui/itemmodels/qstandarditemmodel.h
index 3e0374d741..02274839cb 100644
--- a/src/gui/itemmodels/qstandarditemmodel.h
+++ b/src/gui/itemmodels/qstandarditemmodel.h
@@ -66,6 +66,7 @@ public:
virtual ~QStandardItem();
virtual QVariant data(int role = Qt::UserRole + 1) const;
+ virtual void multiData(QModelRoleDataSpan roleDataSpan) const;
virtual void setData(const QVariant &value, int role = Qt::UserRole + 1);
void clearData();
@@ -331,6 +332,7 @@ public:
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+ void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
bool clearItemData(const QModelIndex &index) override;