1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 ** the names of its contributors may be used to endorse or promote
22 ** products derived from this software without specific prior written
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
44 Provides a table model for use in various examples.
52 Constructs a table model with at least one row and one column.
55 TableModel::TableModel(int rows, int columns, QObject *parent)
56 : QAbstractTableModel(parent)
60 for (int column = 0; column < qMax(1, columns); ++column) {
64 for (int row = 0; row < qMax(1, rows); ++row) {
65 rowList.append(newList);
71 Returns the number of items in the row list as the number of rows
75 int TableModel::rowCount(const QModelIndex &/*parent*/) const
77 return rowList.size();
81 Returns the number of items in the first list item as the number of
82 columns in the model. All rows should have the same number of columns.
85 int TableModel::columnCount(const QModelIndex &/*parent*/) const
87 return rowList[0].size();
91 Returns an appropriate value for the requested data.
92 If the view requests an invalid index, an invalid variant is returned.
93 Any valid index that corresponds to a string in the list causes that
94 string to be returned for the display role; otherwise an invalid variant
98 QVariant TableModel::data(const QModelIndex &index, int role) const
100 if (!index.isValid())
103 if (role == Qt::DisplayRole)
104 return rowList[index.row()][index.column()];
110 Returns the appropriate header string depending on the orientation of
111 the header and the section. If anything other than the display role is
112 requested, we return an invalid variant.
115 QVariant TableModel::headerData(int section, Qt::Orientation orientation,
118 if (role != Qt::DisplayRole)
121 if (orientation == Qt::Horizontal)
122 return QString("Column %1").arg(section);
124 return QString("Row %1").arg(section);
128 Returns an appropriate value for the item's flags. Valid items are
129 enabled, selectable, and editable.
132 Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
134 if (!index.isValid())
135 return Qt::ItemIsEnabled;
137 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
141 Changes an item in the model, but only if the following conditions
144 * The index supplied is valid.
145 * The role associated with editing text is specified.
147 The dataChanged() signal is emitted if the item is changed.
150 bool TableModel::setData(const QModelIndex &index,
151 const QVariant &value, int role)
153 if (!index.isValid() || role != Qt::EditRole)
156 rowList[index.row()][index.column()] = value.toString();
157 emit dataChanged(index, index);
162 Inserts a number of rows into the model at the specified position.
165 bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
167 int columns = columnCount();
168 beginInsertRows(parent, position, position + rows - 1);
170 for (int row = 0; row < rows; ++row) {
172 for (int column = 0; column < columns; ++column)
174 rowList.insert(position, items);
182 Inserts a number of columns into the model at the specified position.
183 Each entry in the list is extended in turn with the required number of
187 bool TableModel::insertColumns(int position, int columns, const QModelIndex &parent)
189 int rows = rowCount();
190 beginInsertColumns(parent, position, position + columns - 1);
192 for (int row = 0; row < rows; ++row) {
193 for (int column = position; column < columns; ++column) {
194 rowList[row].insert(position, "");
203 Removes a number of rows from the model at the specified position.
206 bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
208 beginRemoveRows(parent, position, position + rows - 1);
210 for (int row = 0; row < rows; ++row) {
211 rowList.removeAt(position);
219 Removes a number of columns from the model at the specified position.
220 Each row is shortened by the number of columns specified.
223 bool TableModel::removeColumns(int position, int columns, const QModelIndex &parent)
225 int rows = rowCount();
226 beginRemoveColumns(parent, position, position + columns - 1);
228 for (int row = 0; row < rows; ++row) {
229 for (int column = 0; column < columns; ++column) {
230 rowList[row].removeAt(position);