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
qstringlistmodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4/*
5 A simple model that uses a QStringList as its data source.
6*/
7
8#include "qstringlistmodel.h"
9
10#include <QtCore/qlist.h>
11#include <QtCore/qmap.h>
12
13#include <algorithm>
14
16
62
72
85{
86 if (parent.isValid())
87 return 0;
88
89 return lst.size();
90}
91
96{
97 if (!idx.isValid() || column != 0 || row >= lst.size() || row < 0)
98 return QModelIndex();
99
100 return createIndex(row, 0);
101}
102
107QMap<int, QVariant> QStringListModel::itemData(const QModelIndex &index) const
108{
110 return QMap<int, QVariant>{};
111 const QVariant displayData = lst.at(index.row());
112 return QMap<int, QVariant>{{
113 std::make_pair<int>(Qt::DisplayRole, displayData),
114 std::make_pair<int>(Qt::EditRole, displayData)
115 }};
116}
117
123bool QStringListModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
124{
125 if (roles.isEmpty())
126 return false;
127 if (std::any_of(roles.keyBegin(), roles.keyEnd(), [](int role) -> bool {
128 return role != Qt::DisplayRole && role != Qt::EditRole;
129 })) {
130 return false;
131 }
132 auto roleIter = roles.constFind(Qt::EditRole);
133 if (roleIter == roles.constEnd())
134 roleIter = roles.constFind(Qt::DisplayRole);
135 Q_ASSERT(roleIter != roles.constEnd());
136 return setData(index, roleIter.value(), roleIter.key());
137}
138
149{
150 if (index.row() < 0 || index.row() >= lst.size())
151 return QVariant();
152
153 if (role == Qt::DisplayRole || role == Qt::EditRole)
154 return lst.at(index.row());
155
156 return QVariant();
157}
158
174
186{
187 if (index.row() >= 0 && index.row() < lst.size()
188 && (role == Qt::EditRole || role == Qt::DisplayRole)) {
189 const QString valueString = value.toString();
190 if (lst.at(index.row()) == valueString)
191 return true;
192 lst.replace(index.row(), valueString);
194 return true;
195 }
196 return false;
197}
198
207
222{
223 if (count < 1 || row < 0 || row > rowCount(parent))
224 return false;
225
227
228 for (int r = 0; r < count; ++r)
229 lst.insert(row, QString());
230
232
233 return true;
234}
235
250{
251 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
252 return false;
253
255
256 const auto it = lst.begin() + row;
257 lst.erase(it, it + count);
258
260
261 return true;
262}
263
268bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
269{
270 if (sourceRow < 0
271 || sourceRow + count - 1 >= rowCount(sourceParent)
272 || destinationChild < 0
276 || count <= 0
277 || sourceParent.isValid()
279 return false;
280 }
282 return false;
283
284 int fromRow = sourceRow;
286 fromRow += count - 1;
287 else
289 while (count--)
290 lst.move(fromRow, destinationChild);
291 endMoveRows();
292 return true;
293}
294
295static bool ascendingLessThan(const std::pair<QString, int> &s1, const std::pair<QString, int> &s2)
296{
297 return s1.first < s2.first;
298}
299
300static bool decendingLessThan(const std::pair<QString, int> &s1, const std::pair<QString, int> &s2)
301{
302 return s1.first > s2.first;
303}
304
309{
310 emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
311
312 QList<std::pair<QString, int>> list;
313 const int lstCount = lst.size();
314 list.reserve(lstCount);
315 for (int i = 0; i < lstCount; ++i)
316 list.emplace_back(lst.at(i), i);
317
319 std::sort(list.begin(), list.end(), ascendingLessThan);
320 else
321 std::sort(list.begin(), list.end(), decendingLessThan);
322
323 lst.clear();
324 QList<int> forwarding(lstCount);
325 for (int i = 0; i < lstCount; ++i) {
326 lst.append(list.at(i).first);
327 forwarding[list.at(i).second] = i;
328 }
329
331 QModelIndexList newList;
332 const int numOldIndexes = oldList.size();
333 newList.reserve(numOldIndexes);
334 for (int i = 0; i < numOldIndexes; ++i)
335 newList.append(index(forwarding.at(oldList.at(i).row()), 0));
336 changePersistentIndexList(oldList, newList);
337
338 emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
339}
340
345{
346 return lst;
347}
348
361
369
371
372#include "moc_qstringlistmodel.cpp"
virtual Qt::DropActions supportedDropActions() const
void endResetModel()
Completes a model reset operation.
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
void endRemoveRows()
Ends a row removal operation.
QModelIndexList persistentIndexList() const
void endMoveRows()
Ends a row move operation.
Q_INVOKABLE int int const QModelIndex & destinationParent
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
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.
Q_INVOKABLE int int const QModelIndex int destinationChild
void layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options=CheckIndexOption::NoOption) const
Q_INVOKABLE int sourceRow
void endInsertRows()
Ends a row insertion operation.
void beginResetModel()
Begins a model reset operation.
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
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.
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
qsizetype size() const noexcept
Definition qlist.h:397
reference emplace_back(Args &&... args)
Definition qlist.h:683
iterator end()
Definition qlist.h:626
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
iterator begin()
Definition qlist.h:625
void reserve(qsizetype size)
Definition qlist.h:753
\inmodule QtCore
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
\inmodule QtCore
Definition qobject.h:103
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
\reimp
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Removes count rows from the model, beginning at the given row.
QStringList stringList() const
Returns the string list used by the model to store data.
QStringListModel(QObject *parent=nullptr)
Constructs a string list model with the given parent.
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
\reimp
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Inserts count rows into the model, beginning at the given row.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the flags for the item with the given index.
QMap< int, QVariant > itemData(const QModelIndex &index) const override
\reimp
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows in the model.
void setStringList(const QStringList &strings)
Sets the model's internal string list to strings.
bool clearItemData(const QModelIndex &index) override
\reimp
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
\reimp
Qt::DropActions supportedDropActions() const override
\reimp
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns data for the specified role, from the item with the given index.
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the data for the specified role in the item with the given index in the model,...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ EditRole
@ DisplayRole
SortOrder
Definition qnamespace.h:121
@ AscendingOrder
Definition qnamespace.h:122
@ MoveAction
@ ItemIsEditable
@ ItemIsDragEnabled
@ ItemIsDropEnabled
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint index
[2]
GLboolean r
[2]
GLsizei const GLchar ** strings
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLenum GLenum GLsizei count
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
GLfixed GLfixed GLint GLint order
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool ascendingLessThan(const std::pair< QString, int > &s1, const std::pair< QString, int > &s2)
static bool decendingLessThan(const std::pair< QString, int > &s1, const std::pair< QString, int > &s2)
#define s2
#define emit
QList< int > list
[14]