source: trunk/doc/src/snippets/stringlistmodel/model.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43 model.cpp
44
45 A simple model that uses a QStringList as its data source.
46*/
47
48#include "model.h"
49
50/*!
51 Returns the number of items in the string list as the number of rows
52 in the model.
53*/
54
55//! [0]
56int StringListModel::rowCount(const QModelIndex &parent) const
57{
58 return stringList.count();
59}
60//! [0]
61
62
63#ifdef 0
64// This represents a read-only version of data(), an early stage in the
65// development of the example leading to an editable StringListModel.
66
67/*!
68 Returns an appropriate value for the requested data.
69 If the view requests an invalid index, an invalid variant is returned.
70 Any valid index that corresponds to a string in the list causes that
71 string to be returned.
72*/
73
74//! [1-data-read-only]
75QVariant StringListModel::data(const QModelIndex &index, int role) const
76{
77 if (!index.isValid())
78 return QVariant();
79
80 if (index.row() >= stringList.size())
81 return QVariant();
82
83 if (role == Qt::DisplayRole)
84 return stringList.at(index.row());
85 else
86 return QVariant();
87}
88//! [1-data-read-only]
89#endif
90
91
92/*!
93 Returns an appropriate value for the requested data.
94 If the view requests an invalid index, an invalid variant is returned.
95 Any valid index that corresponds to a string in the list causes that
96 string to be returned.
97*/
98
99//! [1]
100QVariant StringListModel::data(const QModelIndex &index, int role) const
101{
102 if (!index.isValid())
103 return QVariant();
104
105 if (index.row() >= stringList.size())
106 return QVariant();
107
108 if (role == Qt::DisplayRole || role == Qt::EditRole)
109 return stringList.at(index.row());
110 else
111 return QVariant();
112}
113//! [1]
114
115/*!
116 Returns the appropriate header string depending on the orientation of
117 the header and the section. If anything other than the display role is
118 requested, we return an invalid variant.
119*/
120
121//! [2]
122QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
123 int role) const
124{
125 if (role != Qt::DisplayRole)
126 return QVariant();
127
128 if (orientation == Qt::Horizontal)
129 return QString("Column %1").arg(section);
130 else
131 return QString("Row %1").arg(section);
132}
133//! [2]
134
135/*!
136 Returns an appropriate value for the item's flags. Valid items are
137 enabled, selectable, and editable.
138*/
139
140//! [3]
141Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
142{
143 if (!index.isValid())
144 return Qt::ItemIsEnabled;
145
146 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
147}
148//! [3]
149
150/*!
151 Changes an item in the string list, but only if the following conditions
152 are met:
153
154 * The index supplied is valid.
155 * The index corresponds to an item to be shown in a view.
156 * The role associated with editing text is specified.
157
158 The dataChanged() signal is emitted if the item is changed.
159*/
160
161//! [4]
162bool StringListModel::setData(const QModelIndex &index,
163 const QVariant &value, int role)
164{
165 if (index.isValid() && role == Qt::EditRole) {
166
167 stringList.replace(index.row(), value.toString());
168 emit dataChanged(index, index);
169 return true;
170 }
171//! [4] //! [5]
172 return false;
173}
174//! [5]
175
176/*!
177 Inserts a number of rows into the model at the specified position.
178*/
179
180//! [6]
181bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
182{
183 beginInsertRows(QModelIndex(), position, position+rows-1);
184
185 for (int row = 0; row < rows; ++row) {
186 stringList.insert(position, "");
187 }
188
189 endInsertRows();
190 return true;
191//! [6] //! [7]
192}
193//! [7]
194
195/*!
196 Removes a number of rows from the model at the specified position.
197*/
198
199//! [8]
200bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
201{
202 beginRemoveRows(QModelIndex(), position, position+rows-1);
203
204 for (int row = 0; row < rows; ++row) {
205 stringList.removeAt(position);
206 }
207
208 endRemoveRows();
209 return true;
210//! [8] //! [9]
211}
212//! [9]
Note: See TracBrowser for help on using the repository browser.