Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgenericitemmodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/qsize.h>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \class QGenericItemModel
11 \inmodule QtCore
12 \since 6.10
13 \ingroup model-view
14 \brief QGenericItemModel implements QAbstractItemModel for any C++ range.
15 \reentrant
16
17 QGenericItemModel can make the data in any sequentially iterable C++ type
18 available to the \l{Model/View Programming}{model/view framework} of Qt.
19 This makes it easy to display existing data structures in the Qt Widgets
20 and Qt Quick item views, and to allow the user of the application to
21 manipulate the data using a graphical user interface.
22
23 To use QGenericItemModel, instantiate it with a C++ range and set it as
24 the model of one or more views:
25
26 \snippet qgenericitemmodel/main.cpp array
27
28 The range can be any C++ type for which the standard methods
29 \c{std::cbegin} and \c{std::cend} are implemented, and for which the
30 returned iterator type satisfies \c{std::forward_iterator}. Certain model
31 operations will perform better if \c{std::size} is available, and if the
32 iterator satisfies \c{std::random_access_iterator}.
33
34 \section1 Constructing the model
35
36 The range must be provided when constructing the model; there is no API to
37 set the range later, and there is no API to retrieve the range from the
38 model. The range can be provided by value, reference wrapper, or pointer.
39 How the model was constructed defines whether changes through the model API
40 will modify the original data.
41
42 When constructed by value, the model makes a copy of the range, and
43 QAbstractItemModel APIs that modify the model, such as setData() or
44 insertRows(), have no impact on the original range.
45
46 \snippet qgenericitemmodel/main.cpp value
47
48 As there is no API to retrieve the range again, constructing the model from
49 a range by value is mostly only useful for displaying read-only data.
50 Changes to the data can be monitored using the signals emitted by the
51 model, such as \l{QAbstractItemModel}{dataChanged()}.
52
53 However, if the range holds pointers to data, then the copy of the range
54 that the model operates on will typically point to the same data, so calls
55 to setData() will modify the original data, while calls to insertRows()
56 will not.
57
58 To make modifications of the model affect the original range, provide the
59 range either by reference wrapper or by pointer.
60
61 \snippet qgenericitemmodel/main.cpp pointer
62 \snippet qgenericitemmodel/main.cpp reference_wrapper
63
64 In this case, QAbstractItemModel APIs that modify the model also modify the
65 range. Methods that modify the structure of the range, such as insertRows()
66 or removeColumns(), use standard C++ container APIs \c{resize()},
67 \c{insert()}, \c{erase()}, in addition to dereferencing a mutating iterator
68 to set or clear the data.
69
70 \note Once the model has been constructed, the range the model operates on
71 must no longer be modified directly. Views on the model wouldn't be
72 informed about the changes, and structural changes are likely to corrupt
73 instances of QPersistentModelIndex that the model maintains.
74
75 The caller must make sure that the range's lifetime exceeds the lifetime of
76 the model.
77
78 Use smart pointers to make sure that the range is only deleted when all
79 clients are done with it.
80
81 \snippet qgenericitemmodel/main.cpp smart_pointer
82
83 QGenericItemModel supports both shared and unique pointers.
84
85 \section2 Read-only or mutable
86
87 For ranges that are const objects, for which access always yields constant
88 values, or where the required container APIs are not available,
89 QGenericItemModel implements write-access APIs to do nothing and return
90 \c{false}. In the example using \c{std::array}, the model cannot add or
91 remove rows, as the number of entries in a C++ array is fixed. But the
92 values can be changed using setData(), and the user can trigger editing of
93 the values in the list view. By making the array const, the values also
94 become read-only.
95
96 \snippet qgenericitemmodel/main.cpp const_array
97
98 The values are also read-only if the element type is const, like in
99
100 \snippet qgenericitemmodel/main.cpp const_values
101
102 In the above examples using \c{std::vector}, the model can add or remove
103 rows, and the data can be changed. Passing the range as a constant
104 reference will make the model read-only.
105
106 \snippet qgenericitemmodel/main.cpp const_ref
107
108 \note If the values in the range are const, then it's also not possible
109 to remove or insert columns and rows through the QAbstractItemModel API.
110 For more granular control, implement \l{the C++ tuple protocol}.
111
112 \section1 Rows and columns
113
114 The elements in the range are interpreted as rows of the model. Depending
115 on the type of these row elements, QGenericItemModel exposes the range as a
116 list, a table, or a tree.
117
118 If the row elements are simple values, then the range gets represented as a
119 list.
120
121 \snippet qgenericitemmodel/main.cpp list_of_int
122
123 If the type of the row elements is an iterable range, such as a vector,
124 list, or array, then the range gets represented as a table.
125
126 \snippet qgenericitemmodel/main.cpp grid_of_numbers
127
128 If the row type provides the standard C++ container APIs \c{resize()},
129 \c{insert()}, \c{erase()}, then columns can be added and removed via
130 insertColumns() and removeColumns(). All rows are required to have
131 the same number of columns.
132
133 \section2 Structs and gadgets as rows
134
135 If the row type implements \l{the C++ tuple protocol}, then the range gets
136 represented as a table with a fixed number of columns.
137
138 \snippet qgenericitemmodel/main.cpp pair_int_QString
139
140 An easier and more flexible alternative to implementing the tuple protocol
141 for a C++ type is to use Qt's \l{Meta-Object System}{meta-object system} to
142 declare a type with \l{Qt's Property System}{properties}. This can be a
143 value type that is declared as a \l{Q_GADGET}{gadget}, or a QObject subclass.
144
145 \snippet qgenericitemmodel/main.cpp gadget
146
147 Using QObject subclasses allows properties to be \l{Qt Bindable Properties}
148 {bindable}, or to have change notification signals. However, using QObject
149 instances for items has significant memory overhead.
150
151 Using Qt gadgets or objects is more convenient and can be more flexible
152 than implementing the tuple protocol. Those types are also directly
153 accessible from within QML. However, the access through the property system
154 comes with some runtime overhead. For performance critical models, consider
155 implementing the tuple protocol for compile-time generation of the access
156 code.
157
158 \section2 Multi-role items
159
160 The type of the items that the implementations of data(), setData(),
161 clearItemData() etc. operate on can be the same across the entire model -
162 like in the \c{gridOfNumbers} example above. But the range can also have
163 different item types for different columns, like in the \c{numberNames}
164 case.
165
166 By default, the value gets used for the Qt::DisplayRole and Qt::EditRole
167 roles. Most views expect the value to be
168 \l{QVariant::canConvert}{convertible to and from a QString} (but a custom
169 delegate might provide more flexibility).
170
171 If the item is an associative container that uses \c{int},
172 \l{Qt::ItemDataRole}, or QString as the key type, and QVariant as the
173 mapped type, then QGenericItemModel interprets that container as the storage
174 of the data for multiple roles. The data() and setData() functions return
175 and modify the mapped value in the container, and setItemData() modifies all
176 provided values, itemData() returns all stored values, and clearItemData()
177 clears the entire container.
178
179 \snippet qgenericitemmodel/main.cpp color_map
180
181 The most efficient data type to use as the key is Qt::ItemDataRole or
182 \c{int}. When using \c{int}, itemData() returns the container as is, and
183 doesn't have to create a copy of the data.
184
185 Gadgets and QObject types are also represented at multi-role items if they
186 are the item type in a table. The names of the properties have to match the
187 names of the roles.
188
189 \snippet qgenericitemmodel/main.cpp color_gadget_0
190
191 When used in a list, these types are ambiguous: they can be represented as
192 multi-column rows, with each property represented as a separate column. Or
193 they can be single items with each property being a role. To disambiguate,
194 use the QGenericItemModel::SingleColumn and QGenericItemModel::MultiColumn
195 wrappers.
196
197 \snippet qgenericitemmodel/main.cpp color_gadget_1
198
199 \section1 Trees of data
200
201 QGenericItemModel can represent a data structure as a tree model. Such a
202 tree data structure needs to be homomorphic: on all levels of the tree, the
203 list of child rows needs to use the exact same representation as the tree
204 itself. In addition, the row type needs be of a static size: either a gadget
205 or QObject type, or a type that implements the {C++ tuple protocol}.
206
207 To represent such data as a tree, the row type has to implement a traversal
208 protocol that allows QGenericItemModel to navigate up and down the tree.
209 For any given row, the model needs to be able to retrieve the parent row,
210 and the span of children for any given row.
211
212 \snippet qgenericitemmodel/main.cpp tree_protocol_0
213
214 The tree itself is a vector of \c{TreeRow} values. See \l{Rows as pointers
215 or values} for the considerations on whether to use values or pointers of
216 items for the rows.
217
218 \snippet qgenericitemmodel/main.cpp tree_protocol_1
219
220 The row class can be of any fixed-size type described above: a type that
221 implements the tuple protocol, a gadget, or a QObject. In this example, we
222 use a gadget.
223
224 Each row item needs to maintain a pointer to the parent row, as well as an
225 optional range of child rows that is identical to the structure used for the
226 tree.
227
228 Making the row type default constructible is optional, and allows the model
229 to construct new row data elements, for instance in the insertRow() or
230 moveRows() implementations.
231
232 \snippet qgenericitemmodel/main.cpp tree_protocol_2
233
234 The tree traversal protocol can then be implemented as member functions of
235 the row data type. A const \c{parentRow()} function has to return a pointer
236 to a const row item; and the \c{childRows()} function has to return a
237 reference to a const \c{std::optional} that can hold the optional child
238 range.
239
240 These two functions are sufficient for the model to navigate the tree as a
241 read-only data structure. To allow the user to edit data in a view, and the
242 model to implement mutating model APIs such as insertRows(), removeRows(),
243 and moveRows(), we have to implement additional functions for write-access:
244
245 \snippet qgenericitemmodel/main.cpp tree_protocol_3
246
247 The model calls the \c{setParentRow()} function and mutable \c{childRows()}
248 overload to move or insert rows into an existing tree branch, and to update
249 the parent pointer should the old value have become invalid. The non-const
250 overload of \c{childRows()} provides in addition write-access to the row
251 data.
252
253 \note The model performs setting the parent of a row, removing that row
254 from the old parent, and adding it to the list of the new parent's children,
255 as separate steps. This keeps the protocol interface small.
256
257 \dots
258 \snippet qgenericitemmodel/main.cpp tree_protocol_4
259
260 The rest of the class implementation is not relevant for the model, but
261 a \c{addChild()} helper provides us with a convenient way to construct the
262 initial state of the tree.
263
264 \snippet qgenericitemmodel/main.cpp tree_protocol_5
265
266 A QGenericItemModel instantiated with an instance of such a range will
267 represent the data as a tree.
268
269 \snippet qgenericitemmodel/main.cpp tree_protocol_6
270
271 \section3 Tree traversal protocol in a separate class
272
273 The tree traversal protocol can also be implemented in a separate class.
274
275 \snippet qgenericitemmodel/main.cpp explicit_tree_protocol_0
276
277 Pass an instance of this protocol implementation to the QGenericItemModel
278 constructor:
279
280 \snippet qgenericitemmodel/main.cpp explicit_tree_protocol_1
281
282 \section2 Rows as pointers or values
283
284 The row type of the data range can be either a value, or a pointer. In
285 the code above we have been using the tree rows as values in a vector,
286 which avoids that we have to deal with explicit memory management. However,
287 a vector as a contiguous block of memory invalidates all iterators and
288 references when it has to reallocate the storage, or when inserting or
289 removing elements. This impacts the pointer to the parent item, which is
290 the location of the parent row within the vector. Making sure that this
291 parent (and QPersistentModelIndex instances referring to items within it)
292 stays valid can incurr substantial performance overhead. The
293 QGenericItemModel implementation has to assume that all references into the
294 range become invalid when modifying the range.
295
296 Alternatively, we can also use a range of row pointers as the tree type:
297
298 \snippet qgenericitemmodel/main.cpp tree_of_pointers_0
299
300 In this case, we have to allocate all TreeRow instances explicitly using
301 operator \c{new}, and implement the destructor to \c{delete} all items in
302 the vector of children.
303
304 \snippet qgenericitemmodel/main.cpp tree_of_pointers_1
305 \snippet qgenericitemmodel/main.cpp tree_of_pointers_2
306
307 Before we can construct a model that represents this data as a tree, we need
308 to also implement the tree traversal protocol.
309
310 \snippet qgenericitemmodel/main.cpp tree_of_pointers_3
311
312 An explicit protocol implementation for mutable trees of pointers has to
313 provide two additional member functions, \c{newRow()} and
314 \c{deleteRow(RowType *)}.
315
316 \snippet qgenericitemmodel/main.cpp tree_of_pointers_4
317
318 The model will call those functions when creating new rows in insertRows(),
319 and when removing rows in removeRows(). In addition, if the model has
320 ownership of the data, then it will also delete all top-level rows upon
321 destruction. Note how in this example, we move the tree into the model, so
322 we must no longer perform any operations on it. QGenericItemModel, when
323 constructed by moving tree-data with row-pointers into it, will take
324 ownership of the data, and delete the row pointers in it's destructor.
325
326 \note This is not the case for tables and lists that use pointers as their
327 row type. QGenericItemModel will never allocate new rows in lists and tables
328 using operator new, and will never free any rows.
329
330 Using pointers at rows comes with some memory allocation and management
331 overhead. However, when using rows through pointers the references to the
332 row items remain stable, even when they are moved around in the range, or
333 when the range reallocates. This can significantly reduce the cost of
334 making modifications to the model's structure when using insertRows(),
335 removeRows(), or moveRows().
336
337 Each choice has different performance and memory overhead trade-offs. The
338 best option depends on the exact use case and data structure used.
339
340 \section2 The C++ tuple protocol
341
342 As seen in the \c{numberNames} example above, the row type can be a tuple,
343 and in fact any type that implements the tuple protocol. This protocol is
344 implemented by specializing \c{std::tuple_size} and \c{std::tuple_element},
345 and overloading the unqualified \c{get} function. Do so for your custom row
346 type to make existing structured data available to the model/view framework
347 in Qt.
348
349 \snippet qgenericitemmodel/main.cpp tuple_protocol
350
351 In the above implementation, the \c{title} and \c{author} values of the
352 \c{Book} type are returned as \c{const}, so the model flags items in those
353 two columns as read-only. The user won't be able to trigger editing, and
354 setData() does nothing and returns false. For \c{summary} and \c{rating}
355 the implementation returns the same value category as the book, so when
356 \c{get} is called with a mutable reference to a \c{Book}, then it will
357 return a mutable reference of the respective variable. The model makes
358 those columns editable, both for the user and for programmatic access.
359
360 \note The implementation of \c{get} above requires C++23. A C++17 compliant
361 implementation can be found in the unit test code for QGenericItemModel.
362
363 Types that have a meta objects, and implement the C++ tuple protocol, also
364 can cause compile-time ambiguity when used as the row type, as the framework
365 won't know which API to use to access the individual values. Use the
366 QGenericItemModel::SingleColumn and QGenericItemModel::MultiColumns wrapper
367 to disambiguate.
368
369 \sa {Model/View Programming}
370*/
371
372/*!
373 \typedef QGenericItemModel::SingleColumn
374
375 Use this type to disambiguate when using the type \c{T} as the row type in
376 the range. If \c{T} provides a metaobject, then the framework will by
377 default represent the type as multiple columns, resulting in a table model.
378
379 \snippet qgenericitemmodel/main.cpp color_gadget_0
380
381 When stored in a sequential range, this type will be interpreted as
382 multi-column rows with each property being one column. The range will be
383 represented as a table.
384
385 \code
386 QList<ColorEntry> colors = {
387 // ...
388 };
389 QGenericItemModel tableModel(colors); // columnCount() == 3
390 \endcode
391
392 When wrapped into QGenericItemModel::SingleColumn, the model will be a list,
393 with each instance of \c{T} represented as an item with multiple roles.
394
395 \code
396 QList<QGenericItemModel::SingleColumn<ColorEntry>> colors = {
397 // ...
398 };
399 QGenericItemModel listModel(colors); // columnCount() == 1
400 \endcode
401
402 \sa QGenericItemModel::MultiColumn
403*/
404
405/*!
406 \class QGenericItemModel::MultiColumn
407 \brief Represents the wrapped type \c{T} as multiple columns in a QGenericItemModel.
408 \inmodule QtCore
409 \ingroup model-view
410 \since 6.10
411
412 Use this type to disambiguate when the type \c{T} has both a metaobject, and
413 implements \l{the C++ tuple protocol}. The type will be represented as
414 multiple columns, and the individual values will be accessed through the
415 tuple protocol.
416
417 \snippet qgenericitemmodel/main.cpp color_gadget_0
418 \code
419 namespace std {
420 template <> struct tuple_size<ColorEntry> : integral_constant<size_t, 3> {};
421 // ...
422 }
423
424 QList<QGenericItemModel::MultiColumn<ColorEntry>> colors = {
425 // ...
426 };
427 QGenericItemModel colorList(colors);
428 \endcode
429
430 To represent the type a single column value with multiple roles, use
431 QGenericItemModel::SingleColumn instead.
432
433 \sa QGenericItemModel::SingleColumn
434*/
435
436/*!
437 \fn template <typename Range, QGenericItemModelDetails::if_is_table_range<Range>> QGenericItemModel::QGenericItemModel(Range &&range, QObject *parent)
438 \fn template <typename Range, QGenericItemModelDetails::if_is_tree_range<Range>> QGenericItemModel::QGenericItemModel(Range &&range, QObject *parent)
439 \fn template <typename Range, typename Protocol, QGenericItemModelDetails::if_is_tree_range<Range, Protocol>> QGenericItemModel::QGenericItemModel(Range &&range, Protocol &&protocol, QObject *parent)
440
441 Constructs a generic item model instance that operates on the data in \a
442 range. The \a range has to be a sequential range for which \c{std::cbegin}
443 and \c{std::cend} are available. If \a protocol is provided, then the model
444 will represent the range as a tree using the protocol implementation.
445 The model instance becomes a child of \a parent.
446
447 The \a range can be a pointer, in which case mutating model APIs will
448 modify the data in that range instance. If \a range is a value (or moved
449 into the model), then use the signals emitted by the model to respond to
450 changes to the data.
451
452 \note While the model does not take ownership of the range object, you
453 must not modify the \a range directly once the model has been
454 constructed. Such modifications will not emit signals necessary to keep
455 model users (other models or views) synchronized with the model, resulting
456 in inconsistent results, undefined behavior, and crashes.
457*/
458
459/*!
460 Destroys the generic item model.
461
462 The range that the model was constructed from is not destroyed.
463*/
464QGenericItemModel::~QGenericItemModel() = default;
465
466/*!
467 \reimp
468
469 Returns the index of the model item at \a row and \a column in \a parent.
470
471 Passing a valid parent produces an invalid index for models that operate on
472 list and table ranges.
473
474 \sa parent()
475*/
476QModelIndex QGenericItemModel::index(int row, int column, const QModelIndex &parent) const
477{
478 return impl->callConst<QModelIndex>(QGenericItemModelImplBase::Index, row, column, parent);
479}
480
481/*!
482 \reimp
483
484 Returns the parent of the item at the \a child index.
485
486 This function always produces an invalid index for models that operate on
487 list and table ranges. For models operation on a tree, this function
488 returns the index for the row item returned by the parent() implementation
489 of the tree traversal protocol.
490
491 \sa index(), hasChildren()
492*/
493QModelIndex QGenericItemModel::parent(const QModelIndex &child) const
494{
495 return impl->callConst<QModelIndex>(QGenericItemModelImplBase::Parent, child);
496}
497
498/*!
499 \reimp
500
501 Returns the sibling at \a row and \a column for the item at \a index, or an
502 invalid QModelIndex if there is no sibling at that location.
503
504 This implementation is significantly faster than going through the parent()
505 of the \a index.
506
507 \sa index(), QModelIndex::row(), QModelIndex::column()
508*/
509QModelIndex QGenericItemModel::sibling(int row, int column, const QModelIndex &index) const
510{
511 return impl->callConst<QModelIndex>(QGenericItemModelImplBase::Sibling, row, column, index);
512}
513
514/*!
515 \reimp
516
517 Returns the number of rows under the given \a parent. This is the number of
518 items in the root range for an invalid \a parent index.
519
520 If the \a parent index is valid, then this function always returns 0 for
521 models that operate on list and table ranges. For trees, this returns the
522 size of the range returned by the childRows() implementation of the tree
523 traversal protocol.
524
525 \sa columnCount(), insertRows(), hasChildren()
526*/
527int QGenericItemModel::rowCount(const QModelIndex &parent) const
528{
529 return impl->callConst<int>(QGenericItemModelImplBase::RowCount, parent);
530}
531
532/*!
533 \reimp
534
535 Returns the number of columns of the model. This function returns the same
536 value for all \a parent indexes.
537
538 For models operating on a statically sized row type, this returned value is
539 always the same throughout the lifetime of the model. For models operating
540 on dynamically sized row type, the model returns the number of items in the
541 first row, or 0 if the model has no rows.
542
543 \sa rowCount, insertColumns()
544*/
545int QGenericItemModel::columnCount(const QModelIndex &parent) const
546{
547 return impl->callConst<int>(QGenericItemModelImplBase::ColumnCount, parent);
548}
549
550/*!
551 \reimp
552
553 Returns the item flags for the given \a index.
554
555 The implementation returns a combination of flags that enables the item
556 (\c ItemIsEnabled) and allows it to be selected (\c ItemIsSelectable). For
557 models operating on a range with mutable data, it also sets the flag
558 that allows the item to be editable (\c ItemIsEditable).
559
560 \sa Qt::ItemFlags
561*/
562Qt::ItemFlags QGenericItemModel::flags(const QModelIndex &index) const
563{
564 return impl->callConst<Qt::ItemFlags>(QGenericItemModelImplBase::Flags, index);
565}
566
567/*!
568 \reimp
569
570 Returns the data for the given \a role and \a section in the header with
571 the specified \a orientation.
572
573 For horizontal headers, the section number corresponds to the column
574 number. Similarly, for vertical headers, the section number corresponds to
575 the row number.
576
577 \sa Qt::ItemDataRole, setHeaderData(), QHeaderView
578*/
579QVariant QGenericItemModel::headerData(int section, Qt::Orientation orientation, int role) const
580{
581 return impl->callConst<QVariant>(QGenericItemModelImplBase::HeaderData,
582 section, orientation, role);
583}
584
585/*!
586 \reimp
587
588 Returns the data stored under the given \a role for the value in the
589 range referred to by the \a index.
590
591 If the item type for that index is an associative container that maps from
592 either \c{int}, Qt::ItemDataRole, or QString to a QVariant, then the role
593 data is looked up in that container and returned.
594
595 Otherwise, the implementation returns a QVariant constructed from the item
596 via \c{QVariant::fromValue()} for \c{Qt::DisplayRole} or \c{Qt::EditRole}.
597 For other roles, the implementation returns an \b invalid
598 (default-constructed) QVariant.
599
600 \sa Qt::ItemDataRole, setData(), headerData()
601*/
602QVariant QGenericItemModel::data(const QModelIndex &index, int role) const
603{
604 return impl->callConst<QVariant>(QGenericItemModelImplBase::Data, index, role);
605}
606
607/*!
608 \reimp
609
610 Sets the \a role data for the item at \a index to \a data.
611
612 If the item type for that \a index is an associative container that maps
613 from either \c{int}, Qt::ItemDataRole, or QString to a QVariant, then
614 \a data is stored in that container for the key specified by \a role.
615
616 Otherwise, this implementation assigns the value in \a data to the item at
617 the \a index in the range for \c{Qt::DisplayRole} and \c{Qt::EditRole},
618 and returns \c{true}. For other roles, the implementation returns
619 \c{false}.
620
621//! [read-only-setData]
622 For models operating on a read-only range, or on a read-only column in
623 a row type that implements \l{the C++ tuple protocol}, this implementation
624 returns \c{false} immediately.
625//! [read-only-setData]
626*/
627bool QGenericItemModel::setData(const QModelIndex &index, const QVariant &data, int role)
628{
629 return impl->call<bool>(QGenericItemModelImplBase::SetData, index, data, role);
630}
631
632/*!
633 \reimp
634
635 Returns a map with values for all predefined roles in the model for the
636 item at the given \a index.
637
638 If the item type for that \a index is an associative container that maps
639 from either \c{int}, Qt::ItemDataRole, or QString to a QVariant, then the
640 data from that container is returned.
641
642 If the item type is a gadget or QObject subclass, then the values of those
643 properties that match a \l{roleNames()}{role name} are returned.
644
645 If the item is not an associative container, gadget, or QObject subclass,
646 then this calls the base class implementation.
647
648 \sa setItemData(), Qt::ItemDataRole, data()
649*/
650QMap<int, QVariant> QGenericItemModel::itemData(const QModelIndex &index) const
651{
652 return impl->callConst<QMap<int, QVariant>>(QGenericItemModelImplBase::ItemData, index);
653}
654
655/*!
656 \reimp
657
658 If the item type for that \a index is an associative container that maps
659 from either \c{int} or Qt::ItemDataRole to a QVariant, then the entries in
660 \a data are stored in that container. If the associative container maps from
661 QString to QVariant, then only those values in \a data are stored for which
662 there is a mapping in the \l{roleNames()}{role names} table.
663
664 If the item type is a gadget or QObject subclass, then those properties that
665 match a \l{roleNames()}{role name} are set to the corresponding value in
666 \a data.
667
668 Roles for which there is no entry in \a data are not modified.
669
670 For item types that can be copied, this implementation is transactional,
671 and returns true if all the entries from \a data could be stored. If any
672 entry could not be updated, then the original container is not modified at
673 all, and the function returns false.
674
675 If the item is not an associative container, gadget, or QObject subclass,
676 then this calls the base class implementation, which calls setData() for
677 each entry in \a data.
678
679 \sa itemData(), setData(), Qt::ItemDataRole
680*/
681bool QGenericItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &data)
682{
683 return impl->call<bool>(QGenericItemModelImplBase::SetItemData, index, data);
684}
685
686/*!
687 \reimp
688
689 Replaces the value stored in the range at \a index with a default-
690 constructed value.
691
692 \include qgenericitemmodel.cpp read-only-setData
693*/
694bool QGenericItemModel::clearItemData(const QModelIndex &index)
695{
696 return impl->call<bool>(QGenericItemModelImplBase::ClearItemData, index);
697}
698
699/*
700//! [column-change-requirement]
701 \note A dynamically sized row type needs to provide a \c{\1} member function.
702
703 For models operating on a read-only range, or on a range with a
704 statically sized row type (such as a tuple, array, or struct), this
705 implementation does nothing and returns \c{false} immediately. This is
706 always the case for tree models.
707//! [column-change-requirement]
708*/
709
710/*!
711 \reimp
712
713 Inserts \a count empty columns before the item at \a column in all rows
714 of the range at \a parent. Returns \c{true} if successful; otherwise
715 returns \c{false}.
716
717 \include qgenericitemmodel.cpp {column-change-requirement} {insert(const_iterator, size_t, value_type)}
718*/
719bool QGenericItemModel::insertColumns(int column, int count, const QModelIndex &parent)
720{
721 return impl->call<bool>(QGenericItemModelImplBase::InsertColumns, column, count, parent);
722}
723
724/*!
725 \reimp
726
727 Removes \a count columns from the item at \a column on in all rows of the
728 range at \a parent. Returns \c{true} if successful, otherwise returns
729 \c{false}.
730
731 \include qgenericitemmodel.cpp {column-change-requirement} {erase(const_iterator, size_t)}
732*/
733bool QGenericItemModel::removeColumns(int column, int count, const QModelIndex &parent)
734{
735 return impl->call<bool>(QGenericItemModelImplBase::RemoveColumns, column, count, parent);
736}
737
738/*!
739 \reimp
740
741 Moves \a count columns starting with the given \a sourceColumn under parent
742 \a sourceParent to column \a destinationColumn under parent \a destinationParent.
743
744 Returns \c{true} if the columns were successfully moved; otherwise returns
745 \c{false}.
746*/
747bool QGenericItemModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
748 const QModelIndex &destinationParent, int destinationColumn)
749{
750 return impl->call<bool>(QGenericItemModelImplBase::MoveColumns,
751 sourceParent, sourceColumn, count,
752 destinationParent, destinationColumn);
753}
754
755/*
756//! [row-change-requirement]
757 \note The range needs to be dynamically sized and provide a \c{\1}
758 member function.
759
760 For models operating on a read-only or statically-sized range (such as
761 an array), this implementation does nothing and returns \c{false}
762 immediately.
763//! [row-change-requirement]
764*/
765
766/*!
767 \reimp
768
769 Inserts \a count empty rows before the given \a row into the range at
770 \a parent. Returns \c{true} if successful; otherwise returns \c{false}.
771
772 \include qgenericitemmodel.cpp {row-change-requirement} {insert(const_iterator, size_t, value_type)}
773
774 \note For ranges with a dynamically sized column type, the column needs
775 to provide a \c{resize(size_t)} member function.
776*/
777bool QGenericItemModel::insertRows(int row, int count, const QModelIndex &parent)
778{
779 return impl->call<bool>(QGenericItemModelImplBase::InsertRows, row, count, parent);
780}
781
782/*!
783 \reimp
784
785 Removes \a count rows from the range at \a parent, starting with the
786 given \a row. Returns \c{true} if successful, otherwise returns \c{false}.
787
788 \include qgenericitemmodel.cpp {row-change-requirement} {erase(const_iterator, size_t)}
789*/
790bool QGenericItemModel::removeRows(int row, int count, const QModelIndex &parent)
791{
792 return impl->call<bool>(QGenericItemModelImplBase::RemoveRows, row, count, parent);
793}
794
795/*!
796 \reimp
797
798 Moves \a count rows starting with the given \a sourceRow under parent
799 \a sourceParent to row \a destinationRow under parent \a destinationParent.
800
801 Returns \c{true} if the rows were successfully moved; otherwise returns
802 \c{false}.
803*/
804bool QGenericItemModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
805 const QModelIndex &destinationParent, int destinationRow)
806{
807 return impl->call<bool>(QGenericItemModelImplBase::MoveRows,
808 sourceParent, sourceRow, count,
809 destinationParent, destinationRow);
810}
811
812/*!
813 \reimp
814*/
815bool QGenericItemModel::canFetchMore(const QModelIndex &parent) const
816{
817 return QAbstractItemModel::canFetchMore(parent);
818}
819
820/*!
821 \reimp
822*/
823void QGenericItemModel::fetchMore(const QModelIndex &parent)
824{
825 QAbstractItemModel::fetchMore(parent);
826}
827
828/*!
829 \reimp
830*/
831bool QGenericItemModel::hasChildren(const QModelIndex &parent) const
832{
833 return QAbstractItemModel::hasChildren(parent);
834}
835
836/*!
837 \reimp
838*/
839QModelIndex QGenericItemModel::buddy(const QModelIndex &index) const
840{
841 return QAbstractItemModel::buddy(index);
842}
843
844/*!
845 \reimp
846*/
847bool QGenericItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
848 int row, int column, const QModelIndex &parent) const
849{
850 return QAbstractItemModel::canDropMimeData(data, action, row, column, parent);
851}
852
853/*!
854 \reimp
855*/
856bool QGenericItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
857 int row, int column, const QModelIndex &parent)
858{
859 return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
860}
861
862/*!
863 \reimp
864*/
865QMimeData *QGenericItemModel::mimeData(const QModelIndexList &indexes) const
866{
867 return QAbstractItemModel::mimeData(indexes);
868}
869
870/*!
871 \reimp
872*/
873QStringList QGenericItemModel::mimeTypes() const
874{
875 return QAbstractItemModel::mimeTypes();
876}
877
878/*!
879 \reimp
880*/
881QModelIndexList QGenericItemModel::match(const QModelIndex &start, int role, const QVariant &value,
882 int hits, Qt::MatchFlags flags) const
883{
884 return QAbstractItemModel::match(start, role, value, hits, flags);
885}
886
887/*!
888 \reimp
889*/
890void QGenericItemModel::multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
891{
892 QAbstractItemModel::multiData(index, roleDataSpan);
893}
894
895/*!
896 \reimp
897*/
898QHash<int, QByteArray> QGenericItemModel::roleNames() const
899{
900 return QAbstractItemModel::roleNames();
901}
902
903/*!
904 \reimp
905*/
906void QGenericItemModel::sort(int column, Qt::SortOrder order)
907{
908 return QAbstractItemModel::sort(column, order);
909}
910
911/*!
912 \reimp
913*/
914QSize QGenericItemModel::span(const QModelIndex &index) const
915{
916 return QAbstractItemModel::span(index);
917}
918
919/*!
920 \reimp
921*/
922Qt::DropActions QGenericItemModel::supportedDragActions() const
923{
924 return QAbstractItemModel::supportedDragActions();
925}
926
927/*!
928 \reimp
929*/
930Qt::DropActions QGenericItemModel::supportedDropActions() const
931{
932 return QAbstractItemModel::supportedDropActions();
933}
934
935/*!
936 \reimp
937*/
938void QGenericItemModel::resetInternalData()
939{
940 QAbstractItemModel::resetInternalData();
941}
942
943/*!
944 \reimp
945*/
946bool QGenericItemModel::event(QEvent *event)
947{
948 return QAbstractItemModel::event(event);
949}
950
951/*!
952 \reimp
953*/
954bool QGenericItemModel::eventFilter(QObject *object, QEvent *event)
955{
956 return QAbstractItemModel::eventFilter(object, event);
957}
958
959QT_END_NAMESPACE
960
961#include "moc_qgenericitemmodel.cpp"