Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
38int TableModel::rowCount(const QModelIndex &/*parent*/) const
39{
40 return rowList.size();
41}
42
48int TableModel::columnCount(const QModelIndex &/*parent*/) const
49{
50 return rowList[0].size();
51}
52
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
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
95Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
96{
97 if (!index.isValid())
98 return Qt::ItemIsEnabled;
99
101}
102
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
128bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
129{
130 int columns = columnCount();
132
133 for (int row = 0; row < rows; ++row) {
135 for (int column = 0; column < columns; ++column)
137 rowList.insert(position, items);
138 }
139
141 return true;
142}
143
151 const QModelIndex &parent)
152{
153 int rows = rowCount();
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
163 return true;
164}
165
170bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
171{
173
174 for (int row = 0; row < rows; ++row) {
175 rowList.removeAt(position);
176 }
177
179 return true;
180}
181
188 const QModelIndex &parent)
189{
190 int rows = rowCount();
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
200 return true;
201}
void endRemoveRows()
Ends a row removal operation.
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
This signal is emitted whenever the data in an existing item changes.
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
void endInsertRows()
Ends a row insertion operation.
void endRemoveColumns()
Ends a column removal operation.
void endInsertColumns()
Ends a column insertion operation.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
qsizetype size() const noexcept
Definition qlist.h:397
void removeAt(qsizetype i)
Definition qlist.h:590
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:488
void append(parameter_type t)
Definition qlist.h:458
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
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
TableModel(int rows=1, int columns=1, QObject *parent=nullptr)
Constructs a table model with at least one row and one column.
Definition model.cpp:18
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
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ EditRole
@ DisplayRole
@ ItemIsEditable
@ ItemIsEnabled
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLuint index
[2]
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define QStringLiteral(str)
#define emit
QList< QTreeWidgetItem * > items