5
6
7
8
15
16
18TableModel::TableModel(
int rows,
int columns, QObject *parent)
19 : QAbstractTableModel(parent)
23 for (
int column = 0; column < qMax(1, columns); ++column) {
24 newList.append(QString());
27 for (
int row = 0; row < qMax(1, rows); ++row) {
28 rowList.append(newList);
34
35
36
40 return rowList.size();
44
45
46
50 return rowList[0].size();
54
55
56
57
58
59
66 if (role == Qt::DisplayRole)
67 return rowList[index.row()][index.column()];
73
74
75
76
81 if (role != Qt::DisplayRole)
84 if (orientation == Qt::Horizontal)
85 return QStringLiteral(
"Column %1").arg(section);
87 return QStringLiteral(
"Row %1").arg(section);
91
92
93
98 return Qt::ItemIsEnabled;
100 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
104
105
106
107
108
109
110
111
114 const QVariant &value,
int role)
116 if (!index.isValid() || role != Qt::EditRole)
119 rowList[index.row()][index.column()] = value.toString();
120 emit dataChanged(index, index, {role});
125
126
130 int columns = columnCount();
131 beginInsertRows(parent, position, position + rows - 1);
133 for (
int row = 0; row < rows; ++row) {
135 for (
int column = 0; column < columns; ++column)
136 items.append(QString());
137 rowList.insert(position, items);
145
146
147
148
151 const QModelIndex &parent)
153 int rows = rowCount();
154 beginInsertColumns(parent, position, position + columns - 1);
156 for (
int row = 0; row < rows; ++row) {
157 for (
int column = position; column < columns; ++column) {
158 rowList[row].insert(position, QString());
167
168
172 beginRemoveRows(parent, position, position + rows - 1);
174 for (
int row = 0; row < rows; ++row) {
175 rowList.removeAt(position);
183
184
185
188 const QModelIndex &parent)
190 int rows = rowCount();
191 beginRemoveColumns(parent, position, position + columns - 1);
193 for (
int row = 0; row < rows; ++row) {
194 for (
int column = 0; column < columns; ++column) {
195 rowList[row].removeAt(position);
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.
bool removeColumns(int position, int columns, const QModelIndex &parent=QModelIndex()) override
Removes a number of columns from the model at the specified position.
bool insertRows(int position, int rows, const QModelIndex &parent=QModelIndex()) override
Inserts a number of rows into the model at the specified position.
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:
bool removeRows(int position, int rows, const QModelIndex &parent=QModelIndex()) override
Removes a number of rows from the model at the specified position.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns an appropriate value for the item's flags.
bool insertColumns(int position, int columns, const QModelIndex &parent=QModelIndex()) override
Inserts a number of columns into the model at the specified position.
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.
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.
QVariant data(const QModelIndex &index, int role) const override
Returns an appropriate value for the requested data.