1
/****************************************************************************
3
** Copyright (C) 2012 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
****************************************************************************/
42
#include "qstandarditemmodel.h"
44
#ifndef QT_NO_STANDARDITEMMODEL
46
#include <QtCore/qdatetime.h>
47
#include <QtCore/qlist.h>
48
#include <QtCore/qmap.h>
49
#include <QtCore/qpair.h>
50
#include <QtCore/qvariant.h>
51
#include <QtCore/qvector.h>
52
#include <QtCore/qstringlist.h>
53
#include <QtCore/qbitarray.h>
54
#include <QtCore/qmimedata.h>
56
#include <private/qstandarditemmodel_p.h>
61
class QStandardItemModelLessThan
64
inline QStandardItemModelLessThan()
67
inline bool operator()(const QPair<QStandardItem*, int> &l,
68
const QPair<QStandardItem*, int> &r) const
70
return *(l.first) < *(r.first);
74
class QStandardItemModelGreaterThan
77
inline QStandardItemModelGreaterThan()
80
inline bool operator()(const QPair<QStandardItem*, int> &l,
81
const QPair<QStandardItem*, int> &r) const
83
return *(r.first) < *(l.first);
90
QStandardItemPrivate::~QStandardItemPrivate()
92
QVector<QStandardItem*>::const_iterator it;
93
for (it = children.constBegin(); it != children.constEnd(); ++it) {
94
QStandardItem *child = *it;
96
child->d_func()->setModel(0);
101
parent->d_func()->childDeleted(q_func());
107
QPair<int, int> QStandardItemPrivate::position() const
109
if (QStandardItem *par = parent) {
110
int idx = par->d_func()->childIndex(q_func());
112
return QPair<int, int>(-1, -1);
113
return QPair<int, int>(idx / par->columnCount(), idx % par->columnCount());
115
// ### support header items?
116
return QPair<int, int>(-1, -1);
122
void QStandardItemPrivate::setChild(int row, int column, QStandardItem *item,
127
qWarning("QStandardItem::setChild: Can't make an item a child of itself %p",
131
if ((row < 0) || (column < 0))
134
q->setRowCount(row + 1);
135
if (columns <= column)
136
q->setColumnCount(column + 1);
137
int index = childIndex(row, column);
138
Q_ASSERT(index != -1);
139
QStandardItem *oldItem = children.at(index);
143
if (model && emitChanged) {
144
emit model->layoutAboutToBeChanged();
148
if (item->d_func()->parent == 0) {
149
item->d_func()->setParentAndModel(q, model);
151
qWarning("QStandardItem::setChild: Ignoring duplicate insertion of item %p",
157
oldItem->d_func()->setModel(0);
159
children.replace(index, item);
161
if (model && emitChanged)
162
emit model->layoutChanged();
164
if (emitChanged && model)
165
model->d_func()->itemChanged(item);
172
void QStandardItemPrivate::changeFlags(bool enable, Qt::ItemFlags f)
175
Qt::ItemFlags flags = q->flags();
186
void QStandardItemPrivate::childDeleted(QStandardItem *child)
188
int index = childIndex(child);
189
Q_ASSERT(index != -1);
190
children.replace(index, 0);
196
void QStandardItemPrivate::setItemData(const QMap<int, QVariant> &roles)
200
//let's build the vector of new values
201
QVector<QStandardItemData> newValues;
202
QMap<int, QVariant>::const_iterator it;
203
for (it = roles.begin(); it != roles.end(); ++it) {
204
QVariant value = it.value();
205
if (value.isValid()) {
207
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
208
QStandardItemData wid(role,it.value());
209
newValues.append(wid);
213
if (values!=newValues) {
216
model->d_func()->itemChanged(q);
223
const QMap<int, QVariant> QStandardItemPrivate::itemData() const
225
QMap<int, QVariant> result;
226
QVector<QStandardItemData>::const_iterator it;
227
for (it = values.begin(); it != values.end(); ++it)
228
result.insert((*it).role, (*it).value);
235
void QStandardItemPrivate::sortChildren(int column, Qt::SortOrder order)
238
if (column >= columnCount())
241
QVector<QPair<QStandardItem*, int> > sortable;
242
QVector<int> unsortable;
244
sortable.reserve(rowCount());
245
unsortable.reserve(rowCount());
247
for (int row = 0; row < rowCount(); ++row) {
248
QStandardItem *itm = q->child(row, column);
250
sortable.append(QPair<QStandardItem*,int>(itm, row));
252
unsortable.append(row);
255
if (order == Qt::AscendingOrder) {
256
QStandardItemModelLessThan lt;
257
qStableSort(sortable.begin(), sortable.end(), lt);
259
QStandardItemModelGreaterThan gt;
260
qStableSort(sortable.begin(), sortable.end(), gt);
263
QModelIndexList changedPersistentIndexesFrom, changedPersistentIndexesTo;
264
QVector<QStandardItem*> sorted_children(children.count());
265
for (int i = 0; i < rowCount(); ++i) {
266
int r = (i < sortable.count()
267
? sortable.at(i).second
268
: unsortable.at(i - sortable.count()));
269
for (int c = 0; c < columnCount(); ++c) {
270
QStandardItem *itm = q->child(r, c);
271
sorted_children[childIndex(i, c)] = itm;
273
QModelIndex from = model->createIndex(r, c, q);
274
if (model->d_func()->persistent.indexes.contains(from)) {
275
QModelIndex to = model->createIndex(i, c, q);
276
changedPersistentIndexesFrom.append(from);
277
changedPersistentIndexesTo.append(to);
283
children = sorted_children;
286
model->changePersistentIndexList(changedPersistentIndexesFrom, changedPersistentIndexesTo);
289
QVector<QStandardItem*>::iterator it;
290
for (it = children.begin(); it != children.end(); ++it) {
292
(*it)->d_func()->sortChildren(column, order);
298
set the model of this item and all its children
300
void QStandardItemPrivate::setModel(QStandardItemModel *mod)
302
if (children.isEmpty()) {
304
model->d_func()->invalidatePersistentIndex(model->indexFromItem(q_ptr));
307
QStack<QStandardItem*> stack;
309
while (!stack.isEmpty()) {
310
QStandardItem *itm = stack.pop();
311
if (itm->d_func()->model) {
312
itm->d_func()->model->d_func()->invalidatePersistentIndex(itm->d_func()->model->indexFromItem(itm));
314
itm->d_func()->model = mod;
315
const QVector<QStandardItem*> &childList = itm->d_func()->children;
316
for (int i = 0; i < childList.count(); ++i) {
317
QStandardItem *chi = childList.at(i);
328
QStandardItemModelPrivate::QStandardItemModelPrivate()
329
: root(new QStandardItem),
331
sortRole(Qt::DisplayRole)
333
root->setFlags(Qt::ItemIsDropEnabled);
339
QStandardItemModelPrivate::~QStandardItemModelPrivate()
341
delete itemPrototype;
342
qDeleteAll(columnHeaderItems);
343
qDeleteAll(rowHeaderItems);
349
void QStandardItemModelPrivate::init()
351
Q_Q(QStandardItemModel);
352
QObject::connect(q, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
353
q, SLOT(_q_emitItemChanged(QModelIndex,QModelIndex)));
359
void QStandardItemModelPrivate::_q_emitItemChanged(const QModelIndex &topLeft,
360
const QModelIndex &bottomRight)
362
Q_Q(QStandardItemModel);
363
QModelIndex parent = topLeft.parent();
364
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
365
for (int column = topLeft.column(); column <= bottomRight.column(); ++column) {
366
QModelIndex index = q->index(row, column, parent);
367
if (QStandardItem *item = itemFromIndex(index))
368
emit q->itemChanged(item);
376
bool QStandardItemPrivate::insertRows(int row, const QList<QStandardItem*> &items)
379
if ((row < 0) || (row > rowCount()))
381
int count = items.count();
383
model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
384
if (rowCount() == 0) {
385
if (columnCount() == 0)
386
q->setColumnCount(1);
387
children.resize(columnCount() * count);
391
int index = childIndex(row, 0);
393
children.insert(index, columnCount() * count, 0);
395
for (int i = 0; i < items.count(); ++i) {
396
QStandardItem *item = items.at(i);
397
item->d_func()->model = model;
398
item->d_func()->parent = q;
399
int index = childIndex(i + row, 0);
400
children.replace(index, item);
403
model->d_func()->rowsInserted(q, row, count);
407
bool QStandardItemPrivate::insertRows(int row, int count, const QList<QStandardItem*> &items)
410
if ((count < 1) || (row < 0) || (row > rowCount()))
413
model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
414
if (rowCount() == 0) {
415
children.resize(columnCount() * count);
419
int index = childIndex(row, 0);
421
children.insert(index, columnCount() * count, 0);
423
if (!items.isEmpty()) {
424
int index = childIndex(row, 0);
425
int limit = qMin(items.count(), columnCount() * count);
426
for (int i = 0; i < limit; ++i) {
427
QStandardItem *item = items.at(i);
429
if (item->d_func()->parent == 0) {
430
item->d_func()->setParentAndModel(q, model);
432
qWarning("QStandardItem::insertRows: Ignoring duplicate insertion of item %p",
437
children.replace(index, item);
442
model->d_func()->rowsInserted(q, row, count);
449
bool QStandardItemPrivate::insertColumns(int column, int count, const QList<QStandardItem*> &items)
452
if ((count < 1) || (column < 0) || (column > columnCount()))
455
model->d_func()->columnsAboutToBeInserted(q, column, column + count - 1);
456
if (columnCount() == 0) {
457
children.resize(rowCount() * count);
461
int index = childIndex(0, column);
462
for (int row = 0; row < rowCount(); ++row) {
463
children.insert(index, count, 0);
464
index += columnCount();
467
if (!items.isEmpty()) {
468
int limit = qMin(items.count(), rowCount() * count);
469
for (int i = 0; i < limit; ++i) {
470
QStandardItem *item = items.at(i);
472
if (item->d_func()->parent == 0) {
473
item->d_func()->setParentAndModel(q, model);
475
qWarning("QStandardItem::insertColumns: Ignoring duplicate insertion of item %p",
481
int c = column + (i % count);
482
int index = childIndex(r, c);
483
children.replace(index, item);
487
model->d_func()->columnsInserted(q, column, count);
494
void QStandardItemModelPrivate::itemChanged(QStandardItem *item)
496
Q_Q(QStandardItemModel);
497
if (item->d_func()->parent == 0) {
499
int idx = columnHeaderItems.indexOf(item);
501
emit q->headerDataChanged(Qt::Horizontal, idx, idx);
503
idx = rowHeaderItems.indexOf(item);
505
emit q->headerDataChanged(Qt::Vertical, idx, idx);
509
QModelIndex index = q->indexFromItem(item);
510
emit q->dataChanged(index, index);
517
void QStandardItemModelPrivate::rowsAboutToBeInserted(QStandardItem *parent,
520
Q_Q(QStandardItemModel);
521
QModelIndex index = q->indexFromItem(parent);
522
q->beginInsertRows(index, start, end);
528
void QStandardItemModelPrivate::columnsAboutToBeInserted(QStandardItem *parent,
531
Q_Q(QStandardItemModel);
532
QModelIndex index = q->indexFromItem(parent);
533
q->beginInsertColumns(index, start, end);
539
void QStandardItemModelPrivate::rowsAboutToBeRemoved(QStandardItem *parent,
542
Q_Q(QStandardItemModel);
543
QModelIndex index = q->indexFromItem(parent);
544
q->beginRemoveRows(index, start, end);
550
void QStandardItemModelPrivate::columnsAboutToBeRemoved(QStandardItem *parent,
553
Q_Q(QStandardItemModel);
554
QModelIndex index = q->indexFromItem(parent);
555
q->beginRemoveColumns(index, start, end);
561
void QStandardItemModelPrivate::rowsInserted(QStandardItem *parent,
564
Q_Q(QStandardItemModel);
565
if (parent == root.data())
566
rowHeaderItems.insert(row, count, 0);
573
void QStandardItemModelPrivate::columnsInserted(QStandardItem *parent,
574
int column, int count)
576
Q_Q(QStandardItemModel);
577
if (parent == root.data())
578
columnHeaderItems.insert(column, count, 0);
579
q->endInsertColumns();
585
void QStandardItemModelPrivate::rowsRemoved(QStandardItem *parent,
588
Q_Q(QStandardItemModel);
589
if (parent == root.data()) {
590
for (int i = row; i < row + count; ++i) {
591
QStandardItem *oldItem = rowHeaderItems.at(i);
593
oldItem->d_func()->setModel(0);
596
rowHeaderItems.remove(row, count);
604
void QStandardItemModelPrivate::columnsRemoved(QStandardItem *parent,
605
int column, int count)
607
Q_Q(QStandardItemModel);
608
if (parent == root.data()) {
609
for (int i = column; i < column + count; ++i) {
610
QStandardItem *oldItem = columnHeaderItems.at(i);
612
oldItem->d_func()->setModel(0);
615
columnHeaderItems.remove(column, count);
617
q->endRemoveColumns();
622
\brief The QStandardItem class provides an item for use with the
623
QStandardItemModel class.
628
Items usually contain text, icons, or checkboxes.
630
Each item can have its own background brush which is set with the
631
setBackground() function. The current background brush can be found with
632
background(). The text label for each item can be rendered with its own
633
font and brush. These are specified with the setFont() and setForeground()
634
functions, and read with font() and foreground().
636
By default, items are enabled, editable, selectable, checkable, and can be
637
used both as the source of a drag and drop operation and as a drop target.
638
Each item's flags can be changed by calling setFlags(). Checkable items
639
can be checked and unchecked with the setCheckState() function. The
640
corresponding checkState() function indicates whether the item is
643
You can store application-specific data in an item by calling setData().
645
Each item can have a two-dimensional table of child items. This makes it
646
possible to build hierarchies of items. The typical hierarchy is the tree,
647
in which case the child table is a table with a single column (a list).
649
The dimensions of the child table can be set with setRowCount() and
650
setColumnCount(). Items can be positioned in the child table with
651
setChild(). Get a pointer to a child item with child(). New rows and
652
columns of children can also be inserted with insertRow() and
653
insertColumn(), or appended with appendRow() and appendColumn(). When
654
using the append and insert functions, the dimensions of the child table
657
An existing row of children can be removed with removeRow() or takeRow();
658
correspondingly, a column can be removed with removeColumn() or
661
An item's children can be sorted by calling sortChildren().
663
\section1 Subclassing
665
When subclassing QStandardItem to provide custom items, it is possible to
666
define new types for them so that they can be distinguished from the base
667
class. The type() function should be reimplemented to return a new type
668
value equal to or greater than \l UserType.
670
Reimplement data() and setData() if you want to perform custom handling of
671
data queries and/or control how an item's data is represented.
673
Reimplement clone() if you want QStandardItemModel to be able to create
674
instances of your custom item class on demand (see
675
QStandardItemModel::setItemPrototype()).
677
Reimplement read() and write() if you want to control how items are
678
represented in their serialized form.
680
Reimplement \l{operator<()} if you want to control the semantics of item
681
comparison. \l{operator<()} determines the sorted order when sorting items
682
with sortChildren() or with QStandardItemModel::sort().
684
\sa QStandardItemModel, {Item View Convenience Classes}, {Model/View Programming}
688
\enum QStandardItem::ItemType
690
This enum describes the types that are used to describe standard items.
692
\value Type The default type for standard items.
693
\value UserType The minimum value for custom types. Values below UserType are
696
You can define new user types in QStandardItem subclasses to ensure that
697
custom items are treated specially; for example, when items are sorted.
705
QStandardItem::QStandardItem()
706
: d_ptr(new QStandardItemPrivate)
713
Constructs an item with the given \a text.
715
QStandardItem::QStandardItem(const QString &text)
716
: d_ptr(new QStandardItemPrivate)
724
Constructs an item with the given \a icon and \a text.
726
QStandardItem::QStandardItem(const QIcon &icon, const QString &text)
727
: d_ptr(new QStandardItemPrivate)
736
Constructs an item with \a rows rows and \a columns columns of child items.
738
QStandardItem::QStandardItem(int rows, int columns)
739
: d_ptr(new QStandardItemPrivate)
744
setColumnCount(columns);
750
QStandardItem::QStandardItem(QStandardItemPrivate &dd)
758
Constructs a copy of \a other. Note that model() is
761
This function is useful when reimplementing clone().
763
QStandardItem::QStandardItem(const QStandardItem &other)
764
: d_ptr(new QStandardItemPrivate)
772
Assigns \a other's data and flags to this item. Note that
773
type() and model() are not copied.
775
This function is useful when reimplementing clone().
777
QStandardItem &QStandardItem::operator=(const QStandardItem &other)
780
d->values = other.d_func()->values;
786
This causes the item's children to be destructed as well.
788
QStandardItem::~QStandardItem()
793
Returns the item's parent item, or 0 if the item has no parent.
794
\note For toplevel items parent() returns 0. To receive toplevel
795
item's parent use QStandardItemModel::invisibleRootItem() instead.
797
\sa child(), QStandardItemModel::invisibleRootItem()
799
QStandardItem *QStandardItem::parent() const
801
Q_D(const QStandardItem);
802
if (!d->model || (d->model->d_func()->root.data() != d->parent))
808
Sets the item's data for the given \a role to the specified \a value.
810
If you subclass QStandardItem and reimplement this function, your
811
reimplementation should call emitDataChanged() if you do not call
812
the base implementation of setData(). This will ensure that e.g.
813
views using the model are notified of the changes.
815
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
816
as referring to the same data.
818
\sa Qt::ItemDataRole, data(), setFlags()
820
void QStandardItem::setData(const QVariant &value, int role)
823
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
824
QVector<QStandardItemData>::iterator it;
825
for (it = d->values.begin(); it != d->values.end(); ++it) {
826
if ((*it).role == role) {
827
if (value.isValid()) {
828
if ((*it).value.type() == value.type() && (*it).value == value)
835
d->model->d_func()->itemChanged(this);
839
d->values.append(QStandardItemData(role, value));
841
d->model->d_func()->itemChanged(this);
845
Returns the item's data for the given \a role, or an invalid
846
QVariant if there is no data for the role.
848
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
849
as referring to the same data.
851
QVariant QStandardItem::data(int role) const
853
Q_D(const QStandardItem);
854
role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
855
QVector<QStandardItemData>::const_iterator it;
856
for (it = d->values.begin(); it != d->values.end(); ++it) {
857
if ((*it).role == role)
866
Causes the model associated with this item to emit a
867
\l{QAbstractItemModel::dataChanged()}{dataChanged}() signal for this
870
You normally only need to call this function if you have subclassed
871
QStandardItem and reimplemented data() and/or setData().
875
void QStandardItem::emitDataChanged()
879
d->model->d_func()->itemChanged(this);
883
Sets the item flags for the item to \a flags.
885
The item flags determine how the user can interact with the item.
886
This is often used to disable an item.
888
\sa flags(), setData()
890
void QStandardItem::setFlags(Qt::ItemFlags flags)
892
setData((int)flags, Qt::UserRole - 1);
896
Returns the item flags for the item.
898
The item flags determine how the user can interact with the item.
900
By default, items are enabled, editable, selectable, checkable, and can be
901
used both as the source of a drag and drop operation and as a drop target.
905
Qt::ItemFlags QStandardItem::flags() const
907
QVariant v = data(Qt::UserRole - 1);
909
return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable
910
|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
911
return Qt::ItemFlags(v.toInt());
915
\fn QString QStandardItem::text() const
917
Returns the item's text. This is the text that's presented to the user
924
\fn void QStandardItem::setText(const QString &text)
926
Sets the item's text to the \a text specified.
928
\sa text(), setFont(), setForeground()
932
\fn QIcon QStandardItem::icon() const
934
Returns the item's icon.
936
\sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
940
\fn void QStandardItem::setIcon(const QIcon &icon)
942
Sets the item's icon to the \a icon specified.
946
\fn QString QStandardItem::statusTip() const
948
Returns the item's status tip.
950
\sa setStatusTip(), toolTip(), whatsThis()
954
\fn void QStandardItem::setStatusTip(const QString &statusTip)
956
Sets the item's status tip to the string specified by \a statusTip.
958
\sa statusTip(), setToolTip(), setWhatsThis()
962
\fn QString QStandardItem::toolTip() const
964
Returns the item's tooltip.
966
\sa setToolTip(), statusTip(), whatsThis()
970
\fn void QStandardItem::setToolTip(const QString &toolTip)
972
Sets the item's tooltip to the string specified by \a toolTip.
974
\sa toolTip(), setStatusTip(), setWhatsThis()
978
\fn QString QStandardItem::whatsThis() const
980
Returns the item's "What's This?" help.
982
\sa setWhatsThis(), toolTip(), statusTip()
986
\fn void QStandardItem::setWhatsThis(const QString &whatsThis)
988
Sets the item's "What's This?" help to the string specified by \a whatsThis.
990
\sa whatsThis(), setStatusTip(), setToolTip()
994
\fn QFont QStandardItem::font() const
996
Returns the font used to render the item's text.
1002
\fn void QStandardItem::setFont(const QFont &font)
1004
Sets the font used to display the item's text to the given \a font.
1006
\sa font(), setText(), setForeground()
1010
\fn QBrush QStandardItem::background() const
1012
Returns the brush used to render the item's background.
1014
\sa foreground(), setBackground()
1018
\fn void QStandardItem::setBackground(const QBrush &brush)
1020
Sets the item's background brush to the specified \a brush.
1022
\sa background(), setForeground()
1026
\fn QBrush QStandardItem::foreground() const
1028
Returns the brush used to render the item's foreground (e.g. text).
1030
\sa setForeground(), background()
1034
\fn void QStandardItem::setForeground(const QBrush &brush)
1036
Sets the brush used to display the item's foreground (e.g. text) to the
1039
\sa foreground(), setBackground(), setFont()
1043
\fn int QStandardItem::textAlignment() const
1045
Returns the text alignment for the item's text.
1049
\fn void QStandardItem::setTextAlignment(Qt::Alignment alignment)
1051
Sets the text alignment for the item's text to the \a alignment
1058
\fn QSize QStandardItem::sizeHint() const
1060
Returns the size hint set for the item, or an invalid QSize if no
1061
size hint has been set.
1063
If no size hint has been set, the item delegate will compute the
1064
size hint based on the item data.
1070
\fn void QStandardItem::setSizeHint(const QSize &size)
1072
Sets the size hint for the item to be \a size.
1073
If no size hint is set, the item delegate will compute the
1074
size hint based on the item data.
1080
\fn Qt::CheckState QStandardItem::checkState() const
1082
Returns the checked state of the item.
1084
\sa setCheckState(), isCheckable()
1088
\fn void QStandardItem::setCheckState(Qt::CheckState state)
1090
Sets the check state of the item to be \a state.
1092
\sa checkState(), setCheckable()
1096
\fn QString QStandardItem::accessibleText() const
1098
Returns the item's accessible text.
1100
The accessible text is used by assistive technologies (i.e. for users who
1101
cannot use conventional means of interaction).
1103
\sa setAccessibleText(), accessibleDescription()
1107
\fn void QStandardItem::setAccessibleText(const QString &accessibleText)
1109
Sets the item's accessible text to the string specified by \a accessibleText.
1111
The accessible text is used by assistive technologies (i.e. for users who
1112
cannot use conventional means of interaction).
1114
\sa accessibleText(), setAccessibleDescription()
1118
\fn QString QStandardItem::accessibleDescription() const
1120
Returns the item's accessible description.
1122
The accessible description is used by assistive technologies (i.e. for
1123
users who cannot use conventional means of interaction).
1125
\sa setAccessibleDescription(), accessibleText()
1129
\fn void QStandardItem::setAccessibleDescription(const QString &accessibleDescription)
1131
Sets the item's accessible description to the string specified by \a
1132
accessibleDescription.
1134
The accessible description is used by assistive technologies (i.e. for
1135
users who cannot use conventional means of interaction).
1137
\sa accessibleDescription(), setAccessibleText()
1141
Sets whether the item is enabled. If \a enabled is true, the item is enabled,
1142
meaning that the user can interact with the item; if \a enabled is false, the
1143
user cannot interact with the item.
1145
This flag takes precedence over the other item flags; e.g. if an item is not
1146
enabled, it cannot be selected by the user, even if the Qt::ItemIsSelectable
1149
\sa isEnabled(), Qt::ItemIsEnabled, setFlags()
1151
void QStandardItem::setEnabled(bool enabled)
1154
d->changeFlags(enabled, Qt::ItemIsEnabled);
1158
\fn bool QStandardItem::isEnabled() const
1160
Returns whether the item is enabled.
1162
When an item is enabled, the user can interact with it. The possible
1163
types of interaction are specified by the other item flags, such as
1164
isEditable() and isSelectable().
1166
The default value is true.
1168
\sa setEnabled(), flags()
1172
Sets whether the item is editable. If \a editable is true, the item can be
1173
edited by the user; otherwise, the user cannot edit the item.
1175
How the user can edit items in a view is determined by the view's edit
1176
triggers; see QAbstractItemView::editTriggers.
1178
\sa isEditable(), setFlags()
1180
void QStandardItem::setEditable(bool editable)
1183
d->changeFlags(editable, Qt::ItemIsEditable);
1187
\fn bool QStandardItem::isEditable() const
1189
Returns whether the item can be edited by the user.
1191
When an item is editable (and enabled), the user can edit the item by
1192
invoking one of the view's edit triggers; see
1193
QAbstractItemView::editTriggers.
1195
The default value is true.
1197
\sa setEditable(), flags()
1201
Sets whether the item is selectable. If \a selectable is true, the item
1202
can be selected by the user; otherwise, the user cannot select the item.
1204
You can control the selection behavior and mode by manipulating their
1205
view properties; see QAbstractItemView::selectionMode and
1206
QAbstractItemView::selectionBehavior.
1208
\sa isSelectable(), setFlags()
1210
void QStandardItem::setSelectable(bool selectable)
1213
d->changeFlags(selectable, Qt::ItemIsSelectable);
1217
\fn bool QStandardItem::isSelectable() const
1219
Returns whether the item is selectable by the user.
1221
The default value is true.
1223
\sa setSelectable(), flags()
1227
Sets whether the item is user-checkable. If \a checkable is true, the
1228
item can be checked by the user; otherwise, the user cannot check
1231
The item delegate will render a checkable item with a check box next to the
1234
\sa isCheckable(), setCheckState(), setTristate()
1236
void QStandardItem::setCheckable(bool checkable)
1239
if (checkable && !isCheckable()) {
1240
// make sure there's data for the checkstate role
1241
if (!data(Qt::CheckStateRole).isValid())
1242
setData(Qt::Unchecked, Qt::CheckStateRole);
1244
d->changeFlags(checkable, Qt::ItemIsUserCheckable);
1248
\fn bool QStandardItem::isCheckable() const
1250
Returns whether the item is user-checkable.
1252
The default value is false.
1254
\sa setCheckable(), checkState(), isTristate()
1258
Sets whether the item is tristate. If \a tristate is true, the
1259
item is checkable with three separate states; otherwise, the item
1260
is checkable with two states. (Note that this also requires that
1261
the item is checkable; see isCheckable().)
1263
\sa isTristate(), setCheckable(), setCheckState()
1265
void QStandardItem::setTristate(bool tristate)
1268
d->changeFlags(tristate, Qt::ItemIsTristate);
1272
\fn bool QStandardItem::isTristate() const
1274
Returns whether the item is tristate; that is, if it's checkable with three
1277
The default value is false.
1279
\sa setTristate(), isCheckable(), checkState()
1282
#ifndef QT_NO_DRAGANDDROP
1285
Sets whether the item is drag enabled. If \a dragEnabled is true, the item
1286
can be dragged by the user; otherwise, the user cannot drag the item.
1288
Note that you also need to ensure that item dragging is enabled in the view;
1289
see QAbstractItemView::dragEnabled.
1291
\sa isDragEnabled(), setDropEnabled(), setFlags()
1293
void QStandardItem::setDragEnabled(bool dragEnabled)
1296
d->changeFlags(dragEnabled, Qt::ItemIsDragEnabled);
1300
\fn bool QStandardItem::isDragEnabled() const
1302
Returns whether the item is drag enabled. An item that is drag enabled can
1303
be dragged by the user.
1305
The default value is true.
1307
Note that item dragging must be enabled in the view for dragging to work;
1308
see QAbstractItemView::dragEnabled.
1310
\sa setDragEnabled(), isDropEnabled(), flags()
1314
Sets whether the item is drop enabled. If \a dropEnabled is true, the item
1315
can be used as a drop target; otherwise, it cannot.
1317
Note that you also need to ensure that drops are enabled in the view; see
1318
QWidget::acceptDrops(); and that the model supports the desired drop actions;
1319
see QAbstractItemModel::supportedDropActions().
1321
\sa isDropEnabled(), setDragEnabled(), setFlags()
1323
void QStandardItem::setDropEnabled(bool dropEnabled)
1326
d->changeFlags(dropEnabled, Qt::ItemIsDropEnabled);
1330
\fn bool QStandardItem::isDropEnabled() const
1332
Returns whether the item is drop enabled. When an item is drop enabled, it
1333
can be used as a drop target.
1335
The default value is true.
1337
\sa setDropEnabled(), isDragEnabled(), flags()
1340
#endif // QT_NO_DRAGANDDROP
1343
Returns the row where the item is located in its parent's child table, or
1344
-1 if the item has no parent.
1346
\sa column(), parent()
1348
int QStandardItem::row() const
1350
Q_D(const QStandardItem);
1351
QPair<int, int> pos = d->position();
1356
Returns the column where the item is located in its parent's child table,
1357
or -1 if the item has no parent.
1361
int QStandardItem::column() const
1363
Q_D(const QStandardItem);
1364
QPair<int, int> pos = d->position();
1369
Returns the QModelIndex associated with this item.
1371
When you need to invoke item functionality in a QModelIndex-based API (e.g.
1372
QAbstractItemView), you can call this function to obtain an index that
1373
corresponds to the item's location in the model.
1375
If the item is not associated with a model, an invalid QModelIndex is
1378
\sa model(), QStandardItemModel::itemFromIndex()
1380
QModelIndex QStandardItem::index() const
1382
Q_D(const QStandardItem);
1383
return d->model ? d->model->indexFromItem(this) : QModelIndex();
1387
Returns the QStandardItemModel that this item belongs to.
1389
If the item is not a child of another item that belongs to the model, this
1394
QStandardItemModel *QStandardItem::model() const
1396
Q_D(const QStandardItem);
1401
Sets the number of child item rows to \a rows. If this is less than
1402
rowCount(), the data in the unwanted rows is discarded.
1404
\sa rowCount(), setColumnCount()
1406
void QStandardItem::setRowCount(int rows)
1408
int rc = rowCount();
1412
insertRows(qMax(rc, 0), rows - rc);
1414
removeRows(qMax(rows, 0), rc - rows);
1418
Returns the number of child item rows that the item has.
1420
\sa setRowCount(), columnCount()
1422
int QStandardItem::rowCount() const
1424
Q_D(const QStandardItem);
1425
return d->rowCount();
1429
Sets the number of child item columns to \a columns. If this is less than
1430
columnCount(), the data in the unwanted columns is discarded.
1432
\sa columnCount(), setRowCount()
1434
void QStandardItem::setColumnCount(int columns)
1436
int cc = columnCount();
1440
insertColumns(qMax(cc, 0), columns - cc);
1442
removeColumns(qMax(columns, 0), cc - columns);
1446
Returns the number of child item columns that the item has.
1448
\sa setColumnCount(), rowCount()
1450
int QStandardItem::columnCount() const
1452
Q_D(const QStandardItem);
1453
return d->columnCount();
1457
Inserts a row at \a row containing \a items. If necessary, the column
1458
count is increased to the size of \a items.
1460
\sa insertRows(), insertColumn()
1462
void QStandardItem::insertRow(int row, const QList<QStandardItem*> &items)
1467
if (columnCount() < items.count())
1468
setColumnCount(items.count());
1469
d->insertRows(row, 1, items);
1473
Inserts \a items at \a row. The column count wont be changed.
1475
\sa insertRow(), insertColumn()
1477
void QStandardItem::insertRows(int row, const QList<QStandardItem*> &items)
1482
d->insertRows(row, items);
1486
Inserts a column at \a column containing \a items. If necessary,
1487
the row count is increased to the size of \a items.
1489
\sa insertColumns(), insertRow()
1491
void QStandardItem::insertColumn(int column, const QList<QStandardItem*> &items)
1496
if (rowCount() < items.count())
1497
setRowCount(items.count());
1498
d->insertColumns(column, 1, items);
1502
Inserts \a count rows of child items at row \a row.
1504
\sa insertRow(), insertColumns()
1506
void QStandardItem::insertRows(int row, int count)
1509
if (rowCount() < row) {
1510
count += row - rowCount();
1513
d->insertRows(row, count, QList<QStandardItem*>());
1517
Inserts \a count columns of child items at column \a column.
1519
\sa insertColumn(), insertRows()
1521
void QStandardItem::insertColumns(int column, int count)
1524
if (columnCount() < column) {
1525
count += column - columnCount();
1526
column = columnCount();
1528
d->insertColumns(column, count, QList<QStandardItem*>());
1532
\fn void QStandardItem::appendRow(const QList<QStandardItem*> &items)
1534
Appends a row containing \a items. If necessary, the column count is
1535
increased to the size of \a items.
1541
\fn void QStandardItem::appendRows(const QList<QStandardItem*> &items)
1543
Appends rows containing \a items. The column count will not change.
1549
\fn void QStandardItem::appendColumn(const QList<QStandardItem*> &items)
1551
Appends a column containing \a items. If necessary, the row count is
1552
increased to the size of \a items.
1558
\fn bool QStandardItemModel::insertRow(int row, const QModelIndex &parent)
1560
Inserts a single row before the given \a row in the child items of the
1561
\a parent specified. Returns true if the row is inserted; otherwise
1564
\sa insertRows(), insertColumn(), removeRow()
1568
\fn bool QStandardItemModel::insertColumn(int column, const QModelIndex &parent)
1570
Inserts a single column before the given \a column in the child items of
1571
the \a parent specified. Returns true if the column is inserted; otherwise
1574
\sa insertColumns(), insertRow(), removeColumn()
1578
\fn QStandardItem::insertRow(int row, QStandardItem *item)
1581
Inserts a row at \a row containing \a item.
1583
When building a list or a tree that has only one column, this function
1584
provides a convenient way to insert a single new item.
1588
\fn QStandardItem::appendRow(QStandardItem *item)
1591
Appends a row containing \a item.
1593
When building a list or a tree that has only one column, this function
1594
provides a convenient way to append a single new item.
1598
Removes the given \a row. The items that were in the row are deleted.
1600
\sa takeRow(), removeRows(), removeColumn()
1602
void QStandardItem::removeRow(int row)
1608
Removes the given \a column. The items that were in the
1611
\sa takeColumn(), removeColumns(), removeRow()
1613
void QStandardItem::removeColumn(int column)
1615
removeColumns(column, 1);
1619
Removes \a count rows at row \a row. The items that were in those rows are
1622
\sa removeRow(), removeColumn()
1624
void QStandardItem::removeRows(int row, int count)
1627
if ((count < 1) || (row < 0) || ((row + count) > rowCount()))
1630
d->model->d_func()->rowsAboutToBeRemoved(this, row, row + count - 1);
1631
int i = d->childIndex(row, 0);
1632
int n = count * d->columnCount();
1633
for (int j = i; j < n+i; ++j) {
1634
QStandardItem *oldItem = d->children.at(j);
1636
oldItem->d_func()->setModel(0);
1639
d->children.remove(qMax(i, 0), n);
1642
d->model->d_func()->rowsRemoved(this, row, count);
1646
Removes \a count columns at column \a column. The items that were in those
1647
columns are deleted.
1649
\sa removeColumn(), removeRows()
1651
void QStandardItem::removeColumns(int column, int count)
1654
if ((count < 1) || (column < 0) || ((column + count) > columnCount()))
1657
d->model->d_func()->columnsAboutToBeRemoved(this, column, column + count - 1);
1658
for (int row = d->rowCount() - 1; row >= 0; --row) {
1659
int i = d->childIndex(row, column);
1660
for (int j=i; j<i+count; ++j) {
1661
QStandardItem *oldItem = d->children.at(j);
1663
oldItem->d_func()->setModel(0);
1666
d->children.remove(i, count);
1668
d->columns -= count;
1670
d->model->d_func()->columnsRemoved(this, column, count);
1674
Returns true if this item has any children; otherwise returns false.
1676
\sa rowCount(), columnCount(), child()
1678
bool QStandardItem::hasChildren() const
1680
return (rowCount() > 0) && (columnCount() > 0);
1684
Sets the child item at (\a row, \a column) to \a item. This item (the parent
1685
item) takes ownership of \a item. If necessary, the row count and column
1686
count are increased to fit the item.
1690
void QStandardItem::setChild(int row, int column, QStandardItem *item)
1693
d->setChild(row, column, item, true);
1697
\fn QStandardItem::setChild(int row, QStandardItem *item)
1700
Sets the child at \a row to \a item.
1704
Returns the child item at (\a row, \a column) if one has been set; otherwise
1707
\sa setChild(), takeChild(), parent()
1709
QStandardItem *QStandardItem::child(int row, int column) const
1711
Q_D(const QStandardItem);
1712
int index = d->childIndex(row, column);
1715
return d->children.at(index);
1719
Removes the child item at (\a row, \a column) without deleting it, and returns
1720
a pointer to the item. If there was no child at the given location, then
1721
this function returns 0.
1723
Note that this function, unlike takeRow() and takeColumn(), does not affect
1724
the dimensions of the child table.
1726
\sa child(), takeRow(), takeColumn()
1728
QStandardItem *QStandardItem::takeChild(int row, int column)
1731
QStandardItem *item = 0;
1732
int index = d->childIndex(row, column);
1734
item = d->children.at(index);
1736
item->d_func()->setParentAndModel(0, 0);
1737
d->children.replace(index, 0);
1743
Removes \a row without deleting the row items, and returns a list of
1744
pointers to the removed items. For items in the row that have not been
1745
set, the corresponding pointers in the list will be 0.
1747
\sa removeRow(), insertRow(), takeColumn()
1749
QList<QStandardItem*> QStandardItem::takeRow(int row)
1752
if ((row < 0) || (row >= rowCount()))
1753
return QList<QStandardItem*>();
1755
d->model->d_func()->rowsAboutToBeRemoved(this, row, row);
1756
QList<QStandardItem*> items;
1757
int index = d->childIndex(row, 0); // Will return -1 if there are no columns
1759
int col_count = d->columnCount();
1760
for (int column = 0; column < col_count; ++column) {
1761
QStandardItem *ch = d->children.at(index + column);
1763
ch->d_func()->setParentAndModel(0, 0);
1766
d->children.remove(index, col_count);
1770
d->model->d_func()->rowsRemoved(this, row, 1);
1775
Removes \a column without deleting the column items, and returns a list of
1776
pointers to the removed items. For items in the column that have not been
1777
set, the corresponding pointers in the list will be 0.
1779
\sa removeColumn(), insertColumn(), takeRow()
1781
QList<QStandardItem*> QStandardItem::takeColumn(int column)
1784
if ((column < 0) || (column >= columnCount()))
1785
return QList<QStandardItem*>();
1787
d->model->d_func()->columnsAboutToBeRemoved(this, column, column);
1788
QList<QStandardItem*> items;
1790
for (int row = d->rowCount() - 1; row >= 0; --row) {
1791
int index = d->childIndex(row, column);
1792
QStandardItem *ch = d->children.at(index);
1794
ch->d_func()->setParentAndModel(0, 0);
1795
d->children.remove(index);
1800
d->model->d_func()->columnsRemoved(this, column, 1);
1805
Returns true if this item is less than \a other; otherwise returns false.
1807
The default implementation uses the data for the item's sort role (see
1808
QStandardItemModel::sortRole) to perform the comparison if the item
1809
belongs to a model; otherwise, the data for the item's Qt::DisplayRole
1810
(text()) is used to perform the comparison.
1812
sortChildren() and QStandardItemModel::sort() use this function when
1813
sorting items. If you want custom sorting, you can subclass QStandardItem
1814
and reimplement this function.
1816
bool QStandardItem::operator<(const QStandardItem &other) const
1818
const int role = model() ? model()->sortRole() : Qt::DisplayRole;
1819
const QVariant l = data(role), r = other.data(role);
1820
// this code is copied from QSortFilterProxyModel::lessThan()
1821
switch (l.userType()) {
1822
case QVariant::Invalid:
1823
return (r.type() == QVariant::Invalid);
1825
return l.toInt() < r.toInt();
1826
case QVariant::UInt:
1827
return l.toUInt() < r.toUInt();
1828
case QVariant::LongLong:
1829
return l.toLongLong() < r.toLongLong();
1830
case QVariant::ULongLong:
1831
return l.toULongLong() < r.toULongLong();
1832
case QMetaType::Float:
1833
return l.toFloat() < r.toFloat();
1834
case QVariant::Double:
1835
return l.toDouble() < r.toDouble();
1836
case QVariant::Char:
1837
return l.toChar() < r.toChar();
1838
case QVariant::Date:
1839
return l.toDate() < r.toDate();
1840
case QVariant::Time:
1841
return l.toTime() < r.toTime();
1842
case QVariant::DateTime:
1843
return l.toDateTime() < r.toDateTime();
1844
case QVariant::String:
1846
return l.toString().compare(r.toString()) < 0;
1851
Sorts the children of the item using the given \a order, by the values in
1852
the given \a column.
1854
\note This function is recursive, therefore it sorts the children of the
1855
item, its grandchildren, etc.
1859
void QStandardItem::sortChildren(int column, Qt::SortOrder order)
1862
if ((column < 0) || (rowCount() == 0))
1865
QList<QPersistentModelIndex> parents;
1868
emit d->model->layoutAboutToBeChanged(parents, QAbstractItemModel::VerticalSortHint);
1870
d->sortChildren(column, order);
1872
emit d->model->layoutChanged(parents, QAbstractItemModel::VerticalSortHint);
1876
Returns a copy of this item. The item's children are not copied.
1878
When subclassing QStandardItem, you can reimplement this function
1879
to provide QStandardItemModel with a factory that it can use to
1880
create new items on demand.
1882
\sa QStandardItemModel::setItemPrototype(), operator=()
1884
QStandardItem *QStandardItem::clone() const
1886
return new QStandardItem(*this);
1890
Returns the type of this item. The type is used to distinguish custom
1891
items from the base class. When subclassing QStandardItem, you should
1892
reimplement this function and return a new value greater than or equal
1895
\sa QStandardItem::Type
1897
int QStandardItem::type() const
1902
#ifndef QT_NO_DATASTREAM
1905
Reads the item from stream \a in. Only the data and flags of the item are
1906
read, not the child items.
1910
void QStandardItem::read(QDataStream &in)
1916
setFlags(Qt::ItemFlags(flags));
1920
Writes the item to stream \a out. Only the data and flags of the item
1921
are written, not the child items.
1925
void QStandardItem::write(QDataStream &out) const
1927
Q_D(const QStandardItem);
1933
\relates QStandardItem
1936
Reads a QStandardItem from stream \a in into \a item.
1938
This operator uses QStandardItem::read().
1940
\sa {Serializing Qt Data Types}
1942
QDataStream &operator>>(QDataStream &in, QStandardItem &item)
1949
\relates QStandardItem
1952
Writes the QStandardItem \a item to stream \a out.
1954
This operator uses QStandardItem::write().
1956
\sa {Serializing Qt Data Types}
1958
QDataStream &operator<<(QDataStream &out, const QStandardItem &item)
1964
#endif // QT_NO_DATASTREAM
1967
\class QStandardItemModel
1968
\brief The QStandardItemModel class provides a generic model for storing custom data.
1972
QStandardItemModel can be used as a repository for standard Qt
1973
data types. It is one of the \l {Model/View Classes} and is part
1974
of Qt's \l {Model/View Programming}{model/view} framework.
1976
QStandardItemModel provides a classic item-based approach to working with
1977
the model. The items in a QStandardItemModel are provided by
1980
QStandardItemModel implements the QAbstractItemModel interface, which
1981
means that the model can be used to provide data in any view that supports
1982
that interface (such as QListView, QTableView and QTreeView, and your own
1983
custom views). For performance and flexibility, you may want to subclass
1984
QAbstractItemModel to provide support for different kinds of data
1985
repositories. For example, the QDirModel provides a model interface to the
1986
underlying file system.
1988
When you want a list or tree, you typically create an empty
1989
QStandardItemModel and use appendRow() to add items to the model, and
1990
item() to access an item. If your model represents a table, you typically
1991
pass the dimensions of the table to the QStandardItemModel constructor and
1992
use setItem() to position items into the table. You can also use setRowCount()
1993
and setColumnCount() to alter the dimensions of the model. To insert items,
1994
use insertRow() or insertColumn(), and to remove items, use removeRow() or
1997
You can set the header labels of your model with setHorizontalHeaderLabels()
1998
and setVerticalHeaderLabels().
2000
You can search for items in the model with findItems(), and sort the model by
2003
Call clear() to remove all items from the model.
2005
An example usage of QStandardItemModel to create a table:
2007
\snippet code/src_gui_itemviews_qstandarditemmodel.cpp 0
2009
An example usage of QStandardItemModel to create a tree:
2011
\snippet code/src_gui_itemviews_qstandarditemmodel.cpp 1
2013
After setting the model on a view, you typically want to react to user
2014
actions, such as an item being clicked. Since a QAbstractItemView provides
2015
QModelIndex-based signals and functions, you need a way to obtain the
2016
QStandardItem that corresponds to a given QModelIndex, and vice
2017
versa. itemFromIndex() and indexFromItem() provide this mapping. Typical
2018
usage of itemFromIndex() includes obtaining the item at the current index
2019
in a view, and obtaining the item that corresponds to an index carried by
2020
a QAbstractItemView signal, such as QAbstractItemView::clicked(). First
2021
you connect the view's signal to a slot in your class:
2023
\snippet code/src_gui_itemviews_qstandarditemmodel.cpp 2
2025
When you receive the signal, you call itemFromIndex() on the given model
2026
index to get a pointer to the item:
2028
\snippet code/src_gui_itemviews_qstandarditemmodel.cpp 3
2030
Conversely, you must obtain the QModelIndex of an item when you want to
2031
invoke a model/view function that takes an index as argument. You can
2032
obtain the index either by using the model's indexFromItem() function, or,
2033
equivalently, by calling QStandardItem::index():
2035
\snippet code/src_gui_itemviews_qstandarditemmodel.cpp 4
2037
You are, of course, not required to use the item-based approach; you could
2038
instead rely entirely on the QAbstractItemModel interface when working with
2039
the model, or use a combination of the two as appropriate.
2041
\sa QStandardItem, {Model/View Programming}, QAbstractItemModel,
2042
{itemviews/simpletreemodel}{Simple Tree Model example},
2043
{Item View Convenience Classes}
2047
\fn void QStandardItemModel::itemChanged(QStandardItem *item)
2050
This signal is emitted whenever the data of \a item has changed.
2054
Constructs a new item model with the given \a parent.
2056
QStandardItemModel::QStandardItemModel(QObject *parent)
2057
: QAbstractItemModel(*new QStandardItemModelPrivate, parent)
2059
Q_D(QStandardItemModel);
2061
d->root->d_func()->setModel(this);
2065
Constructs a new item model that initially has \a rows rows and \a columns
2066
columns, and that has the given \a parent.
2068
QStandardItemModel::QStandardItemModel(int rows, int columns, QObject *parent)
2069
: QAbstractItemModel(*new QStandardItemModelPrivate, parent)
2071
Q_D(QStandardItemModel);
2073
d->root->insertColumns(0, columns);
2074
d->columnHeaderItems.insert(0, columns, 0);
2075
d->root->insertRows(0, rows);
2076
d->rowHeaderItems.insert(0, rows, 0);
2077
d->root->d_func()->setModel(this);
2083
QStandardItemModel::QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent)
2084
: QAbstractItemModel(dd, parent)
2086
Q_D(QStandardItemModel);
2091
Destructs the model. The model destroys all its items.
2093
QStandardItemModel::~QStandardItemModel()
2098
Sets the item role names to \a roleNames.
2100
void QStandardItemModel::setItemRoleNames(const QHash<int,QByteArray> &roleNames)
2102
Q_D(QStandardItemModel);
2103
d->roleNames = roleNames;
2107
Removes all items (including header items) from the model and sets the
2108
number of rows and columns to zero.
2110
\sa removeColumns(), removeRows()
2112
void QStandardItemModel::clear()
2114
Q_D(QStandardItemModel);
2116
d->root.reset(new QStandardItem);
2117
d->root->d_func()->setModel(this);
2118
qDeleteAll(d->columnHeaderItems);
2119
d->columnHeaderItems.clear();
2120
qDeleteAll(d->rowHeaderItems);
2121
d->rowHeaderItems.clear();
2128
Returns a pointer to the QStandardItem associated with the given \a index.
2130
Calling this function is typically the initial step when processing
2131
QModelIndex-based signals from a view, such as
2132
QAbstractItemView::activated(). In your slot, you call itemFromIndex(),
2133
with the QModelIndex carried by the signal as argument, to obtain a
2134
pointer to the corresponding QStandardItem.
2136
Note that this function will lazily create an item for the index (using
2137
itemPrototype()), and set it in the parent item's child table, if no item
2138
already exists at that index.
2140
If \a index is an invalid index, this function returns 0.
2144
QStandardItem *QStandardItemModel::itemFromIndex(const QModelIndex &index) const
2146
Q_D(const QStandardItemModel);
2147
if ((index.row() < 0) || (index.column() < 0) || (index.model() != this))
2149
QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
2152
QStandardItem *item = parent->child(index.row(), index.column());
2155
item = d->createItem();
2156
parent->d_func()->setChild(index.row(), index.column(), item);
2164
Returns the QModelIndex associated with the given \a item.
2166
Use this function when you want to perform an operation that requires the
2167
QModelIndex of the item, such as
2168
QAbstractItemView::scrollTo(). QStandardItem::index() is provided as
2169
convenience; it is equivalent to calling this function.
2171
\sa itemFromIndex(), QStandardItem::index()
2173
QModelIndex QStandardItemModel::indexFromItem(const QStandardItem *item) const
2175
if (item && item->d_func()->parent) {
2176
QPair<int, int> pos = item->d_func()->position();
2177
return createIndex(pos.first, pos.second, item->d_func()->parent);
2179
return QModelIndex();
2185
Sets the number of rows in this model to \a rows. If
2186
this is less than rowCount(), the data in the unwanted rows
2189
\sa setColumnCount()
2191
void QStandardItemModel::setRowCount(int rows)
2193
Q_D(QStandardItemModel);
2194
d->root->setRowCount(rows);
2200
Sets the number of columns in this model to \a columns. If
2201
this is less than columnCount(), the data in the unwanted columns
2206
void QStandardItemModel::setColumnCount(int columns)
2208
Q_D(QStandardItemModel);
2209
d->root->setColumnCount(columns);
2215
Sets the item for the given \a row and \a column to \a item. The model
2216
takes ownership of the item. If necessary, the row count and column count
2217
are increased to fit the item. The previous item at the given location (if
2218
there was one) is deleted.
2222
void QStandardItemModel::setItem(int row, int column, QStandardItem *item)
2224
Q_D(QStandardItemModel);
2225
d->root->d_func()->setChild(row, column, item, true);
2229
\fn QStandardItemModel::setItem(int row, QStandardItem *item)
2236
Returns the item for the given \a row and \a column if one has been set;
2237
otherwise returns 0.
2239
\sa setItem(), takeItem(), itemFromIndex()
2241
QStandardItem *QStandardItemModel::item(int row, int column) const
2243
Q_D(const QStandardItemModel);
2244
return d->root->child(row, column);
2250
Returns the model's invisible root item.
2252
The invisible root item provides access to the model's top-level items
2253
through the QStandardItem API, making it possible to write functions that
2254
can treat top-level items and their children in a uniform way; for
2255
example, recursive functions involving a tree model.
2257
\note Calling \l{QAbstractItemModel::index()}{index()} on the QStandardItem object
2258
retrieved from this function is not valid.
2260
QStandardItem *QStandardItemModel::invisibleRootItem() const
2262
Q_D(const QStandardItemModel);
2263
return d->root.data();
2269
Sets the horizontal header item for \a column to \a item. The model takes
2270
ownership of the item. If necessary, the column count is increased to fit
2271
the item. The previous header item (if there was one) is deleted.
2273
\sa horizontalHeaderItem(), setHorizontalHeaderLabels(),
2274
setVerticalHeaderItem()
2276
void QStandardItemModel::setHorizontalHeaderItem(int column, QStandardItem *item)
2278
Q_D(QStandardItemModel);
2281
if (columnCount() <= column)
2282
setColumnCount(column + 1);
2284
QStandardItem *oldItem = d->columnHeaderItems.at(column);
2285
if (item == oldItem)
2289
if (item->model() == 0) {
2290
item->d_func()->setModel(this);
2292
qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p",
2299
oldItem->d_func()->setModel(0);
2302
d->columnHeaderItems.replace(column, item);
2303
emit headerDataChanged(Qt::Horizontal, column, column);
2309
Returns the horizontal header item for \a column if one has been set;
2310
otherwise returns 0.
2312
\sa setHorizontalHeaderItem(), verticalHeaderItem()
2314
QStandardItem *QStandardItemModel::horizontalHeaderItem(int column) const
2316
Q_D(const QStandardItemModel);
2317
if ((column < 0) || (column >= columnCount()))
2319
return d->columnHeaderItems.at(column);
2325
Sets the vertical header item for \a row to \a item. The model takes
2326
ownership of the item. If necessary, the row count is increased to fit the
2327
item. The previous header item (if there was one) is deleted.
2329
\sa verticalHeaderItem(), setVerticalHeaderLabels(),
2330
setHorizontalHeaderItem()
2332
void QStandardItemModel::setVerticalHeaderItem(int row, QStandardItem *item)
2334
Q_D(QStandardItemModel);
2337
if (rowCount() <= row)
2338
setRowCount(row + 1);
2340
QStandardItem *oldItem = d->rowHeaderItems.at(row);
2341
if (item == oldItem)
2345
if (item->model() == 0) {
2346
item->d_func()->setModel(this);
2348
qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p",
2355
oldItem->d_func()->setModel(0);
2358
d->rowHeaderItems.replace(row, item);
2359
emit headerDataChanged(Qt::Vertical, row, row);
2365
Returns the vertical header item for row \a row if one has been set;
2366
otherwise returns 0.
2368
\sa setVerticalHeaderItem(), horizontalHeaderItem()
2370
QStandardItem *QStandardItemModel::verticalHeaderItem(int row) const
2372
Q_D(const QStandardItemModel);
2373
if ((row < 0) || (row >= rowCount()))
2375
return d->rowHeaderItems.at(row);
2381
Sets the horizontal header labels using \a labels. If necessary, the
2382
column count is increased to the size of \a labels.
2384
\sa setHorizontalHeaderItem()
2386
void QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels)
2388
Q_D(QStandardItemModel);
2389
if (columnCount() < labels.count())
2390
setColumnCount(labels.count());
2391
for (int i = 0; i < labels.count(); ++i) {
2392
QStandardItem *item = horizontalHeaderItem(i);
2394
item = d->createItem();
2395
setHorizontalHeaderItem(i, item);
2397
item->setText(labels.at(i));
2404
Sets the vertical header labels using \a labels. If necessary, the row
2405
count is increased to the size of \a labels.
2407
\sa setVerticalHeaderItem()
2409
void QStandardItemModel::setVerticalHeaderLabels(const QStringList &labels)
2411
Q_D(QStandardItemModel);
2412
if (rowCount() < labels.count())
2413
setRowCount(labels.count());
2414
for (int i = 0; i < labels.count(); ++i) {
2415
QStandardItem *item = verticalHeaderItem(i);
2417
item = d->createItem();
2418
setVerticalHeaderItem(i, item);
2420
item->setText(labels.at(i));
2427
Sets the item prototype for the model to the specified \a item. The model
2428
takes ownership of the prototype.
2430
The item prototype acts as a QStandardItem factory, by relying on the
2431
QStandardItem::clone() function. To provide your own prototype, subclass
2432
QStandardItem, reimplement QStandardItem::clone() and set the prototype to
2433
be an instance of your custom class. Whenever QStandardItemModel needs to
2434
create an item on demand (for instance, when a view or item delegate calls
2435
setData())), the new items will be instances of your custom class.
2437
\sa itemPrototype(), QStandardItem::clone()
2439
void QStandardItemModel::setItemPrototype(const QStandardItem *item)
2441
Q_D(QStandardItemModel);
2442
if (d->itemPrototype != item) {
2443
delete d->itemPrototype;
2444
d->itemPrototype = item;
2451
Returns the item prototype used by the model. The model uses the item
2452
prototype as an item factory when it needs to construct new items on
2453
demand (for instance, when a view or item delegate calls setData()).
2455
\sa setItemPrototype()
2457
const QStandardItem *QStandardItemModel::itemPrototype() const
2459
Q_D(const QStandardItemModel);
2460
return d->itemPrototype;
2466
Returns a list of items that match the given \a text, using the given \a
2467
flags, in the given \a column.
2469
QList<QStandardItem*> QStandardItemModel::findItems(const QString &text,
2470
Qt::MatchFlags flags, int column) const
2472
QModelIndexList indexes = match(index(0, column, QModelIndex()),
2473
Qt::DisplayRole, text, -1, flags);
2474
QList<QStandardItem*> items;
2475
for (int i = 0; i < indexes.size(); ++i)
2476
items.append(itemFromIndex(indexes.at(i)));
2483
Appends a row containing \a items. If necessary, the column count is
2484
increased to the size of \a items.
2486
\sa insertRow(), appendColumn()
2488
void QStandardItemModel::appendRow(const QList<QStandardItem*> &items)
2490
invisibleRootItem()->appendRow(items);
2496
Appends a column containing \a items. If necessary, the row count is
2497
increased to the size of \a items.
2499
\sa insertColumn(), appendRow()
2501
void QStandardItemModel::appendColumn(const QList<QStandardItem*> &items)
2503
invisibleRootItem()->appendColumn(items);
2508
\fn QStandardItemModel::appendRow(QStandardItem *item)
2511
When building a list or a tree that has only one column, this function
2512
provides a convenient way to append a single new \a item.
2518
Inserts a row at \a row containing \a items. If necessary, the column
2519
count is increased to the size of \a items.
2521
\sa takeRow(), appendRow(), insertColumn()
2523
void QStandardItemModel::insertRow(int row, const QList<QStandardItem*> &items)
2525
invisibleRootItem()->insertRow(row, items);
2531
\fn void QStandardItemModel::insertRow(int row, QStandardItem *item)
2534
Inserts a row at \a row containing \a item.
2536
When building a list or a tree that has only one column, this function
2537
provides a convenient way to append a single new item.
2543
Inserts a column at \a column containing \a items. If necessary, the row
2544
count is increased to the size of \a items.
2546
\sa takeColumn(), appendColumn(), insertRow()
2548
void QStandardItemModel::insertColumn(int column, const QList<QStandardItem*> &items)
2550
invisibleRootItem()->insertColumn(column, items);
2556
Removes the item at (\a row, \a column) without deleting it. The model
2557
releases ownership of the item.
2559
\sa item(), takeRow(), takeColumn()
2561
QStandardItem *QStandardItemModel::takeItem(int row, int column)
2563
Q_D(QStandardItemModel);
2564
return d->root->takeChild(row, column);
2570
Removes the given \a row without deleting the row items, and returns a
2571
list of pointers to the removed items. The model releases ownership of the
2572
items. For items in the row that have not been set, the corresponding
2573
pointers in the list will be 0.
2577
QList<QStandardItem*> QStandardItemModel::takeRow(int row)
2579
Q_D(QStandardItemModel);
2580
return d->root->takeRow(row);
2586
Removes the given \a column without deleting the column items, and returns
2587
a list of pointers to the removed items. The model releases ownership of
2588
the items. For items in the column that have not been set, the
2589
corresponding pointers in the list will be 0.
2593
QList<QStandardItem*> QStandardItemModel::takeColumn(int column)
2595
Q_D(QStandardItemModel);
2596
return d->root->takeColumn(column);
2602
Removes the horizontal header item at \a column from the header without
2603
deleting it, and returns a pointer to the item. The model releases
2604
ownership of the item.
2606
\sa horizontalHeaderItem(), takeVerticalHeaderItem()
2608
QStandardItem *QStandardItemModel::takeHorizontalHeaderItem(int column)
2610
Q_D(QStandardItemModel);
2611
if ((column < 0) || (column >= columnCount()))
2613
QStandardItem *headerItem = d->columnHeaderItems.at(column);
2615
headerItem->d_func()->setParentAndModel(0, 0);
2616
d->columnHeaderItems.replace(column, 0);
2624
Removes the vertical header item at \a row from the header without
2625
deleting it, and returns a pointer to the item. The model releases
2626
ownership of the item.
2628
\sa verticalHeaderItem(), takeHorizontalHeaderItem()
2630
QStandardItem *QStandardItemModel::takeVerticalHeaderItem(int row)
2632
Q_D(QStandardItemModel);
2633
if ((row < 0) || (row >= rowCount()))
2635
QStandardItem *headerItem = d->rowHeaderItems.at(row);
2637
headerItem->d_func()->setParentAndModel(0, 0);
2638
d->rowHeaderItems.replace(row, 0);
2645
\property QStandardItemModel::sortRole
2646
\brief the item role that is used to query the model's data when sorting items
2648
The default value is Qt::DisplayRole.
2650
\sa sort(), QStandardItem::sortChildren()
2652
int QStandardItemModel::sortRole() const
2654
Q_D(const QStandardItemModel);
2658
void QStandardItemModel::setSortRole(int role)
2660
Q_D(QStandardItemModel);
2667
int QStandardItemModel::columnCount(const QModelIndex &parent) const
2669
Q_D(const QStandardItemModel);
2670
QStandardItem *item = d->itemFromIndex(parent);
2671
return item ? item->columnCount() : 0;
2677
QVariant QStandardItemModel::data(const QModelIndex &index, int role) const
2679
Q_D(const QStandardItemModel);
2680
QStandardItem *item = d->itemFromIndex(index);
2681
return item ? item->data(role) : QVariant();
2687
Qt::ItemFlags QStandardItemModel::flags(const QModelIndex &index) const
2689
Q_D(const QStandardItemModel);
2690
if (!d->indexValid(index))
2691
return d->root->flags();
2692
QStandardItem *item = d->itemFromIndex(index);
2694
return item->flags();
2695
return Qt::ItemIsSelectable
2698
|Qt::ItemIsDragEnabled
2699
|Qt::ItemIsDropEnabled;
2705
bool QStandardItemModel::hasChildren(const QModelIndex &parent) const
2707
Q_D(const QStandardItemModel);
2708
QStandardItem *item = d->itemFromIndex(parent);
2709
return item ? item->hasChildren() : false;
2715
QModelIndex QStandardItemModel::sibling(int row, int column, const QModelIndex &idx) const
2717
return createIndex(row, column, idx.internalPointer());
2723
QVariant QStandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const
2725
Q_D(const QStandardItemModel);
2727
|| ((orientation == Qt::Horizontal) && (section >= columnCount()))
2728
|| ((orientation == Qt::Vertical) && (section >= rowCount()))) {
2731
QStandardItem *headerItem = 0;
2732
if (orientation == Qt::Horizontal)
2733
headerItem = d->columnHeaderItems.at(section);
2734
else if (orientation == Qt::Vertical)
2735
headerItem = d->rowHeaderItems.at(section);
2736
return headerItem ? headerItem->data(role)
2737
: QAbstractItemModel::headerData(section, orientation, role);
2743
QStandardItemModel supports both copy and move.
2745
Qt::DropActions QStandardItemModel::supportedDropActions () const
2747
return Qt::CopyAction | Qt::MoveAction;
2753
QModelIndex QStandardItemModel::index(int row, int column, const QModelIndex &parent) const
2755
Q_D(const QStandardItemModel);
2756
QStandardItem *parentItem = d->itemFromIndex(parent);
2757
if ((parentItem == 0)
2760
|| (row >= parentItem->rowCount())
2761
|| (column >= parentItem->columnCount())) {
2762
return QModelIndex();
2764
return createIndex(row, column, parentItem);
2770
bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent)
2772
Q_D(QStandardItemModel);
2773
QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2776
return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
2782
bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent)
2784
Q_D(QStandardItemModel);
2785
QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2788
return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
2794
QMap<int, QVariant> QStandardItemModel::itemData(const QModelIndex &index) const
2796
Q_D(const QStandardItemModel);
2797
QStandardItem *item = d->itemFromIndex(index);
2798
return item ? item->d_func()->itemData() : QMap<int, QVariant>();
2804
QModelIndex QStandardItemModel::parent(const QModelIndex &child) const
2806
Q_D(const QStandardItemModel);
2807
if (!d->indexValid(child))
2808
return QModelIndex();
2809
QStandardItem *parentItem = static_cast<QStandardItem*>(child.internalPointer());
2810
return indexFromItem(parentItem);
2816
bool QStandardItemModel::removeColumns(int column, int count, const QModelIndex &parent)
2818
Q_D(QStandardItemModel);
2819
QStandardItem *item = d->itemFromIndex(parent);
2820
if ((item == 0) || (count < 1) || (column < 0) || ((column + count) > item->columnCount()))
2822
item->removeColumns(column, count);
2829
bool QStandardItemModel::removeRows(int row, int count, const QModelIndex &parent)
2831
Q_D(QStandardItemModel);
2832
QStandardItem *item = d->itemFromIndex(parent);
2833
if ((item == 0) || (count < 1) || (row < 0) || ((row + count) > item->rowCount()))
2835
item->removeRows(row, count);
2842
int QStandardItemModel::rowCount(const QModelIndex &parent) const
2844
Q_D(const QStandardItemModel);
2845
QStandardItem *item = d->itemFromIndex(parent);
2846
return item ? item->rowCount() : 0;
2852
bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
2854
if (!index.isValid())
2856
QStandardItem *item = itemFromIndex(index);
2859
item->setData(value, role);
2866
bool QStandardItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
2868
Q_D(QStandardItemModel);
2870
|| ((orientation == Qt::Horizontal) && (section >= columnCount()))
2871
|| ((orientation == Qt::Vertical) && (section >= rowCount()))) {
2874
QStandardItem *headerItem = 0;
2875
if (orientation == Qt::Horizontal) {
2876
headerItem = d->columnHeaderItems.at(section);
2877
if (headerItem == 0) {
2878
headerItem = d->createItem();
2879
headerItem->d_func()->setModel(this);
2880
d->columnHeaderItems.replace(section, headerItem);
2882
} else if (orientation == Qt::Vertical) {
2883
headerItem = d->rowHeaderItems.at(section);
2884
if (headerItem == 0) {
2885
headerItem = d->createItem();
2886
headerItem->d_func()->setModel(this);
2887
d->rowHeaderItems.replace(section, headerItem);
2891
headerItem->setData(value, role);
2900
bool QStandardItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
2902
QStandardItem *item = itemFromIndex(index);
2905
item->d_func()->setItemData(roles);
2912
void QStandardItemModel::sort(int column, Qt::SortOrder order)
2914
Q_D(QStandardItemModel);
2915
d->root->sortChildren(column, order);
2919
\fn QObject *QStandardItemModel::parent() const
2927
QStringList QStandardItemModel::mimeTypes() const
2929
return QAbstractItemModel::mimeTypes() << QLatin1String("application/x-qstandarditemmodeldatalist");
2935
QMimeData *QStandardItemModel::mimeData(const QModelIndexList &indexes) const
2937
QMimeData *data = QAbstractItemModel::mimeData(indexes);
2941
QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
2942
if (!mimeTypes().contains(format))
2945
QDataStream stream(&encoded, QIODevice::WriteOnly);
2947
QSet<QStandardItem*> itemsSet;
2948
QStack<QStandardItem*> stack;
2949
itemsSet.reserve(indexes.count());
2950
stack.reserve(indexes.count());
2951
for (int i = 0; i < indexes.count(); ++i) {
2952
QStandardItem *item = itemFromIndex(indexes.at(i));
2957
//remove duplicates childrens
2959
QSet<QStandardItem *> seen;
2960
while (!stack.isEmpty()) {
2961
QStandardItem *itm = stack.pop();
2962
if (seen.contains(itm))
2966
const QVector<QStandardItem*> &childList = itm->d_func()->children;
2967
for (int i = 0; i < childList.count(); ++i) {
2968
QStandardItem *chi = childList.at(i);
2970
QSet<QStandardItem *>::iterator it = itemsSet.find(chi);
2971
if (it != itemsSet.end()) {
2980
stack.reserve(itemsSet.count());
2981
foreach (QStandardItem *item, itemsSet) {
2985
//stream everything recursively
2986
while (!stack.isEmpty()) {
2987
QStandardItem *item = stack.pop();
2988
if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
2989
stream << item->row() << item->column();
2992
stream << *item << item->columnCount() << item->d_ptr->children.count();
2993
stack += item->d_ptr->children;
2995
QStandardItem dummy;
2996
stream << dummy << 0 << 0;
3000
data->setData(format, encoded);
3006
Used by QStandardItemModel::dropMimeData
3007
stream out an item and his children
3009
void QStandardItemModelPrivate::decodeDataRecursive(QDataStream &stream, QStandardItem *item)
3011
int colCount, childCount;
3013
stream >> colCount >> childCount;
3014
item->setColumnCount(colCount);
3016
int childPos = childCount;
3018
while(childPos > 0) {
3020
QStandardItem *child = createItem();
3021
decodeDataRecursive(stream, child);
3022
item->setChild( childPos / colCount, childPos % colCount, child);
3030
bool QStandardItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
3031
int row, int column, const QModelIndex &parent)
3033
Q_D(QStandardItemModel);
3034
// check if the action is supported
3035
if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
3037
// check if the format is supported
3038
QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
3039
if (!data->hasFormat(format))
3040
return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
3042
if (row > rowCount(parent))
3043
row = rowCount(parent);
3045
row = rowCount(parent);
3049
// decode and insert
3050
QByteArray encoded = data->data(format);
3051
QDataStream stream(&encoded, QIODevice::ReadOnly);
3054
//code based on QAbstractItemModel::decodeData
3055
// adapted to work with QStandardItem
3060
QVector<int> rows, columns;
3061
QVector<QStandardItem *> items;
3063
while (!stream.atEnd()) {
3065
QStandardItem *item = d->createItem();
3067
d->decodeDataRecursive(stream, item);
3073
left = qMin(c, left);
3074
bottom = qMax(r, bottom);
3075
right = qMax(c, right);
3078
// insert the dragged items into the table, use a bit array to avoid overwriting items,
3079
// since items from different tables can have the same row and column
3080
int dragRowCount = 0;
3081
int dragColumnCount = right - left + 1;
3083
// Compute the number of continuous rows upon insertion and modify the rows to match
3084
QVector<int> rowsToInsert(bottom + 1);
3085
for (int i = 0; i < rows.count(); ++i)
3086
rowsToInsert[rows.at(i)] = 1;
3087
for (int i = 0; i < rowsToInsert.count(); ++i) {
3088
if (rowsToInsert[i] == 1){
3089
rowsToInsert[i] = dragRowCount;
3093
for (int i = 0; i < rows.count(); ++i)
3094
rows[i] = top + rowsToInsert[rows[i]];
3096
QBitArray isWrittenTo(dragRowCount * dragColumnCount);
3098
// make space in the table for the dropped data
3099
int colCount = columnCount(parent);
3100
if (colCount < dragColumnCount + column) {
3101
insertColumns(colCount, dragColumnCount + column - colCount, parent);
3102
colCount = columnCount(parent);
3104
insertRows(row, dragRowCount, parent);
3107
column = qMax(0, column);
3109
QStandardItem *parentItem = itemFromIndex (parent);
3111
parentItem = invisibleRootItem();
3113
QVector<QPersistentModelIndex> newIndexes(items.size());
3114
// set the data in the table
3115
for (int j = 0; j < items.size(); ++j) {
3116
int relativeRow = rows.at(j) - top;
3117
int relativeColumn = columns.at(j) - left;
3118
int destinationRow = relativeRow + row;
3119
int destinationColumn = relativeColumn + column;
3120
int flat = (relativeRow * dragColumnCount) + relativeColumn;
3121
// if the item was already written to, or we just can't fit it in the table, create a new row
3122
if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
3123
destinationColumn = qBound(column, destinationColumn, colCount - 1);
3124
destinationRow = row + dragRowCount;
3125
insertRows(row + dragRowCount, 1, parent);
3126
flat = (dragRowCount * dragColumnCount) + relativeColumn;
3127
isWrittenTo.resize(++dragRowCount * dragColumnCount);
3129
if (!isWrittenTo.testBit(flat)) {
3130
newIndexes[j] = index(destinationRow, destinationColumn, parentItem->index());
3131
isWrittenTo.setBit(flat);
3135
for(int k = 0; k < newIndexes.size(); k++) {
3136
if (newIndexes.at(k).isValid()) {
3137
parentItem->setChild(newIndexes.at(k).row(), newIndexes.at(k).column(), items.at(k));
3148
#include "moc_qstandarditemmodel.cpp"
3150
#endif // QT_NO_STANDARDITEMMODEL