1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qabstractitemmodel.h"
30
#include <qdatastream.h>
31
#include <qstringlist.h>
33
#include <qmimedata.h>
36
#include <private/qabstractitemmodel_p.h>
37
#include <qbitarray.h>
41
QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index)
43
Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list
44
QPersistentModelIndexData *d = 0;
45
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(index.model());
46
QList<QPersistentModelIndexData*> *persistentIndexes = &(model->d_func()->persistent.indexes);
47
for (int i = 0; i < persistentIndexes->count(); ++i) {
48
if (persistentIndexes->at(i)->index == index) {
49
d = persistentIndexes->at(i);
53
if (!d) { // not found
54
d = new QPersistentModelIndexData();
57
persistentIndexes->append(d);
63
void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
66
QAbstractItemModel *model = const_cast<QAbstractItemModel*>(data->model);
67
// a valid persistent model index with a null model pointer can only happen if the model was destroyed
69
QAbstractItemModelPrivate *p = model->d_func();
70
int position = p->persistent.indexes.indexOf(data);
71
p->persistent.changed.removeAll(position);
72
p->persistent.invalidated.removeAll(position);
73
p->persistent.indexes.removeAll(data);
79
\class QPersistentModelIndex
81
\brief The QPersistentModelIndex class is used to locate data in a data model.
85
A QPersistentModelIndex is a model index that can be stored by an
86
application, and later used to access information in a model.
87
Unlike the QModelIndex class, it is safe to store a
88
QPersistentModelIndex since the model will ensure that references
89
to data will continue to be valid as long as that data exists within
92
It is good practice to check that persistent model indexes are valid
95
\sa \link model-view-programming.html Model/View Programming\endlink QModelIndex QAbstractItemModel
100
\fn QPersistentModelIndex::QPersistentModelIndex()
105
QPersistentModelIndex::QPersistentModelIndex()
111
\fn QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
113
Creates a new QPersistentModelIndex that is a copy of the \a other persistent
117
QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
124
Creates a new QPersistentModelIndex that is a copy of the model \a index.
127
QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)
130
if (index.isValid()) {
131
d = QPersistentModelIndexData::create(index);
137
\fn QPersistentModelIndex::~QPersistentModelIndex()
142
QPersistentModelIndex::~QPersistentModelIndex()
144
if (d && !d->ref.deref()) {
145
QPersistentModelIndexData::destroy(d);
151
\fn bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const
153
Returns true if this persistent model index is equal to the \a other
154
persistent model index, otherwist returns false.
157
bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const
160
return d->index == other.d->index;
165
\fn bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const
167
Returns true if this persistent model index is smaller than the \a other
168
persistent model index; otherwise returns false.
171
bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const
177
Sets the persistent model index to refer to the same item in a model
178
as the \a other persistent model index.
181
QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other)
185
if (d && !d->ref.deref())
186
QPersistentModelIndexData::destroy(d);
193
Sets the persistent model index to refer to the same item in a model
194
as the \a other model index.
197
QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other)
199
if (d && !d->ref.deref())
200
QPersistentModelIndexData::destroy(d);
201
if (other.isValid()) {
202
d = QPersistentModelIndexData::create(other);
211
\fn QPersistentModelIndex::operator const QModelIndex&() const
213
Cast operator that returns a const QModelIndex&.
216
QPersistentModelIndex::operator const QModelIndex&() const
218
static const QModelIndex invalid;
225
\fn bool QPersistentModelIndex::operator==(const QModelIndex &other) const
227
Returns true if this persistent model index refers to the same location as
228
the \a other model index; otherwise returns false.
231
bool QPersistentModelIndex::operator==(const QModelIndex &other) const
234
return d->index == other;
235
return !other.isValid();
239
\fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
241
Returns true if this persistent model index does not refer to the same
242
location as the \a other model index; otherwise returns false.
245
bool QPersistentModelIndex::operator!=(const QModelIndex &other) const
248
return d->index != other;
249
return other.isValid();
253
\fn int QPersistentModelIndex::row() const
255
Returns the row this persistent model index refers to.
258
int QPersistentModelIndex::row() const
261
return d->index.row();
266
\fn int QPersistentModelIndex::column() const
268
Returns the column this persistent model index refers to.
271
int QPersistentModelIndex::column() const
274
return d->index.column();
279
\fn void *QPersistentModelIndex::internalPointer() const
283
Returns a \c{void} \c{*} pointer used by the model to associate the index with
284
the internal data structure.
287
void *QPersistentModelIndex::internalPointer() const
290
return d->index.internalPointer();
295
\fn void *QPersistentModelIndex::internalId() const
299
Returns a \c{qint64} used by the model to associate the index with
300
the internal data structure.
303
qint64 QPersistentModelIndex::internalId() const
306
return d->index.internalId();
311
Returns the parent QModelIndex for this persistent index, or
312
QModelIndex() if it has no parent.
314
\sa child() sibling() model()
316
QModelIndex QPersistentModelIndex::parent() const
319
return d->index.parent();
320
return QModelIndex();
324
Returns the sibling at \a row and \a column or an invalid
325
QModelIndex if there is no sibling at this position.
330
QModelIndex QPersistentModelIndex::sibling(int row, int column) const
333
return d->index.sibling(row, column);
334
return QModelIndex();
338
Returns the child of the model index that is stored in the given
339
\a row and \a column.
341
\sa parent() sibling()
344
QModelIndex QPersistentModelIndex::child(int row, int column) const
347
return d->index.child(row, column);
348
return QModelIndex();
352
Returns the model that the index belongs to.
354
const QAbstractItemModel *QPersistentModelIndex::model() const
357
return d->index.model();
362
\fn bool QPersistentModelIndex::isValid() const
364
Returns true if this persistent model index is valid; otherwise returns
368
bool QPersistentModelIndex::isValid() const
370
return d && d->index.isValid();
373
#ifndef QT_NO_DEBUG_STREAM
374
QDebug operator<<(QDebug dbg, const QModelIndex &idx)
376
#ifndef Q_BROKEN_DEBUG_STREAM
377
dbg.nospace() << "QModelIndex(" << idx.row() << "," << idx.column()
378
<< "," << idx.internalId() << "," << idx.model() << ")";
381
qWarning("This compiler doesn't support streaming QModelIndex to QDebug");
387
QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
392
dbg << QModelIndex();
397
QAbstractItemModelPrivate::~QAbstractItemModelPrivate()
399
QList<QPersistentModelIndexData*>::iterator it = persistent.indexes.begin();
400
for (; it != persistent.indexes.end(); ++it) {
402
(*it)->index = QModelIndex();
407
void QAbstractItemModelPrivate::invalidate(int position)
409
QModelIndex parent = persistent.indexes.at(position)->index;
410
for (int i = 0; i < persistent.indexes.count(); ++i) {
411
if (persistent.indexes.at(i)->index.parent() == parent) {
412
Q_ASSERT((persistent.indexes.at(i)));
413
invalidate(i); // recursive
416
persistent.indexes[position]->index = QModelIndex();
419
void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
423
persistent.changed.clear();
424
for (int position = 0; position < persistent.indexes.count(); ++position) {
425
QModelIndex index = persistent.indexes.at(position)->index;
426
if (index.isValid() && index.parent() == parent && index.row() >= first)
427
persistent.changed.append(position);
431
void QAbstractItemModelPrivate::rowsInserted(const QModelIndex &parent,
434
int count = (last - first) + 1;
435
for (int i = 0; i < persistent.changed.count(); ++i) {
436
int position = persistent.changed.at(i);
437
QModelIndex old = persistent.indexes.at(position)->index;
438
persistent.indexes[position]->index =
439
q_func()->index(old.row() + count, old.column(), parent);
441
persistent.changed.clear();
444
void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
447
persistent.changed.clear();
448
persistent.invalidated.clear();
449
for (int position = 0; position < persistent.indexes.count(); ++position) {
450
QModelIndex index = persistent.indexes.at(position)->index;
451
if (index.isValid() && index.parent() == parent) {
452
if (index.row() > last) // below the removed rows
453
persistent.changed.append(position);
454
else if (index.row() >= first) // about to be removed
455
persistent.invalidated.append(position);
460
void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent,
463
int count = (last - first) + 1;
464
for (int i = 0; i < persistent.changed.count(); ++i) {
465
int position = persistent.changed.at(i);
466
QModelIndex old = persistent.indexes.at(position)->index;
467
persistent.indexes[position]->index =
468
q_func()->index(old.row() - count, old.column(), parent);
470
persistent.changed.clear();
471
for (int j = 0; j < persistent.invalidated.count(); ++j)
472
invalidate(persistent.invalidated.at(j));
473
persistent.invalidated.clear();
476
void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent,
480
persistent.changed.clear();
481
for (int position = 0; position < persistent.indexes.count(); ++position) {
482
QModelIndex index = persistent.indexes.at(position)->index;
483
if (index.isValid() && index.parent() == parent && index.column() >= first)
484
persistent.changed.append(position);
488
void QAbstractItemModelPrivate::columnsInserted(const QModelIndex &parent,
491
int count = (last - first) + 1;
492
for (int i = 0; i < persistent.changed.count(); ++i) {
493
int position = persistent.changed.at(i);
494
QModelIndex old = persistent.indexes.at(position)->index;
495
persistent.indexes[position]->index =
496
q_func()->index(old.row(), old.column() + count, parent);
498
persistent.changed.clear();
501
void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent,
504
persistent.changed.clear();
505
persistent.invalidated.clear();
506
for (int position = 0; position < persistent.indexes.count(); ++position) {
507
QModelIndex index = persistent.indexes.at(position)->index;
508
if (index.isValid() && index.parent() == parent) {
509
if (index.column() > last) // after the removed columns
510
persistent.changed.append(position);
511
else if (index.column() >= first) // about to be removed
512
persistent.invalidated.append(position);
517
void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent,
520
int count = (last - first) + 1;
521
for (int i = 0; i < persistent.changed.count(); ++i) {
522
int position = persistent.changed.at(i);
523
QModelIndex old = persistent.indexes.at(position)->index;
524
persistent.indexes[position]->index =
525
q_func()->index(old.row(), old.column() - count, parent);
527
persistent.changed.clear();
528
for (int j = 0; j < persistent.invalidated.count(); ++j)
529
invalidate(persistent.invalidated.at(j));
530
persistent.invalidated.clear();
533
void QAbstractItemModelPrivate::reset()
535
for (int i = 0; i < persistent.indexes.count(); ++i)
536
persistent.indexes[i]->index = QModelIndex();
540
\class QModelIndex qabstractitemmodel.h
542
\brief The QModelIndex class is used to locate data in a data model.
547
This class is used as an index into item models derived from
548
QAbstractItemModel. The index is used by item views, delegates, and
549
selection models to locate an item in the model. QModelIndex objects are
550
created by the model.
552
Model indexes contain all the information required to specify the items
553
they refer to in a model. Indexes are located in a row and a column, and
554
they may have a parent index; use row(), column(), and parent() to obtain
555
this information. Top-level items in a model are represented by model
556
indexes that do not have a parent index - in this case, parent() will
557
return an invalid model index that is equivalent to an index constructed
558
with the zero argument form of the QModelIndex() constructor.
560
To obtain a model index that refers to an item in a model, call
561
QAbstractItemModel::index() with the required row and column
562
values, and the model index of the parent. When referring to
563
top-level items in a model, supply QModelIndex() as the parent index.
565
The model() function returns the model that the index references as a
567
The child() function is used to examine the items held beneath the index
569
The sibling() function allows you to traverse items in the model on the
570
same level as the index.
572
Model indexes can become invalid over time so they should be used
573
immediately and then discarded. If you need to keep a model index
574
over time use a QPersistentModelIndex.
576
\sa \link model-view-programming.html Model/View Programming\endlink QPersistentModelIndex QAbstractItemModel
580
\fn QModelIndex::QModelIndex()
582
Creates a new empty model index.
583
This type of model index is used to indicate
584
that the position in the model is invalid.
586
\sa isValid() QAbstractItemModel
590
\fn QModelIndex::QModelIndex(int row, int column, void *data, const QAbstractItemModel *model)
594
Creates a new model index at the given \a row and \a column,
595
pointing to some \a data.
599
\fn QModelIndex::QModelIndex(const QModelIndex &other)
601
Creates a new model index that is a copy of the \a other model
606
\fn QModelIndex::~QModelIndex()
608
Destroys the model index.
612
\fn int QModelIndex::row() const
614
Returns the row this model index refers to.
619
\fn int QModelIndex::column() const
621
Returns the column this model index refers to.
626
\fn void *QModelIndex::internalPointer() const
628
Returns a \c{void} \c{*} pointer used by the model to associate
629
the index with the internal data structure.
633
\fn void *QModelIndex::internalId() const
635
Returns a \c{qint64} used by the model to associate
636
the index with the internal data structure.
640
\fn bool QModelIndex::isValid() const
642
Returns true if this model index is valid; otherwise returns
647
\fn const QAbstractItemModel *QModelIndex::model() const
649
Returns a pointer to the model containing the item that this index
654
\fn QModelIndex QModelIndex::sibling(int row, int column) const
656
Returns the sibling at \a row and \a column or an invalid
657
QModelIndex if there is no sibling at this position.
663
\fn QModelIndex QModelIndex::child(int row, int column) const
665
Returns the child of the model index that is stored in the given
666
\a row and \a column.
668
\sa parent() sibling()
672
\fn bool QModelIndex::operator==(const QModelIndex &other) const
674
Returns true if this model index refers to the same location as
675
the \a other model index; otherwise returns false.
680
\fn bool QModelIndex::operator!=(const QModelIndex &other) const
682
Returns true if this model index does not refer to the same
683
location as the \a other model index; otherwise returns false.
688
\fn QModelIndex QModelIndex::parent() const
690
Return the parent of the model index, or QModelIndex() if it has no
693
\sa child() sibling() model()
697
\class QAbstractItemModel qabstractitemmodel.h
699
\brief The QAbstractItemModel class provides the abstract interface for
705
The QAbstractItemModel class defines the standard interface that
706
item models must use to be able to interoperate with other
707
components in the model/view architecture. It is not supposed to
708
be instantiated directly. Instead, you should subclass it to create
711
The QAbstractItemModel class is one of the \l{Model/View Classes}
712
and is part of Qt's \l{Model/View Programming}{model/view framework}.
714
If you need a model to use with a QListView or a QTableView, you
715
should consider subclassing QAbstractListModel or QAbstractTableModel
716
instead of this class.
718
The underlying data model is exposed to views and delegates as
719
a hierarchy of tables. If you don't make use of the hierarchy,
720
then the model is a simple table of rows and columns. Each item
721
has a unique index specified by a QModelIndex.
723
\img modelindex-no-parent.png
725
Every item of data that can be accessed via a model has an associated
726
model index that is obtained using the index() function. Each index
727
may have a sibling() index; child items have a parent() index.
729
Each item has a number of data elements associated with it, and each
730
of these can be retrieved by specifying a role (see \l Qt::ItemDataRole)
731
to the model's data() function. Data for all available roles can be
732
obtained at the same time using the itemData() function.
734
Data for each role is set using a particular \l Qt::ItemDataRole.
735
Data for individual roles are set individually with setData(), or they
736
can be set for all roles with setItemData().
738
Items can be queried with flags() (see \l Qt::ItemFlag) to see if they
739
can be selected, dragged, or manipulated in other ways.
741
If an item has child objects, hasChildren() returns true for the
744
The model has a rowCount() and a columnCount() for each level of
745
the hierarchy. Rows and columns can be inserted and removed with
746
insertRows(), insertColumns(), removeRows(), and removeColumns().
748
The model emits signals to indicate changes. For example,
749
dataChanged() is emitted whenever items of data made available by
750
the model are changed. Changes to the headers supplied by the model
751
cause headerDataChanged() to be emitted. If the structure of the
752
underlying data changes, the model can emit layoutChanged() to
753
indicate to any attached views that they should redisplay any items
754
shown, taking the new structure into account.
756
The items available through the model can be searched for particular
757
data using the match() function.
759
If the model is sortable, it can be sorted with sort().
761
\section1 Subclassing
763
When subclassing QAbstractItemModel, at the very least you must
764
implement index(), parent(), rowCount(), columnCount(), hasChildren(),
765
and data(). To enable editing in your model, you must
766
also implement setData(), and reimplement flags() to ensure that
767
\c ItemIsEditable is returned.
769
You can also reimplement headerData() and setHeaderData() to control
770
the way the headers for your model are presented.
772
Models that provide interfaces to resizable data structures can
773
provide implementations of insertRows(), removeRows(), insertColumns(),
774
and removeColumns(). When implementing these functions, it is
775
important to notify any connected views about changes to the model's
776
dimensions both \e before and \e after they occur:
779
\o An insertRows() implementation must call beginInsertRows()
780
\e before inserting new rows into the data structure, and it must
781
call endInsertRows() \e{immediately afterwards}.
782
\o An insertColumns() implementation must call beginInsertColumns()
783
\e before inserting new columns into the data structure, and it must
784
call endInsertColumns() \e{immediately afterwards}.
785
\o A removeRows() implementation must call beginRemoveRows()
786
\e before the rows are removed from the data structure, and it must
787
call endRemoveRows() \e{immediately afterwards}.
788
\o A removeColumns() implementation must call beginRemoveColumns()
789
\e before the columns are removed from the data structure, and it must
790
call endRemoveColumns() \e{immediately afterwards}.
793
The signals that these functions emit give attached components the chance
794
to take action before any data becomes unavailable. The encapsulation of
795
the insert and remove operations with these begin and end functions also
796
enable the model to manage
797
\l{QPersistentModelIndex}{persistent model indexes} correctly.
798
\bold{If you want selections to be handled properly, you must ensure that
799
you call these functions.}
801
\sa \link model-view-programming.html Model/View Programming\endlink QModelIndex QAbstractItemView
806
\fn QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const = 0
808
Returns the index of the item in the model specified by the given \a row,
809
\a column and \a parent index.
813
\fn bool QAbstractItemModel::insertColumn(int column, const QModelIndex &parent)
815
Inserts a single column before the given \a column in the child items of
816
the \a parent specified. Returns true if the column is inserted; otherwise
819
\sa insertColumns() insertRow() removeColumn()
823
\fn bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent)
825
Inserts a single row before the given \a row in the child items of the
826
\a parent specified. Returns true if the row is inserted; otherwise
829
\sa insertRows() insertColumn() removeRow()
833
\fn QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const = 0
835
Returns the parent of the model item with the given \a index.
839
\fn bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent)
841
Removes the given \a column from the child items of the \a parent specified.
842
Returns true if the column is removed; otherwise returns false.
844
\sa removeColumns(), removeRow(), insertColumn()
848
\fn bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent)
850
Removes the given \a row from the child items of the \a parent specified.
851
Returns true if the row is removed; otherwise returns false.
853
\sa removeRows(), removeColumn(), insertRow()
857
\fn void QAbstractItemModel::headerDataChanged(Qt::Orientation orientation, int first, int last)
859
This signal is emitted whenever a header is changed. The \a orientation
860
indicates whether the horizontal or vertical header has changed. The
861
sections in the header from the \a first to the \a last need to be updated.
863
\sa headerData(), setHeaderData(), dataChanged()
867
Constructs an abstract item model with the given \a parent.
869
QAbstractItemModel::QAbstractItemModel(QObject *parent)
870
: QObject(*new QAbstractItemModelPrivate, parent)
877
QAbstractItemModel::QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent)
878
: QObject(dd, parent)
883
Destroys the abstract item model.
885
QAbstractItemModel::~QAbstractItemModel()
890
\fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const
892
Returns the sibling at \a row and \a column for the item at \a
893
index or an invalid QModelIndex if there is no sibling.
895
\a row, \a column, and \a index.
900
\fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const = 0
902
Returns the number of rows under the given \a parent.
907
\fn int QAbstractItemModel::columnCount(const QModelIndex &parent) const = 0;
908
Returns the number of columns for the given \a parent.
912
\fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
914
This signal is emitted whenever the data in an existing item
915
changes. The affected items are those between \a topLeft and \a
916
bottomRight inclusive.
918
\sa headerDataChanged(), setData(), layoutChanged()
922
\fn void QAbstractItemModel::rowsInserted(const QModelIndex &parent, int start, int end)
925
This signal is emitted after rows have been inserted into the
926
model. The new items are those between \a start and \a end
927
inclusive, under the given \a parent item.
933
\fn void QAbstractItemModel::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
936
This signal is emitted just before rows are inserted into the
937
model. The new items will be positioned between \a start and \a end
938
inclusive, under the given \a parent item.
944
\fn void QAbstractItemModel::rowsRemoved(const QModelIndex &parent, int start, int end)
947
This signal is emitted after rows have been removed from the
948
model. The removed items are those between \a start and \a end
949
inclusive, under the given \a parent item.
955
\fn void QAbstractItemModel::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
958
This signal is emitted just before rows are removed from the
959
model. The items that will be removed are those between \a start and \a end
960
inclusive, under the given \a parent item.
966
\fn void QAbstractItemModel::columnsInserted(const QModelIndex &parent, int start, int end)
969
This signal is emitted after columns have been inserted into the
970
model. The new items are those between \a start and \a end
971
inclusive, under the given \a parent item.
977
\fn void QAbstractItemModel::columnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
980
This signal is emitted just before columns are inserted into the
981
model. The new items will be positioned between \a start and \a end
982
inclusive, under the given \a parent item.
988
\fn void QAbstractItemModel::columnsRemoved(const QModelIndex &parent, int start, int end)
991
This signal is emitted after columns have been removed from the
992
model. The removed items are those between \a start and \a end
993
inclusive, under the given \a parent item.
999
\fn void QAbstractItemModel::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
1002
This signal is emitted just before columns are removed
1003
from the model. The items to be removed are those between \a start and
1004
\a end inclusive, under the given \a parent item.
1010
Returns true if the model returns a valid QModelIndex for \a row and
1011
\a column with \a parent, otherwise returns false.
1013
bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const
1015
if (row < 0 || column < 0)
1017
return row < rowCount(parent) && column < columnCount(parent);
1022
Returns true if \a parent has any children; otherwise returns false.
1024
\sa parent() index()
1026
bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const
1028
return (rowCount(parent) > 0) && (columnCount(parent) > 0);
1033
Returns a map with values for all predefined roles in the model
1034
for the item at the given \a index.
1036
This must be reimplemented if you want to extend the model with
1039
\sa Qt::ItemDataRole data()
1041
QMap<int, QVariant> QAbstractItemModel::itemData(const QModelIndex &index) const
1043
QMap<int, QVariant> roles;
1044
for (int i = 0; i < Qt::UserRole; ++i) {
1045
QVariant variantData = data(index, i);
1046
if (variantData.type() != QVariant::Invalid)
1047
roles.insert(i, variantData);
1053
Sets the \a role data for the item at \a index to \a value.
1054
Returns true if successful; otherwise returns false.
1056
The base class implementation returns false. This function and
1057
data() must be reimplemented for editable models.
1059
\sa data() itemData()
1061
bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
1070
\fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0
1072
Returns the data stored under the given \a role for the item referred to
1075
\sa Qt::ItemDataRole
1079
For every \c Qt::ItemDataRole in \a roles, sets the role data for the item at
1080
\a index to the associated value in \a roles. Returns true if
1081
successful; otherwise returns false.
1083
\sa setData() data() itemData()
1085
bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
1088
for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
1089
b = b && setData(index, it.value(), it.key());
1094
Returns a list of MIME types that can be used to describe a list of
1099
QStringList QAbstractItemModel::mimeTypes() const
1102
types << "application/x-qabstractitemmodeldatalist";
1107
Returns an object that contains a serialized description of the specified
1108
\a indexes. The format used to describe the items corresponding to the
1109
indexes is obtained from the mimeTypes() function.
1111
If the list of indexes is empty, 0 is returned rather than a serialized
1114
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
1116
if (indexes.count() <= 0)
1118
QMimeData *data = new QMimeData();
1119
QString format = mimeTypes().at(0);
1121
QDataStream stream(&encoded, QIODevice::WriteOnly);
1122
encodeData(indexes, stream);
1123
data->setData(format, encoded);
1128
Handles the \a data supplied by a drag and drop operation that ended with
1129
the given \a action over the row in the model specified by the \a row,
1130
\a column, and the \a parent index.
1132
\sa supportedDropActions()
1134
bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
1135
int row, int column, const QModelIndex &parent)
1137
// check if the action is supported
1138
if (!data || action != Qt::CopyAction)
1140
// check if the format is supported
1141
QString format = mimeTypes().at(0);
1142
if (!data->hasFormat(format))
1144
if (row > rowCount(parent))
1145
row = rowCount(parent);
1147
row = rowCount(parent);
1150
// decode and insert
1151
QByteArray encoded = data->data(format);
1152
QDataStream stream(&encoded, QIODevice::ReadOnly);
1153
return decodeData(row, column, parent, stream);
1157
Returns the drop actions supported by this model.
1161
Qt::DropActions QAbstractItemModel::supportedDropActions() const
1163
return Qt::CopyAction;
1167
Inserts \a count rows into the model before the given \a row.
1168
The items in the new row will be children of the item represented by the
1169
\a parent model index.
1171
If \a row is 0, the rows are prepended to any existing rows in the parent.
1172
If \a row is rowCount(), the rows are appended to any existing rows in the
1174
If \a parent has no children, a single column with \a count rows is inserted.
1176
Returns true if the rows were successfully inserted; otherwise returns
1179
The base class implementation does nothing and returns false.
1181
If you want to be able to insert rows in a subclass, you must reimplement
1182
this function, calling the beginInsertRows() function \e before inserting
1183
new rows into your underlying data store, and call the endInsertRows()
1184
function \e afterwards. Return true to indicate success; otherwise return
1187
bool QAbstractItemModel::insertRows(int, int, const QModelIndex &)
1193
Inserts \a count new columns into the model before the given \a column.
1194
The items in each new column will be children of the item represented by the
1195
\a parent model index.
1197
If \a column is 0, the columns are prepended to any existing columns.
1198
If \a column is columnCount(), the columns are appended to any existing
1200
If \a parent has no children, a single row with \a count columns is inserted.
1202
Returns true if the columns were successfully inserted; otherwise returns
1205
The base class implementation does nothing and returns false.
1207
If you want to be able to insert columns in a subclass, you must reimplement
1208
this function, calling the beginInsertColumns() function \e before inserting
1209
new columns into your underlying data store, and call the endInsertColumns()
1210
function \e afterwards. Return true to indicate success; otherwise return
1213
bool QAbstractItemModel::insertColumns(int, int, const QModelIndex &)
1219
Removes \a count rows starting with the given \a row under parent
1220
\a parent from the model. Returns true if the rows were successfully
1221
removed; otherwise returns false.
1223
The base class implementation does nothing and returns false.
1225
If you want to be able to remove rows in a subclass, you must reimplement
1226
this function, calling the beginRemoveRows() function \e before removing
1227
new rows into your underlying data store, and call the endRemoveRows()
1228
function \e afterwards. Return true to indicate success; otherwise return
1231
\sa removeRow(), removeColumns(), insertColumns()
1233
bool QAbstractItemModel::removeRows(int, int, const QModelIndex &)
1239
Removes \a count columns starting with the given \a column under
1240
parent \a parent from the model. Returns true if the columns were
1241
successfully removed; otherwise returns false.
1243
The base class implementation does nothing and returns false.
1245
If you want to be able to remove columns in a subclass, you must reimplement
1246
this function, calling the beginRemoveColumns() function \e before removing
1247
new columns into your underlying data store, and call the endRemoveColumns()
1248
function \e afterwards. Return true to indicate success; otherwise return
1251
\sa removeColumn(), removeRows(), insertColumns()
1253
bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &)
1259
Fetches any available data for the items with the parent specified by the
1262
Reimplement this if you have incremental data.
1264
The default implementation does nothing.
1268
void QAbstractItemModel::fetchMore(const QModelIndex &)
1274
Returns true if there is more data available for \a parent,
1277
The default implementation always returns false.
1281
bool QAbstractItemModel::canFetchMore(const QModelIndex &) const
1287
Returns the item flags for the given \a index.
1289
The base class implementation returns a combination of flags that
1290
enables the item (\c ItemIsEnabled) and allows it to be
1291
selected (\c ItemIsSelectable).
1295
Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
1298
return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
1302
Sorts the model by \a column in the given \a order.
1304
The base class implementation does nothing.
1306
void QAbstractItemModel::sort(int column, Qt::SortOrder order)
1314
Returns a model index for the buddy of the item represented by \a index.
1315
When the user wants to edit an item, the view will call this function to
1316
check whether another item in the model should be edited instead, and
1317
construct a delegate using the model index returned by the buddy item.
1319
In the default implementation each item is its own buddy.
1321
QModelIndex QAbstractItemModel::buddy(const QModelIndex &index) const
1327
Returns a list of indexes for the items where the data stored under
1328
the given \a role matches the specified \a value. The way the search
1329
is performed is defined by the \a flags given. The list that is
1330
returned may be empty.
1332
The search starts from the \a start index, and continues until the
1333
number of matching data items equals \a hits, the search reaches
1334
the last row, or the search reaches \a start again, depending on
1335
whether \c MatchWrap is specified in \a flags.
1337
QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
1338
const QVariant &value, int hits,
1339
Qt::MatchFlags flags) const
1341
QModelIndexList result;
1342
uint matchType = flags & 0x0F;
1343
bool caseSesitive = flags & Qt::MatchCaseSensitive;
1344
bool recurse = flags & Qt::MatchRecursive;
1345
bool wrap = flags & Qt::MatchWrap;
1346
bool allHits = (hits == -1);
1347
QString text; // only convert to a string if it is needed
1348
QModelIndex p = parent(start);
1349
int from = start.row();
1350
int to = rowCount(p);
1351
// iterates twice if wrapping
1352
for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
1353
for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) {
1354
QModelIndex idx = index(r, start.column(), p);
1355
QVariant v = data(idx, role);
1356
// QVariant based matching
1357
if (matchType == Qt::MatchExactly) {
1360
} else { // QString based matching
1361
if (text.isEmpty()) { // lazy conversion
1362
text = value.toString();
1364
text = text.toLower();
1366
QString t = v.toString();
1369
switch (matchType) {
1370
case Qt::MatchRegExp:
1371
if (QRegExp(text).exactMatch(t))
1374
case Qt::MatchWildcard:
1375
if (QRegExp(text, Qt::CaseSensitive, QRegExp::Wildcard).exactMatch(t))
1378
case Qt::MatchStartsWith:
1379
if (t.startsWith(text))
1382
case Qt::MatchEndsWith:
1383
if (t.endsWith(text))
1386
case Qt::MatchContains:
1388
if (t.contains(text))
1392
if (recurse && hasChildren(idx)) { // search the hierarchy
1393
result += match(index(0, idx.column(), idx), role,
1394
(text.isEmpty() ? value : text),
1395
(allHits ? -1 : hits - result.count()), flags);
1398
// prepare for the next iteration
1406
Returns the row and column span of the item represented by \a index.
1409
QSize QAbstractItemModel::span(const QModelIndex &) const
1415
Called to let the model know that it should submit whatever it has cached
1416
to the permanent storage. Typically used for row editing.
1418
Returns false on error, otherwise true.
1421
bool QAbstractItemModel::submit()
1427
Called to let the model know that it should discart whatever it has cached.
1428
Typically used for row editing.
1431
void QAbstractItemModel::revert()
1437
Returns the data for the given \a role and \a section in the header
1438
with the specified \a orientation.
1440
\sa Qt::ItemDataRole
1443
QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const
1445
if (role == Qt::DisplayRole) {
1446
if ((orientation == Qt::Horizontal && section < columnCount())
1447
|| (orientation == Qt::Vertical && section < rowCount()))
1448
return QString::number(section + 1);
1449
} else if (role == Qt::TextAlignmentRole)
1450
return Qt::AlignVCenter;
1455
Sets the \a role for the header \a section to \a value.
1456
The \a orientation gives the orientation of the header.
1461
bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation,
1462
const QVariant &value, int role)
1465
Q_UNUSED(orientation);
1472
\fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const
1474
Creates a model index for the given \a row and \a column with the internal pointer \a ptr.
1476
This function provides a consistent interface that model subclasses must
1477
use to create model indexes.
1481
\fn QModelIndex QAbstractItemModel::createIndex(int row, int column, int id) const
1483
Creates a model index for the given \a row and \a column with the internal identifier \a id.
1485
This function provides a consistent interface that model subclasses must
1486
use to create model indexes.
1492
void QAbstractItemModel::encodeData(const QModelIndexList &indexes, QDataStream &stream) const
1494
QModelIndexList::ConstIterator it = indexes.begin();
1495
for (; it != indexes.end(); ++it) {
1496
stream << (*it).row();
1497
stream << (*it).column();
1498
stream << itemData(*it);
1505
bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent,
1506
QDataStream &stream)
1512
QVector<QMap<int, QVariant> > data;
1514
QVector<int> columns;
1515
// get data, positions and dimensions of the dragged data (the positions are from the source table)
1516
while (!stream.atEnd()) {
1518
QMap<int, QVariant> d;
1529
left = qMin(c, left);
1530
bottom = qMax(r, bottom);
1531
right = qMax(c, right);
1534
// insert the dragged items into the table, use a bit array to avoid overwriting items,
1535
// since items from different tables can have the same row and column
1536
int dragRowCount = bottom - top + 1 ;
1537
int dragColumnCount = right - left + 1;
1538
QBitArray isWrittenTo(dragRowCount * dragColumnCount);
1540
// make space in the table for the dropped data
1541
int colCount = columnCount(parent);
1542
if (colCount == 0) {
1543
insertColumns(colCount, dragColumnCount - colCount, parent);
1544
colCount = columnCount(parent);
1546
insertRows(row, dragRowCount, parent);
1549
column = qMax(0, column);
1551
// set the data in the table
1552
for (int j = 0; j < data.size(); ++j) {
1553
int relativeRow = rows.at(j) - top;
1554
int relativeColumn = columns.at(j) - left;
1555
int destinationRow = relativeRow + row;
1556
int destinationColumn = relativeColumn + column;
1557
int flat = (relativeRow * dragColumnCount) + relativeColumn;
1558
// if the item was already written to, or we just can't fit it in the table, create a new row
1559
if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
1560
destinationColumn = qBound(column, destinationColumn, colCount - 1);
1561
destinationRow = row + dragRowCount;
1562
insertRows(row + dragRowCount, 1, parent);
1563
flat = (dragRowCount * dragColumnCount) + relativeColumn;
1564
isWrittenTo.resize(++dragRowCount * dragColumnCount);
1566
if (!isWrittenTo.testBit(flat)) {
1567
QModelIndex idx = index(destinationRow, destinationColumn, parent);
1568
setItemData(idx, data.at(j));
1569
isWrittenTo.setBit(flat);
1577
Begins a row insertion operation.
1579
When reimplementing insertRows() in a subclass, you must call this
1580
function \e before inserting data into the model's underlying data
1583
The \a parent index corresponds to the parent into which the new
1584
rows are inserted; \a first and \a last are the row numbers of the new
1585
rows to be inserted.
1589
void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
1591
Q_D(QAbstractItemModel);
1592
d->change.parent = parent;
1593
d->change.first = first;
1594
d->change.last = last;
1595
emit rowsAboutToBeInserted(parent, first, last);
1596
d->rowsAboutToBeInserted(parent, first, last);
1600
Ends a row insertion operation.
1602
When reimplementing insertRows() in a subclass, you must call this
1603
function \e after inserting data into the model's underlying data
1606
\sa beginInsertRows()
1608
void QAbstractItemModel::endInsertRows()
1610
Q_D(QAbstractItemModel);
1611
d->rowsInserted(d->change.parent, d->change.first, d->change.last);
1612
emit rowsInserted(d->change.parent, d->change.first, d->change.last);
1616
Begins a row removal operation.
1618
When reimplementing removeRows() in a subclass, you must call this
1619
function \e before removing data from the model's underlying data
1622
The \a parent index corresponds to the parent from which the new
1623
rows are removed; \a first and \a last are the row numbers of the
1628
void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)
1630
Q_D(QAbstractItemModel);
1631
d->change.parent = parent;
1632
d->change.first = first;
1633
d->change.last = last;
1634
emit rowsAboutToBeRemoved(parent, first, last);
1635
d->rowsAboutToBeRemoved(parent, first, last);
1639
Ends a row removal operation.
1641
When reimplementing removeRows() in a subclass, you must call this
1642
function \e after removing data from the model's underlying data
1645
\sa beginRemoveRows()
1647
void QAbstractItemModel::endRemoveRows()
1649
Q_D(QAbstractItemModel);
1650
d->rowsRemoved(d->change.parent, d->change.first, d->change.last);
1651
emit rowsRemoved(d->change.parent, d->change.first, d->change.last);
1655
Begins a column insertion operation.
1657
When reimplementing insertColumns() in a subclass, you must call this
1658
function \e before inserting data into the model's underlying data
1661
The \a parent index corresponds to the parent into which the new
1662
columns are inserted; \a first and \a last are the column numbers of
1663
the new columns to be inserted.
1665
\sa endInsertColumns()
1667
void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last)
1669
Q_D(QAbstractItemModel);
1670
d->change.parent = parent;
1671
d->change.first = first;
1672
d->change.last = last;
1673
emit columnsAboutToBeInserted(parent, first, last);
1674
d->columnsAboutToBeInserted(parent, first, last);
1678
Ends a column insertion operation.
1680
When reimplementing insertColumns() in a subclass, you must call this
1681
function \e after inserting data into the model's underlying data
1684
\sa beginInsertColumns()
1686
void QAbstractItemModel::endInsertColumns()
1688
Q_D(QAbstractItemModel);
1689
d->columnsInserted(d->change.parent, d->change.first, d->change.last);
1690
emit columnsInserted(d->change.parent, d->change.first, d->change.last);
1694
Begins a column removal operation.
1696
When reimplementing removeColumns() in a subclass, you must call this
1697
function \e before removing data from the model's underlying data
1700
The \a parent index corresponds to the parent from which the new
1701
columns are removed; \a first and \a last are the column numbers of the
1702
columns to be removed.
1704
\sa endRemoveColumns()
1706
void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first, int last)
1708
Q_D(QAbstractItemModel);
1709
d->change.parent = parent;
1710
d->change.first = first;
1711
d->change.last = last;
1712
emit columnsAboutToBeRemoved(parent, first, last);
1713
d->columnsAboutToBeRemoved(parent, first, last);
1717
Ends a column removal operation.
1719
When reimplementing removeColumns() in a subclass, you must call this
1720
function \e after removing data from the model's underlying data
1723
\sa beginRemoveColumns()
1725
void QAbstractItemModel::endRemoveColumns()
1727
Q_D(QAbstractItemModel);
1728
d->columnsRemoved(d->change.parent, d->change.first, d->change.last);
1729
emit columnsRemoved(d->change.parent, d->change.first, d->change.last);
1733
Resets the model to its original state in any attached views.
1735
When a model is reset it means that any previous data reported from
1736
the model is now invalid and has to be queried for again.
1738
When a model radically changes its data it can sometimes be easier
1739
to just emit this signal rather than dataChanged().
1741
void QAbstractItemModel::reset()
1743
Q_D(QAbstractItemModel);
1750
Changes the QPersistentModelIndex that is equal to the given \a from
1751
model index to the given \a to model index.
1753
If no persistent model index equal to the given \a from model index was
1754
found, nothing is changed.
1756
void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QModelIndex &to)
1758
// ### optimize (use QMap ?)
1759
Q_D(QAbstractItemModel);
1760
QList<QPersistentModelIndexData*> persistentIndexes = d->persistent.indexes;
1761
for (int i = 0; i < persistentIndexes.count(); ++i)
1762
if (persistentIndexes.at(i)->index == from)
1763
persistentIndexes.at(i)->index = to; // there could be more pointing to the same index
1767
\class QAbstractTableModel
1768
\brief The QAbstractTableModel class provides an abstract model that can be
1769
subclassed to create table models.
1773
QAbstractTableModel provides a standard interface for models that represent
1774
their data as a two-dimensional array of items. It is not used directly,
1775
but must be subclassed.
1777
Since the model provides a more specialized interface than
1778
QAbstractItemModel, it is not suitable for use with tree views, although
1779
it can be used to provide data to a QListView. If you need to represent
1780
a simple list of items, and only need a model to contain a single column
1781
of data, subclassing the QAbstractListModel may be more appropriate.
1783
The rowCount() and columnCount() functions return the dimensions of the
1784
table. To retrieve a model index corresponding to an item in the model, use
1785
index() and provide only the row and column numbers.
1787
\section1 Subclassing
1789
When subclassing QAbstractTableModel, you must implement rowCount(),
1790
columnCount(), and data(). Default implementations of the index() and
1791
parent() functions are provided by QAbstractTableModel.
1792
Well behaved models will also implement headerData().
1794
Editable models need to implement setData(), and implement flags() to
1795
return a value containing
1796
\l{Qt::ItemFlags}{Qt::ItemIsEditable}.
1798
Models that provide interfaces to resizable data structures can
1799
provide implementations of insertRows(), removeRows(), insertColumns(),
1800
and removeColumns(). When implementing these functions, it is
1801
important to call the appropriate functions so that all connected views
1802
are aware of any changes:
1805
\o An insertRows() implementation must call beginInsertRows()
1806
\e before inserting new rows into the data structure, and it must
1807
call endInsertRows() \e{immediately afterwards}.
1808
\o An insertColumns() implementation must call beginInsertColumns()
1809
\e before inserting new columns into the data structure, and it must
1810
call endInsertColumns() \e{immediately afterwards}.
1811
\o A removeRows() implementation must call beginRemoveRows()
1812
\e before the rows are removed from the data structure, and it must
1813
call endRemoveRows() \e{immediately afterwards}.
1814
\o A removeColumns() implementation must call beginRemoveColumns()
1815
\e before the columns are removed from the data structure, and it must
1816
call endRemoveColumns() \e{immediately afterwards}.
1819
\sa \link model-view-programming.html Model/View Programming\endlink QAbstractItemModel QAbstractListModel
1824
Constructs an abstract table model for the given \a parent.
1827
QAbstractTableModel::QAbstractTableModel(QObject *parent)
1828
: QAbstractItemModel(parent)
1836
Constructs an abstract table model with \a dd and the given \a parent.
1839
QAbstractTableModel::QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent)
1840
: QAbstractItemModel(dd, parent)
1846
Destroys the abstract table model.
1849
QAbstractTableModel::~QAbstractTableModel()
1855
\fn QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
1857
Returns the index of the data in \a row and \a column with \a parent.
1862
QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const
1864
return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
1868
\fn QModelIndex QAbstractTableModel::parent(const QModelIndex &index) const
1870
Returns the parent of the model item with the given \a index.
1872
\sa index() hasChildren()
1875
QModelIndex QAbstractTableModel::parent(const QModelIndex &) const
1877
return QModelIndex();
1880
bool QAbstractTableModel::hasChildren(const QModelIndex &parent) const
1882
return !parent.isValid();
1886
\class QAbstractListModel
1887
\brief The QAbstractListModel class provides an abstract model that can be
1888
subclassed to create one-dimensional list models.
1892
QAbstractListModel provides a standard interface for models that represent
1893
their data as a simple non-hierarchical sequence of items. It is not used
1894
directly, but must be subclassed.
1896
Since the model provides a more specialized interface than
1897
QAbstractItemModel, it is not suitable for use with tree views; you will
1898
need to subclass QAbstractItemModel if you want to provide a model for
1899
that purpose. If you need to use a number of list models to manage data,
1900
it may be more appropriate to subclass QAbstractTableModel class instead.
1902
Simple models can be created by subclassing this class and implementing
1903
the minimum number of required functions. For example, we could implement
1904
a simple read-only QStringList-based model that provides a list of strings
1905
to a QListView widget. In such a case, we only need to implement the
1906
rowCount() function to return the number of items in the list, and the
1907
data() function to retrieve items from the list.
1909
Since the model represents a one-dimensional structure, the rowCount()
1910
function returns the total number of items in the model. The columnCount()
1911
function is implemented for interoperability with all kinds of views, but
1912
by default informs views that the model contains only one column.
1914
\section1 Subclassing
1916
When subclassing QAbstractListModel, you must provide implementations
1917
of the rowCount() and data() functions. Well behaved models also provide
1918
a headerData() implementation.
1920
For editable list models, you must also provide an implementation of
1921
setData(), implement the flags() function so that it returns a value
1922
containing \l{Qt::ItemFlags}{Qt::ItemIsEditable}.
1924
Note that QAbstractListModel provides a default implementation of
1925
columnCount() that informs views that there is only a single column
1926
of items in this model.
1928
Models that provide interfaces to resizable list-like data structures
1929
can provide implementations of insertRows() and removeRows(). When
1930
implementing these functions, it is important to call the appropriate
1931
functions so that all connected views are aware of any changes:
1934
\o An insertRows() implementation must call beginInsertRows()
1935
\e before inserting new rows into the data structure, and it must
1936
call endInsertRows() \e{immediately afterwards}.
1937
\o A removeRows() implementation must call beginRemoveRows()
1938
\e before the rows are removed from the data structure, and it must
1939
call endRemoveRows() \e{immediately afterwards}.
1942
\sa \link model-view-programming.html Model/View Programming\endlink QAbstractItemView QAbstractTableModel
1947
Constructs an abstract list model with the given \a parent.
1950
QAbstractListModel::QAbstractListModel(QObject *parent)
1951
: QAbstractItemModel(parent)
1959
Constructs an abstract list model with \a dd and the given \a parent.
1962
QAbstractListModel::QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent)
1963
: QAbstractItemModel(dd, parent)
1969
Destroys the abstract list model.
1972
QAbstractListModel::~QAbstractListModel()
1978
\fn QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
1980
Returns the index of the data in \a row and \a column with \a parent.
1985
QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent) const
1987
return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
1991
\fn QModelIndex QAbstractListModel::parent(const QModelIndex &index) const
1993
Returns the parent of the model item with the given \a index.
1995
\sa index() hasChildren()
1998
QModelIndex QAbstractListModel::parent(const QModelIndex &) const
2000
return QModelIndex();
2006
Returns the number of columns in the list with the given \a parent.
2011
int QAbstractListModel::columnCount(const QModelIndex &) const
2016
bool QAbstractListModel::hasChildren(const QModelIndex &parent) const
2018
return !parent.isValid();