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
src_corelib_kernel_qabstractitemmodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QSortFilterProxyModel>
5#include <QVariant>
6#include <QModelIndex>
7#include <QModelRoleDataSpan>
8#include <QAbstractItemModel>
9
11{
12public:
13 void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override;
14
15 QVariant data(const QModelIndex &index, int role) const override;
16
17 void methods_calls(QModelIndex &parent,
18 QModelIndex &sourceParent,
19 QModelIndex &destinationParent)
20 {
21 //! [0]
22 beginInsertRows(parent, 2, 4);
23 //! [0]
24
25
26 //! [1]
27 beginInsertRows(parent, 4, 5);
28 //! [1]
29
30
31 //! [2]
32 beginRemoveRows(parent, 2, 3);
33 //! [2]
34
35
36 //! [3]
37 beginInsertColumns(parent, 4, 6);
38 //! [3]
39
40
41 //! [4]
42 beginInsertColumns(parent, 6, 8);
43 //! [4]
44
45
46 //! [5]
47 beginRemoveColumns(parent, 4, 6);
48 //! [5]
49
50
51 //! [6]
52 beginMoveRows(sourceParent, 2, 4, destinationParent, 2);
53 //! [6]
54
55
56 //! [7]
57 beginMoveRows(sourceParent, 2, 4, destinationParent, 6);
58 //! [7]
59
60
61 //! [8]
62 beginMoveRows(parent, 2, 2, parent, 0);
63 //! [8]
64
65
66 //! [9]
67 beginMoveRows(parent, 2, 2, parent, 4);
68 //! [9]
69 }
70};
71
72//! [12]
74{
76public:
81
82 //...
83
85 {
86 if (role != Qt::BackgroundRole)
88
90 return m_customData.value(index.row());
92 }
93
94private slots:
96 {
98 }
99
100private:
102};
103//! [12]
104
105void example(CustomDataProxy *model, const QModelIndex &index)
106{
107 //! [13]
108 QVariant text = model->data(index, Qt::DisplayRole);
109 QVariant decoration = model->data(index, Qt::DecorationRole);
110 QVariant checkState = model->data(index, Qt::CheckStateRole);
111 // etc.
112 //! [13]
113
114 //! [14]
115 std::array<QModelRoleData, 3> roleData = { {
116 QModelRoleData(Qt::DisplayRole),
117 QModelRoleData(Qt::DecorationRole),
118 QModelRoleData(Qt::CheckStateRole)
119 } };
120
121 // Usually, this is not necessary: A QModelRoleDataSpan
122 // will be built automatically for you when passing an array-like
123 // container to multiData().
124 QModelRoleDataSpan span(roleData);
125
126 model->multiData(index, span);
127
128 // Use roleData[0].data(), roleData[1].data(), etc.
129 //! [14]
130}
131
132char result = 0;
133
134//! [15]
135void MyModel::multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
136{
137 for (QModelRoleData &roleData : roleDataSpan) {
138 int role = roleData.role();
139
140 // ... obtain the data for index and role ...
141
142 roleData.setData(result);
143 }
144}
145//! [15]
146
147//! [16]
148QVariant MyModel::data(const QModelIndex &index, int role) const
149{
150 QModelRoleData roleData(role);
151 multiData(index, roleData);
152 return roleData.data();
153}
154//! [16]
void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override
[15]
void methods_calls(QModelIndex &parent, QModelIndex &sourceParent, QModelIndex &destinationParent)
QVariant data(const QModelIndex &index, int role) const override
[15]
void example(CustomDataProxy *model, const QModelIndex &index)
[12]