Qt 4.8
qstringlistmodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 /*
43  A simple model that uses a QStringList as its data source.
44 */
45 
46 #include "qstringlistmodel.h"
47 
48 #ifndef QT_NO_STRINGLISTMODEL
49 
51 
97  : QAbstractListModel(parent)
98 {
99 }
100 
107  : QAbstractListModel(parent), lst(strings)
108 {
109 }
110 
123 {
124  if (parent.isValid())
125  return 0;
126 
127  return lst.count();
128 }
129 
140 {
141  if (index.row() < 0 || index.row() >= lst.size())
142  return QVariant();
143 
144  if (role == Qt::DisplayRole || role == Qt::EditRole)
145  return lst.at(index.row());
146 
147  return QVariant();
148 }
149 
158 Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const
159 {
160  if (!index.isValid())
162 
164 }
165 
175 bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role)
176 {
177  if (index.row() >= 0 && index.row() < lst.size()
178  && (role == Qt::EditRole || role == Qt::DisplayRole)) {
179  lst.replace(index.row(), value.toString());
180  emit dataChanged(index, index);
181  return true;
182  }
183  return false;
184 }
185 
197 bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent)
198 {
199  if (count < 1 || row < 0 || row > rowCount(parent))
200  return false;
201 
202  beginInsertRows(QModelIndex(), row, row + count - 1);
203 
204  for (int r = 0; r < count; ++r)
205  lst.insert(row, QString());
206 
207  endInsertRows();
208 
209  return true;
210 }
211 
223 bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent)
224 {
225  if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
226  return false;
227 
228  beginRemoveRows(QModelIndex(), row, row + count - 1);
229 
230  for (int r = 0; r < count; ++r)
231  lst.removeAt(row);
232 
233  endRemoveRows();
234 
235  return true;
236 }
237 
239 {
240  return s1.first < s2.first;
241 }
242 
244 {
245  return s1.first > s2.first;
246 }
247 
252 {
254 
256  for (int i = 0; i < lst.count(); ++i)
257  list.append(QPair<QString, int>(lst.at(i), i));
258 
259  if (order == Qt::AscendingOrder)
260  qSort(list.begin(), list.end(), ascendingLessThan);
261  else
262  qSort(list.begin(), list.end(), decendingLessThan);
263 
264  lst.clear();
265  QVector<int> forwarding(list.count());
266  for (int i = 0; i < list.count(); ++i) {
267  lst.append(list.at(i).first);
268  forwarding[list.at(i).second] = i;
269  }
270 
272  QModelIndexList newList;
273  for (int i = 0; i < oldList.count(); ++i)
274  newList.append(index(forwarding.at(oldList.at(i).row()), 0));
275  changePersistentIndexList(oldList, newList);
276 
278 }
279 
284 {
285  return lst;
286 }
287 
295 {
297  lst = strings;
299 }
300 
305 {
307 }
308 
310 
311 #endif // QT_NO_STRINGLISTMODEL
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Reimplemented Function
T1 first
Definition: qpair.h:65
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensio...
void endInsertRows()
Ends a row insertion operation.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Inserts count rows into the model, beginning at the given row.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void endResetModel()
Completes a model reset operation.
QStringList stringList() const
Returns the string list used by the model to store data.
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
static bool ascendingLessThan(const QPair< QString, int > &s1, const QPair< QString, int > &s2)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
SortOrder
Definition: qnamespace.h:189
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setStringList(const QStringList &strings)
Sets the model&#39;s internal string list to strings.
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
int row() const
Returns the row this model index refers to.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the data for the specified role in the item with the given index in the model, to the provided value.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
QStringListModel(QObject *parent=0)
Constructs a string list model with the given parent.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void endRemoveRows()
Ends a row removal operation.
void clear()
Removes all items from the list.
Definition: qlist.h:764
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
static bool decendingLessThan(const QPair< QString, int > &s1, const QPair< QString, int > &s2)
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qlist.h:609
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows in the model.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
The QModelIndex class is used to locate data in a data model.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the flags for the item with the given index.
Qt::DropActions supportedDropActions() const
Reimplemented Function
QVariant data(const QModelIndex &index, int role) const
Returns data for the specified role, from the item with the given index.
void beginResetModel()
Begins a model reset operation.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Removes count rows from the model, beginning at the given row.
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480