diff options
| author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2025-07-10 17:31:41 +0200 |
|---|---|---|
| committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2025-12-04 14:29:20 +0100 |
| commit | e22cd01076e795ed853b0536605d3bb205587d78 (patch) | |
| tree | f30f5ce0c6103baf62448a9b76474715545bdf40 /src/corelib/kernel/qmetaobjectbuilder.cpp | |
| parent | ba5f7153a0e0d4aa9ebaa52f5c4507e5da974bf5 (diff) | |
Add QRangeModelAdapter: C++-style access to the range while talking QAIM
If a QRangeModel represents a C++ range, then the C++ range must no
longer be modified directly, as clients of the model won't be notified
about data or structural changes. Ignoring this (documented) warning
might end up with views not presenting the data correctly, or
even result in crashes as the model cannot update QPersistentModelIndex
instances.
Modifying the range through the QAbstractItemModel API is ok, but
clumsy, as it requires dealing with QModelIndex and QVariant for basic
operations.
QRangeModelAdapter provides an easy, type safe, and data-structure aware
API for reading and also modifying a range that a QRangeModel operates
on. This includes an interator API for rows, and - unless the range is
a list - columns. Dereferencing row iterators yields a row reference
type from which a row can be accessed for reading, or that a new row
can be assigned to. Dereferencing a const column iterator yields an
item; dereferencing a mutable column iterator yields a reference type
that a new item value can be assigned to.
Since QRangeModel itself is not a template class (so we don't know the
type of the range anymore once it has been created), we have to create
the adapter from the range (and optional protocol), which then
implicitly creates the model. Constructing the adapter implicitly
constructs the model, which is owned by the adapter. QRangeModelAdapter
is a value type, using std::shared_ptr for the model so that all copies
of the adapter operate on the same model.
To be able to set entire multi-role objects as items, introduce a new
Qt::ItemDataRole enum value, Qt::RangeModelAdapterRole. This is very
similar to Qt::RangeModelDataRole, but QML has specific requirements
that QRangeModelAdapter doesn't have, and we want to pass items back and
forth without modifying their value category - ie. an item that is a
shared_ptr<Object> is not useful for QML (which needs an Object *), but
a C++ user expects to get a shared_ptr<Object> from a call to at(), and
also expects to be able to set such an item.
The code has room for de-duplicating some logic in follow-up commits.
[ChangeLog][Core] Added QRangeModelAdapter for C++-style access to a
range used in a QRangeModel, while implementing QAbstractItemModel
protocol.
Change-Id: I3f2f94cb51b850100590fbe2c9a7c9dabbec59bd
Reviewed-by: Artem Dyomin <artem.dyomin@qt.io>
Diffstat (limited to 'src/corelib/kernel/qmetaobjectbuilder.cpp')
0 files changed, 0 insertions, 0 deletions
