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 A simple model that uses a QStringList as its data source.
8*/
9
10#include "model.h"
11
18int StringListModel::rowCount(const QModelIndex &parent) const
19{
20 return stringList.count();
21}
23
24
25#ifdef 0
26// This represents a read-only version of data(), an early stage in the
27// development of the example leading to an editable StringListModel.
28
37QVariant StringListModel::data(const QModelIndex &index, int role) const
38{
39 if (!index.isValid())
40 return QVariant();
41
42 if (index.row() >= stringList.size())
43 return QVariant();
44
45 if (role == Qt::DisplayRole)
46 return stringList.at(index.row());
47 else
48 return QVariant();
49}
51#endif
52
53
62QVariant StringListModel::data(const QModelIndex &index, int role) const
63{
64 if (!index.isValid())
65 return QVariant();
66
67 if (index.row() >= stringList.size())
68 return QVariant();
69
70 if (role == Qt::DisplayRole || role == Qt::EditRole)
71 return stringList.at(index.row());
72 else
73 return QVariant();
74}
76
85 int role) const
86{
87 if (role != Qt::DisplayRole)
88 return QVariant();
89
90 if (orientation == Qt::Horizontal)
91 return QStringLiteral("Column %1").arg(section);
92 else
93 return QStringLiteral("Row %1").arg(section);
94}
96
103Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
104{
105 if (!index.isValid())
106 return Qt::ItemIsEnabled;
107
109}
111
125 const QVariant &value, int role)
126{
127 if (index.isValid() && role == Qt::EditRole) {
128
129 stringList.replace(index.row(), value.toString());
130 emit dataChanged(index, index, {role});
131 return true;
132 }
134 return false;
135}
137
143bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
144{
146
147 for (int row = 0; row < rows; ++row) {
148 stringList.insert(position, "");
149 }
150
152 return true;
154}
156
162bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
163{
165
166 for (int row = 0; row < rows; ++row) {
167 stringList.removeAt(position);
168 }
169
171 return true;
173}
virtual Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
void endRemoveRows()
Ends a row 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 endInsertRows()
Ends a row 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.
\inmodule QtCore
\inmodule QtCore
Definition qvariant.h:65
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
[3]
Definition model.cpp:142
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
[3]
Definition model.cpp:123
QVariant data(const QModelIndex &index, int role) const override
[0]
Definition model.cpp:61
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of items in the string list as the number of rows in the model.
Definition model.cpp:18
Qt::ItemFlags flags(const QModelIndex &index) const override
[1]
Definition model.cpp:102
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
[1]
Definition model.cpp:83
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
[7]
Definition model.cpp:161
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ EditRole
@ DisplayRole
@ ItemIsEditable
@ ItemIsEnabled
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint index
[2]
GLenum GLenum GLsizei void * row
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define QStringLiteral(str)
#define emit