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 Provides a table model for use in various examples.
8*/
9
10#include <QtGui>
11
12#include "model.h"
13
14/*!
15 Constructs a table model with at least one row and one column.
16*/
17
18TableModel::TableModel(int rows, int columns, QObject *parent)
19 : QAbstractTableModel(parent)
20{
21 QStringList newList;
22
23 for (int column = 0; column < qMax(1, columns); ++column) {
24 newList.append(QString());
25 }
26
27 for (int row = 0; row < qMax(1, rows); ++row) {
28 rowList.append(newList);
29 }
30}
31
32
33/*!
34 Returns the number of items in the row list as the number of rows
35 in the model.
36*/
37
38int TableModel::rowCount(const QModelIndex &/*parent*/) const
39{
40 return rowList.size();
41}
42
43/*!
44 Returns the number of items in the first list item as the number of
45 columns in the model. All rows should have the same number of columns.
46*/
47
48int TableModel::columnCount(const QModelIndex &/*parent*/) const
49{
50 return rowList[0].size();
51}
52
53/*!
54 Returns an appropriate value for the requested data.
55 If the view requests an invalid index, an invalid variant is returned.
56 Any valid index that corresponds to a string in the list causes that
57 string to be returned for the display role; otherwise an invalid variant
58 is returned.
59*/
60
61QVariant TableModel::data(const QModelIndex &index, int role) const
62{
63 if (!index.isValid())
64 return QVariant();
65
66 if (role == Qt::DisplayRole)
67 return rowList[index.row()][index.column()];
68 else
69 return QVariant();
70}
71
72/*!
73 Returns the appropriate header string depending on the orientation of
74 the header and the section. If anything other than the display role is
75 requested, we return an invalid variant.
76*/
77
78QVariant TableModel::headerData(int section, Qt::Orientation orientation,
79 int role) const
80{
81 if (role != Qt::DisplayRole)
82 return QVariant();
83
84 if (orientation == Qt::Horizontal)
85 return QStringLiteral("Column %1").arg(section);
86 else
87 return QStringLiteral("Row %1").arg(section);
88}
89
90/*!
91 Returns an appropriate value for the item's flags. Valid items are
92 enabled, selectable, and editable.
93*/
94
95Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
96{
97 if (!index.isValid())
98 return Qt::ItemIsEnabled;
99
100 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
101}
102
103/*!
104 Changes an item in the model, but only if the following conditions
105 are met:
106
107 * The index supplied is valid.
108 * The role associated with editing text is specified.
109
110 The dataChanged() signal is emitted if the item is changed.
111*/
112
113bool TableModel::setData(const QModelIndex &index,
114 const QVariant &value, int role)
115{
116 if (!index.isValid() || role != Qt::EditRole)
117 return false;
118
119 rowList[index.row()][index.column()] = value.toString();
120 emit dataChanged(index, index, {role});
121 return true;
122}
123
124/*!
125 Inserts a number of rows into the model at the specified position.
126*/
127
128bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
129{
130 int columns = columnCount();
131 beginInsertRows(parent, position, position + rows - 1);
132
133 for (int row = 0; row < rows; ++row) {
134 QStringList items;
135 for (int column = 0; column < columns; ++column)
136 items.append(QString());
137 rowList.insert(position, items);
138 }
139
140 endInsertRows();
141 return true;
142}
143
144/*!
145 Inserts a number of columns into the model at the specified position.
146 Each entry in the list is extended in turn with the required number of
147 empty strings.
148*/
149
150bool TableModel::insertColumns(int position, int columns,
151 const QModelIndex &parent)
152{
153 int rows = rowCount();
154 beginInsertColumns(parent, position, position + columns - 1);
155
156 for (int row = 0; row < rows; ++row) {
157 for (int column = position; column < columns; ++column) {
158 rowList[row].insert(position, QString());
159 }
160 }
161
162 endInsertColumns();
163 return true;
164}
165
166/*!
167 Removes a number of rows from the model at the specified position.
168*/
169
170bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
171{
172 beginRemoveRows(parent, position, position + rows - 1);
173
174 for (int row = 0; row < rows; ++row) {
175 rowList.removeAt(position);
176 }
177
178 endRemoveRows();
179 return true;
180}
181
182/*!
183 Removes a number of columns from the model at the specified position.
184 Each row is shortened by the number of columns specified.
185*/
186
187bool TableModel::removeColumns(int position, int columns,
188 const QModelIndex &parent)
189{
190 int rows = rowCount();
191 beginRemoveColumns(parent, position, position + columns - 1);
192
193 for (int row = 0; row < rows; ++row) {
194 for (int column = 0; column < columns; ++column) {
195 rowList[row].removeAt(position);
196 }
197 }
198
199 endRemoveColumns();
200 return true;
201}
[0]
Definition model.h:12
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of items in the row list as the number of rows in the model.
Definition model.cpp:38
bool removeColumns(int position, int columns, const QModelIndex &parent=QModelIndex()) override
Removes a number of columns from the model at the specified position.
Definition model.cpp:187
bool insertRows(int position, int rows, const QModelIndex &parent=QModelIndex()) override
Inserts a number of rows into the model at the specified position.
Definition model.cpp:128
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Changes an item in the model, but only if the following conditions are met:
Definition model.cpp:113
bool removeRows(int position, int rows, const QModelIndex &parent=QModelIndex()) override
Removes a number of rows from the model at the specified position.
Definition model.cpp:170
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns an appropriate value for the item's flags.
Definition model.cpp:95
bool insertColumns(int position, int columns, const QModelIndex &parent=QModelIndex()) override
Inserts a number of columns into the model at the specified position.
Definition model.cpp:150
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns the appropriate header string depending on the orientation of the header and the section.
Definition model.cpp:78
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of items in the first list item as the number of columns in the model.
Definition model.cpp:48
QVariant data(const QModelIndex &index, int role) const override
Returns an appropriate value for the requested data.
Definition model.cpp:61