1
/****************************************************************************
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtGui module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
43
A simple model that uses a QVariantList as its data source.
46
// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
47
// To use the same method of setting role name that it does, we
48
// set our compatibility to Qt4 here too.
49
#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
51
#include "qvariantlistmodel.h"
53
#include <QtCore/qvector.h>
56
\class QVariantListModel
57
\brief The QVariantListModel class provides a model that supplies variants to views.
59
QVariantListModel is an editable model that can be used for simple
60
cases where you need to display a number of variants in a view.
62
The model provides all the standard functions of an editable
63
model, representing the data in the variant list as a model with
64
one column and a number of rows equal to the number of items in
67
Model indexes corresponding to items are obtained with the
68
\l{QAbstractListModel::index()}{index()} function. Item data is
69
read with the data() function and written with setData().
70
The number of rows (and number of items in the variant list)
71
can be found with the rowCount() function.
73
The model can be constructed with an existing variant list, or
74
variants can be set later with the setVariantList() convenience
75
function. Variants can also be inserted in the usual way with the
76
insertRows() function, and removed with removeRows(). The contents
77
of the variant list can be retrieved with the variantList()
80
\sa QAbstractListModel, QAbstractItemModel, {Model Classes}
84
Constructs a variant list model with the given \a parent.
87
QVariantListModel::QVariantListModel(QObject *parent) :
88
QAbstractListModel(parent)
90
QHash<int, QByteArray> roles(roleNames());
91
roles[Qt::DisplayRole] = "modelData";
96
Constructs a variant list model containing the specified \a list
97
with the given \a parent.
100
QVariantListModel::QVariantListModel(const QVariantList &list, QObject *parent) :
101
QAbstractListModel(parent), lst(list)
103
QHash<int, QByteArray> roles(roleNames());
104
roles[Qt::DisplayRole] = "modelData";
108
QVariantListModel::~QVariantListModel() {
112
Returns the number of rows in the model. This value corresponds to the
113
number of items in the model's internal variant list.
115
The optional \a parent argument is in most models used to specify
116
the parent of the rows to be counted. Because this is a list if a
117
valid parent is specified, the result will always be 0.
119
\sa insertRows(), removeRows(), QAbstractItemModel::rowCount()
122
int QVariantListModel::rowCount(const QModelIndex &parent) const
124
if (parent.isValid())
133
QModelIndex QVariantListModel::sibling(int row, int column,
134
const QModelIndex &idx) const
136
if (!idx.isValid() || column != 0 || row >= lst.count())
137
return QModelIndex();
139
return createIndex(row, 0);
143
Returns data for the specified \a role, from the item with the
146
If the view requests an invalid index, an invalid variant is returned.
151
QVariant QVariantListModel::data(const QModelIndex &index, int role) const
153
if (index.row() < 0 || index.row() >= lst.size())
156
if (role == Qt::DisplayRole || role == Qt::EditRole)
157
return lst.at(index.row());
163
Sets the data for the specified \a role in the item with the given
164
\a index in the model, to the provided \a value.
166
The dataChanged() signal is emitted if the item is changed.
168
\sa Qt::ItemDataRole, data()
171
bool QVariantListModel::setData(const QModelIndex &index, const QVariant &value,
174
if (index.row() >= 0 && index.row() < lst.size()
175
&& (role == Qt::EditRole || role == Qt::DisplayRole))
177
lst.replace(index.row(), value);
178
dataChanged(index, index, QVector<int>() << role);
185
Inserts \a count rows into the model, beginning at the given \a row.
187
The \a parent index of the rows is optional and is only used for
188
consistency with QAbstractItemModel. By default, a null index is
189
specified, indicating that the rows are inserted in the top level of
192
\sa QAbstractItemModel::insertRows()
195
bool QVariantListModel::insertRows(int row, int count,
196
const QModelIndex &parent)
198
if (count < 1 || row < 0 || row > rowCount(parent))
201
beginInsertRows(QModelIndex(), row, row + count - 1);
203
for (int r = 0; r < count; ++r)
204
lst.insert(row, QVariant());
212
Removes \a count rows from the model, beginning at the given \a row.
214
The \a parent index of the rows is optional and is only used for
215
consistency with QAbstractItemModel. By default, a null index is
216
specified, indicating that the rows are removed in the top level of
219
\sa QAbstractItemModel::removeRows()
222
bool QVariantListModel::removeRows(int row, int count,
223
const QModelIndex &parent)
225
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
228
beginRemoveRows(QModelIndex(), row, row + count - 1);
230
for (int r = 0; r < count; ++r)
239
Returns the variant list used by the model to store data.
241
QVariantList QVariantListModel::variantList() const
247
Sets the model's internal variant list to \a list. The model will
248
notify any attached views that its underlying data has changed.
252
void QVariantListModel::setVariantList(const QVariantList &list)
254
int size = lst.size();
255
bool sameSize = list.size() == size;
266
dataChanged(QAbstractListModel::index(0),
267
QAbstractListModel::index(size - 1));