summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/src_corelib_kernel_qabstractitemmodel.cpp
blob: 3f7dc07d9fbb98b6eb221757c07d3b524b9aef80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

//! [0]
beginInsertRows(parent, 2, 4);
//! [0]


//! [1]
beginInsertRows(parent, 4, 5);
//! [1]


//! [2]
beginRemoveRows(parent, 2, 3);
//! [2]


//! [3]
beginInsertColumns(parent, 4, 6);
//! [3]


//! [4]
beginInsertColumns(parent, 6, 8);
//! [4]


//! [5]
beginRemoveColumns(parent, 4, 6);
//! [5]


//! [6]
beginMoveRows(sourceParent, 2, 4, destinationParent, 2);
//! [6]


//! [7]
beginMoveRows(sourceParent, 2, 4, destinationParent, 6);
//! [7]


//! [8]
beginMoveRows(parent, 2, 2, parent, 0);
//! [8]


//! [9]
beginMoveRows(parent, 2, 2, parent, 4);
//! [9]

//! [10]
myData.clear();
reset();
//! [10]

//! [11]
beginResetModel();
myData.clear();
endResetModel();
//! [11]

//! [12]
class CustomDataProxy : public QSortFilterProxyModel
{
    Q_OBJECT
public:
    CustomDataProxy(QObject *parent)
      : QSortFilterProxyModel(parent)
    {
    }

    ...

    QVariant data(const QModelIndex &index, int role) override
    {
        if (role != Qt::BackgroundRole)
            return QSortFilterProxyModel::data(index, role);

        if (m_customData.contains(index.row()))
            return m_customData.value(index.row());
        return QSortFilterProxyModel::data(index, role);
    }

private slots:
    void resetInternalData()
    {
        m_customData.clear();
    }

private:
  QHash<int, QVariant> m_customData;
};
//! [12]

//! [13]
QVariant text = model->data(index, Qt::DisplayRole);
QVariant decoration = model->data(index, Qt::DecorationRole);
QVariant checkState = model->data(index, Qt::CheckStateRole);
// etc.
//! [13]

//! [14]
std::array<QModelRoleData, 3> roleData = { {
    QModelRoleData(Qt::DisplayRole),
    QModelRoleData(Qt::DecorationRole),
    QModelRoleData(Qt::CheckStateRole)
} };

// Usually, this is not necessary: A QModelRoleDataSpan
// will be built automatically for you when passing an array-like
// container to multiData().
QModelRoleDataSpan span(roleData);

model->multiData(index, span);

// Use roleData[0].data(), roleData[1].data(), etc.
//! [14]

//! [15]
void MyModel::multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
{
    for (QModelRoleData &roleData : roleDataSpan) {
        int role = roleData.role();

        // ... obtain the data for index and role ...

        roleData.setData(result);
    }
}
//! [15]

//! [16]
QVariant MyModel::data(const QModelIndex &index, int role) const
{
    QModelRoleData roleData(role);
    multiData(index, roleData);
    return roleData.data();
}
//! [16]