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
model.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/*
5 model.cpp
6
7 A simple model that uses a QStringList as its data source.
8*/
9
10#include "model.h"
11
12/*!
13 Returns the number of items in the string list as the number of rows
14 in the model.
15*/
16
17//! [0]
18int StringListModel::rowCount(const QModelIndex &parent) const
19{
20 return stringList.count();
21}
22//! [0]
23
24
25#if 0
26// This represents a read-only version of data(), an early stage in the
27// development of the example leading to an editable StringListModel.
28
29/*!
30 Returns an appropriate value for the requested data.
31 If the view requests an invalid index, an invalid variant is returned.
32 Any valid index that corresponds to a string in the list causes that
33 string to be returned.
34*/
35
36//! [1-data-read-only]
37QVariant StringListModel::data(const QModelIndex &index, int role) const
38{
39 if (!index.isValid())
40 return QVariant();
41
42 if (index.row() >= stringList.size())
43 return QVariant();
44
45 if (role == Qt::DisplayRole)
46 return stringList.at(index.row());
47 else
48 return QVariant();
49}
50//! [1-data-read-only]
51#endif
52
53
54/*!
55 Returns an appropriate value for the requested data.
56 If the view requests an invalid index, an invalid variant is returned.
57 Any valid index that corresponds to a string in the list causes that
58 string to be returned.
59*/
60
61//! [1]
62QVariant StringListModel::data(const QModelIndex &index, int role) const
63{
64 if (!index.isValid())
65 return QVariant();
66
67 if (index.row() >= stringList.size())
68 return QVariant();
69
70 if (role == Qt::DisplayRole || role == Qt::EditRole)
71 return stringList.at(index.row());
72 else
73 return QVariant();
74}
75//! [1]
76
77/*!
78 Returns the appropriate header string depending on the orientation of
79 the header and the section. If anything other than the display role is
80 requested, we return an invalid variant.
81*/
82
83//! [2]
84QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
85 int role) const
86{
87 if (role != Qt::DisplayRole)
88 return QVariant();
89
90 if (orientation == Qt::Horizontal)
91 return QStringLiteral("Column %1").arg(section);
92 else
93 return QStringLiteral("Row %1").arg(section);
94}
95//! [2]
96
97/*!
98 Returns an appropriate value for the item's flags. Valid items are
99 enabled, selectable, and editable.
100*/
101
102//! [3]
103Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
104{
105 if (!index.isValid())
106 return Qt::ItemIsEnabled;
107
108 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
109}
110//! [3]
111
112/*!
113 Changes an item in the string list, but only if the following conditions
114 are met:
115
116 * The index supplied is valid.
117 * The index corresponds to an item to be shown in a view.
118 * The role associated with editing text is specified.
119
120 The dataChanged() signal is emitted if the item is changed.
121*/
122
123//! [4]
124bool StringListModel::setData(const QModelIndex &index,
125 const QVariant &value, int role)
126{
127 if (index.isValid() && role == Qt::EditRole) {
128
129 stringList.replace(index.row(), value.toString());
130 emit dataChanged(index, index, {role});
131 return true;
132 }
133//! [4] //! [5]
134 return false;
135}
136//! [5]
137
138/*!
139 Inserts a number of rows into the model at the specified position.
140*/
141
142//! [6]
143bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
144{
145 beginInsertRows(QModelIndex(), position, position+rows-1);
146
147 for (int row = 0; row < rows; ++row) {
148 stringList.insert(position, "");
149 }
150
151 endInsertRows();
152 return true;
153//! [6] //! [7]
154}
155//! [7]
156
157/*!
158 Removes a number of rows from the model at the specified position.
159*/
160
161//! [8]
162bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
163{
164 beginRemoveRows(QModelIndex(), position, position+rows-1);
165
166 for (int row = 0; row < rows; ++row) {
167 stringList.removeAt(position);
168 }
169
170 endRemoveRows();
171 return true;
172//! [8] //! [9]
173}
174//! [9]
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
[3]
Definition model.cpp:143
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
[3]
Definition model.cpp:124
QVariant data(const QModelIndex &index, int role) const override
[0]
Definition model.cpp:62
Qt::ItemFlags flags(const QModelIndex &index) const override
[1]
Definition model.cpp:103
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
[1]
Definition model.cpp:84
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
[7]
Definition model.cpp:162