Qt 4.8
qstandarditemmodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qstandarditemmodel.h"
43 
44 #ifndef QT_NO_STANDARDITEMMODEL
45 
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>
55 
56 #include <private/qstandarditemmodel_p.h>
57 #include <qdebug.h>
58 
60 
62 {
63 public:
65  { }
66 
68  const QPair<QStandardItem*, int> &r) const
69  {
70  return *(l.first) < *(r.first);
71  }
72 };
73 
75 {
76 public:
78  { }
79 
81  const QPair<QStandardItem*, int> &r) const
82  {
83  return *(r.first) < *(l.first);
84  }
85 };
86 
91 {
93  for (it = children.constBegin(); it != children.constEnd(); ++it) {
94  QStandardItem *child = *it;
95  if (child)
96  child->d_func()->setModel(0);
97  delete child;
98  }
99  children.clear();
100  if (parent && model)
101  parent->d_func()->childDeleted(q_func());
102 }
103 
108 {
109  if (QStandardItem *par = parent) {
110  int idx = par->d_func()->childIndex(q_func());
111  if (idx == -1)
112  return QPair<int, int>(-1, -1);
113  return QPair<int, int>(idx / par->columnCount(), idx % par->columnCount());
114  }
115  // ### support header items?
116  return QPair<int, int>(-1, -1);
117 }
118 
122 void QStandardItemPrivate::setChild(int row, int column, QStandardItem *item,
123  bool emitChanged)
124 {
126  if (item == q) {
127  qWarning("QStandardItem::setChild: Can't make an item a child of itself %p",
128  item);
129  return;
130  }
131  if ((row < 0) || (column < 0))
132  return;
133  if (rows <= row)
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);
140  if (item == oldItem)
141  return;
142  if (item) {
143  if (item->d_func()->parent == 0) {
144  item->d_func()->setParentAndModel(q, model);
145  } else {
146  qWarning("QStandardItem::setChild: Ignoring duplicate insertion of item %p",
147  item);
148  return;
149  }
150  }
151  if (oldItem)
152  oldItem->d_func()->setModel(0);
153  delete oldItem;
154  children.replace(index, item);
155  if (emitChanged && model)
156  model->d_func()->itemChanged(item);
157 }
158 
159 
163 void QStandardItemPrivate::changeFlags(bool enable, Qt::ItemFlags f)
164 {
166  Qt::ItemFlags flags = q->flags();
167  if (enable)
168  flags |= f;
169  else
170  flags &= ~f;
171  q->setFlags(flags);
172 }
173 
178 {
179  int index = childIndex(child);
180  Q_ASSERT(index != -1);
181  children.replace(index, 0);
182 }
183 
188 {
190 
191  //let's build the vector of new values
192  QVector<QWidgetItemData> newValues;
194  for (it = roles.begin(); it != roles.end(); ++it) {
195  QVariant value = it.value();
196  if (value.isValid()) {
197  int role = it.key();
198  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
199  QWidgetItemData wid(role,it.value());
200  newValues.append(wid);
201  }
202  }
203 
204  if (values!=newValues) {
205  values=newValues;
206  if (model)
207  model->d_func()->itemChanged(q);
208  }
209 }
210 
215 {
216  QMap<int, QVariant> result;
218  for (it = values.begin(); it != values.end(); ++it)
219  result.insert((*it).role, (*it).value);
220  return result;
221 }
222 
227 {
229  if (column >= columnCount())
230  return;
231 
233  QVector<int> unsortable;
234 
235  sortable.reserve(rowCount());
236  unsortable.reserve(rowCount());
237 
238  for (int row = 0; row < rowCount(); ++row) {
239  QStandardItem *itm = q->child(row, column);
240  if (itm)
241  sortable.append(QPair<QStandardItem*,int>(itm, row));
242  else
243  unsortable.append(row);
244  }
245 
246  if (order == Qt::AscendingOrder) {
248  qStableSort(sortable.begin(), sortable.end(), lt);
249  } else {
251  qStableSort(sortable.begin(), sortable.end(), gt);
252  }
253 
254  QModelIndexList changedPersistentIndexesFrom, changedPersistentIndexesTo;
255  QVector<QStandardItem*> sorted_children(children.count());
256  for (int i = 0; i < rowCount(); ++i) {
257  int r = (i < sortable.count()
258  ? sortable.at(i).second
259  : unsortable.at(i - sortable.count()));
260  for (int c = 0; c < columnCount(); ++c) {
261  QStandardItem *itm = q->child(r, c);
262  sorted_children[childIndex(i, c)] = itm;
263  if (model) {
264  QModelIndex from = model->createIndex(r, c, q);
265  if (model->d_func()->persistent.indexes.contains(from)) {
266  QModelIndex to = model->createIndex(i, c, q);
267  changedPersistentIndexesFrom.append(from);
268  changedPersistentIndexesTo.append(to);
269  }
270  }
271  }
272  }
273 
274  children = sorted_children;
275 
276  if (model) {
277  model->changePersistentIndexList(changedPersistentIndexesFrom, changedPersistentIndexesTo);
278  }
279 
281  for (it = children.begin(); it != children.end(); ++it) {
282  if (*it)
283  (*it)->d_func()->sortChildren(column, order);
284  }
285 }
286 
292 {
293  if (children.isEmpty()) {
294  if (model)
295  model->d_func()->invalidatePersistentIndex(model->indexFromItem(q_ptr));
296  model = mod;
297  } else {
299  stack.push(q_ptr);
300  while (!stack.isEmpty()) {
301  QStandardItem *itm = stack.pop();
302  if (itm->d_func()->model) {
303  itm->d_func()->model->d_func()->invalidatePersistentIndex(itm->d_func()->model->indexFromItem(itm));
304  }
305  itm->d_func()->model = mod;
306  const QVector<QStandardItem*> &childList = itm->d_func()->children;
307  for (int i = 0; i < childList.count(); ++i) {
308  QStandardItem *chi = childList.at(i);
309  if (chi)
310  stack.push(chi);
311  }
312  }
313  }
314 }
315 
320  : root(new QStandardItem),
321  itemPrototype(0),
322  sortRole(Qt::DisplayRole)
323 {
325 }
326 
331 {
332  delete itemPrototype;
335 }
336 
341 {
345 }
346 
351  const QModelIndex &bottomRight)
352 {
354  QModelIndex parent = topLeft.parent();
355  for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
356  for (int column = topLeft.column(); column <= bottomRight.column(); ++column) {
357  QModelIndex index = q->index(row, column, parent);
358  if (QStandardItem *item = itemFromIndex(index))
359  emit q->itemChanged(item);
360  }
361  }
362 }
363 
368 {
370  if ((row < 0) || (row > rowCount()))
371  return false;
372  int count = items.count();
373  if (model)
374  model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
375  if (rowCount() == 0) {
376  if (columnCount() == 0)
377  q->setColumnCount(1);
378  children.resize(columnCount() * count);
379  rows = count;
380  } else {
381  rows += count;
382  int index = childIndex(row, 0);
383  if (index != -1)
384  children.insert(index, columnCount() * count, 0);
385  }
386  for (int i = 0; i < items.count(); ++i) {
387  QStandardItem *item = items.at(i);
388  item->d_func()->model = model;
389  item->d_func()->parent = q;
390  int index = childIndex(i + row, 0);
391  children.replace(index, item);
392  }
393  if (model)
394  model->d_func()->rowsInserted(q, row, count);
395  return true;
396 }
397 
398 bool QStandardItemPrivate::insertRows(int row, int count, const QList<QStandardItem*> &items)
399 {
401  if ((count < 1) || (row < 0) || (row > rowCount()))
402  return false;
403  if (model)
404  model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);
405  if (rowCount() == 0) {
406  children.resize(columnCount() * count);
407  rows = count;
408  } else {
409  rows += count;
410  int index = childIndex(row, 0);
411  if (index != -1)
412  children.insert(index, columnCount() * count, 0);
413  }
414  if (!items.isEmpty()) {
415  int index = childIndex(row, 0);
416  int limit = qMin(items.count(), columnCount() * count);
417  for (int i = 0; i < limit; ++i) {
418  QStandardItem *item = items.at(i);
419  if (item) {
420  if (item->d_func()->parent == 0) {
421  item->d_func()->setParentAndModel(q, model);
422  } else {
423  qWarning("QStandardItem::insertRows: Ignoring duplicate insertion of item %p",
424  item);
425  item = 0;
426  }
427  }
428  children.replace(index, item);
429  ++index;
430  }
431  }
432  if (model)
433  model->d_func()->rowsInserted(q, row, count);
434  return true;
435 }
436 
440 bool QStandardItemPrivate::insertColumns(int column, int count, const QList<QStandardItem*> &items)
441 {
443  if ((count < 1) || (column < 0) || (column > columnCount()))
444  return false;
445  if (model)
446  model->d_func()->columnsAboutToBeInserted(q, column, column + count - 1);
447  if (columnCount() == 0) {
448  children.resize(rowCount() * count);
449  columns = count;
450  } else {
451  columns += count;
452  int index = childIndex(0, column);
453  for (int row = 0; row < rowCount(); ++row) {
454  children.insert(index, count, 0);
455  index += columnCount();
456  }
457  }
458  if (!items.isEmpty()) {
459  int limit = qMin(items.count(), rowCount() * count);
460  for (int i = 0; i < limit; ++i) {
461  QStandardItem *item = items.at(i);
462  if (item) {
463  if (item->d_func()->parent == 0) {
464  item->d_func()->setParentAndModel(q, model);
465  } else {
466  qWarning("QStandardItem::insertColumns: Ignoring duplicate insertion of item %p",
467  item);
468  item = 0;
469  }
470  }
471  int r = i / count;
472  int c = column + (i % count);
473  int index = childIndex(r, c);
474  children.replace(index, item);
475  }
476  }
477  if (model)
478  model->d_func()->columnsInserted(q, column, count);
479  return true;
480 }
481 
486 {
488  if (item->d_func()->parent == 0) {
489  // Header item
490  int idx = columnHeaderItems.indexOf(item);
491  if (idx != -1) {
492  emit q->headerDataChanged(Qt::Horizontal, idx, idx);
493  } else {
494  idx = rowHeaderItems.indexOf(item);
495  if (idx != -1)
496  emit q->headerDataChanged(Qt::Vertical, idx, idx);
497  }
498  } else {
499  // Normal item
500  QModelIndex index = q->indexFromItem(item);
501  emit q->dataChanged(index, index);
502  }
503 }
504 
509  int start, int end)
510 {
512  QModelIndex index = q->indexFromItem(parent);
513  q->beginInsertRows(index, start, end);
514 }
515 
520  int start, int end)
521 {
523  QModelIndex index = q->indexFromItem(parent);
524  q->beginInsertColumns(index, start, end);
525 }
526 
531  int start, int end)
532 {
534  QModelIndex index = q->indexFromItem(parent);
535  q->beginRemoveRows(index, start, end);
536 }
537 
542  int start, int end)
543 {
545  QModelIndex index = q->indexFromItem(parent);
546  q->beginRemoveColumns(index, start, end);
547 }
548 
553  int row, int count)
554 {
556  if (parent == root.data())
557  rowHeaderItems.insert(row, count, 0);
558  q->endInsertRows();
559 }
560 
565  int column, int count)
566 {
568  if (parent == root.data())
569  columnHeaderItems.insert(column, count, 0);
570  q->endInsertColumns();
571 }
572 
577  int row, int count)
578 {
580  if (parent == root.data()) {
581  for (int i = row; i < row + count; ++i) {
582  QStandardItem *oldItem = rowHeaderItems.at(i);
583  if (oldItem)
584  oldItem->d_func()->setModel(0);
585  delete oldItem;
586  }
587  rowHeaderItems.remove(row, count);
588  }
589  q->endRemoveRows();
590 }
591 
596  int column, int count)
597 {
599  if (parent == root.data()) {
600  for (int i = column; i < column + count; ++i) {
601  QStandardItem *oldItem = columnHeaderItems.at(i);
602  if (oldItem)
603  oldItem->d_func()->setModel(0);
604  delete oldItem;
605  }
606  columnHeaderItems.remove(column, count);
607  }
608  q->endRemoveColumns();
609 }
610 
702  : d_ptr(new QStandardItemPrivate)
703 {
705  d->q_ptr = this;
706 }
707 
713 {
715  d->q_ptr = this;
716  setText(text);
717 }
718 
724 {
726  d->q_ptr = this;
727  setIcon(icon);
728  setText(text);
729 }
730 
734 QStandardItem::QStandardItem(int rows, int columns)
736 {
738  d->q_ptr = this;
739  setRowCount(rows);
740  setColumnCount(columns);
741 }
742 
747  : d_ptr(&dd)
748 {
750  d->q_ptr = this;
751 }
752 
761 {
763  d->q_ptr = this;
764  operator=(other);
765 }
766 
774 {
776  d->values = other.d_func()->values;
777  return *this;
778 }
779 
785 {
786 }
787 
794 {
795  Q_D(const QStandardItem);
796  if (!d->model || (d->model->d_func()->root.data() != d->parent))
797  return d->parent;
798  return 0;
799 }
800 
814 void QStandardItem::setData(const QVariant &value, int role)
815 {
817  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
819  for (it = d->values.begin(); it != d->values.end(); ++it) {
820  if ((*it).role == role) {
821  if (value.isValid()) {
822  if ((*it).value.type() == value.type() && (*it).value == value)
823  return;
824  (*it).value = value;
825  } else {
826  d->values.erase(it);
827  }
828  if (d->model)
829  d->model->d_func()->itemChanged(this);
830  return;
831  }
832  }
833  d->values.append(QWidgetItemData(role, value));
834  if (d->model)
835  d->model->d_func()->itemChanged(this);
836 }
837 
846 {
847  Q_D(const QStandardItem);
848  role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
850  for (it = d->values.begin(); it != d->values.end(); ++it) {
851  if ((*it).role == role)
852  return (*it).value;
853  }
854  return QVariant();
855 }
856 
873 {
875  if (d->model)
876  d->model->d_func()->itemChanged(this);
877 }
878 
887 void QStandardItem::setFlags(Qt::ItemFlags flags)
888 {
889  setData((int)flags, Qt::UserRole - 1);
890 }
891 
902 Qt::ItemFlags QStandardItem::flags() const
903 {
904  QVariant v = data(Qt::UserRole - 1);
905  if (!v.isValid())
908  return Qt::ItemFlags(v.toInt());
909 }
910 
1227 {
1228  Q_D(QStandardItem);
1229  d->changeFlags(enabled, Qt::ItemIsEnabled);
1230 }
1231 
1258 void QStandardItem::setEditable(bool editable)
1259 {
1260  Q_D(QStandardItem);
1261  d->changeFlags(editable, Qt::ItemIsEditable);
1262 }
1263 
1291 void QStandardItem::setSelectable(bool selectable)
1292 {
1293  Q_D(QStandardItem);
1294  d->changeFlags(selectable, Qt::ItemIsSelectable);
1295 }
1296 
1320 void QStandardItem::setCheckable(bool checkable)
1321 {
1322  Q_D(QStandardItem);
1323  if (checkable && !isCheckable()) {
1324  // make sure there's data for the checkstate role
1325  if (!data(Qt::CheckStateRole).isValid())
1327  }
1328  d->changeFlags(checkable, Qt::ItemIsUserCheckable);
1329 }
1330 
1352 void QStandardItem::setTristate(bool tristate)
1353 {
1354  Q_D(QStandardItem);
1355  d->changeFlags(tristate, Qt::ItemIsTristate);
1356 }
1357 
1372 #ifndef QT_NO_DRAGANDDROP
1373 
1383 void QStandardItem::setDragEnabled(bool dragEnabled)
1384 {
1385  Q_D(QStandardItem);
1386  d->changeFlags(dragEnabled, Qt::ItemIsDragEnabled);
1387 }
1388 
1416 void QStandardItem::setDropEnabled(bool dropEnabled)
1417 {
1418  Q_D(QStandardItem);
1419  d->changeFlags(dropEnabled, Qt::ItemIsDropEnabled);
1420 }
1421 
1436 #endif // QT_NO_DRAGANDDROP
1437 
1445 {
1446  Q_D(const QStandardItem);
1447  QPair<int, int> pos = d->position();
1448  return pos.first;
1449 }
1450 
1458 {
1459  Q_D(const QStandardItem);
1460  QPair<int, int> pos = d->position();
1461  return pos.second;
1462 }
1463 
1477 {
1478  Q_D(const QStandardItem);
1479  return d->model ? d->model->indexFromItem(this) : QModelIndex();
1480 }
1481 
1491 {
1492  Q_D(const QStandardItem);
1493  return d->model;
1494 }
1495 
1503 {
1504  int rc = rowCount();
1505  if (rc == rows)
1506  return;
1507  if (rc < rows)
1508  insertRows(qMax(rc, 0), rows - rc);
1509  else
1510  removeRows(qMax(rows, 0), rc - rows);
1511 }
1512 
1519 {
1520  Q_D(const QStandardItem);
1521  return d->rowCount();
1522 }
1523 
1531 {
1532  int cc = columnCount();
1533  if (cc == columns)
1534  return;
1535  if (cc < columns)
1536  insertColumns(qMax(cc, 0), columns - cc);
1537  else
1538  removeColumns(qMax(columns, 0), cc - columns);
1539 }
1540 
1547 {
1548  Q_D(const QStandardItem);
1549  return d->columnCount();
1550 }
1551 
1559 {
1560  Q_D(QStandardItem);
1561  if (row < 0)
1562  return;
1563  if (columnCount() < items.count())
1564  setColumnCount(items.count());
1565  d->insertRows(row, 1, items);
1566 }
1567 
1574 {
1575  Q_D(QStandardItem);
1576  if (row < 0)
1577  return;
1578  d->insertRows(row, items);
1579 }
1580 
1588 {
1589  Q_D(QStandardItem);
1590  if (column < 0)
1591  return;
1592  if (rowCount() < items.count())
1593  setRowCount(items.count());
1594  d->insertColumns(column, 1, items);
1595 }
1596 
1602 void QStandardItem::insertRows(int row, int count)
1603 {
1604  Q_D(QStandardItem);
1605  if (rowCount() < row) {
1606  count += row - rowCount();
1607  row = rowCount();
1608  }
1609  d->insertRows(row, count, QList<QStandardItem*>());
1610 }
1611 
1618 {
1619  Q_D(QStandardItem);
1620  if (columnCount() < column) {
1621  count += column - columnCount();
1622  column = columnCount();
1623  }
1624  d->insertColumns(column, count, QList<QStandardItem*>());
1625 }
1626 
1705 {
1706  removeRows(row, 1);
1707 }
1708 
1716 {
1717  removeColumns(column, 1);
1718 }
1719 
1726 void QStandardItem::removeRows(int row, int count)
1727 {
1728  Q_D(QStandardItem);
1729  if ((count < 1) || (row < 0) || ((row + count) > rowCount()))
1730  return;
1731  if (d->model)
1732  d->model->d_func()->rowsAboutToBeRemoved(this, row, row + count - 1);
1733  int i = d->childIndex(row, 0);
1734  int n = count * d->columnCount();
1735  for (int j = i; j < n+i; ++j) {
1736  QStandardItem *oldItem = d->children.at(j);
1737  if (oldItem)
1738  oldItem->d_func()->setModel(0);
1739  delete oldItem;
1740  }
1741  d->children.remove(qMax(i, 0), n);
1742  d->rows -= count;
1743  if (d->model)
1744  d->model->d_func()->rowsRemoved(this, row, count);
1745 }
1746 
1754 {
1755  Q_D(QStandardItem);
1756  if ((count < 1) || (column < 0) || ((column + count) > columnCount()))
1757  return;
1758  if (d->model)
1759  d->model->d_func()->columnsAboutToBeRemoved(this, column, column + count - 1);
1760  for (int row = d->rowCount() - 1; row >= 0; --row) {
1761  int i = d->childIndex(row, column);
1762  for (int j=i; j<i+count; ++j) {
1763  QStandardItem *oldItem = d->children.at(j);
1764  if (oldItem)
1765  oldItem->d_func()->setModel(0);
1766  delete oldItem;
1767  }
1768  d->children.remove(i, count);
1769  }
1770  d->columns -= count;
1771  if (d->model)
1772  d->model->d_func()->columnsRemoved(this, column, count);
1773 }
1774 
1781 {
1782  return (rowCount() > 0) && (columnCount() > 0);
1783 }
1784 
1793 {
1794  Q_D(QStandardItem);
1795  d->setChild(row, column, item, true);
1796 }
1797 
1812 {
1813  Q_D(const QStandardItem);
1814  int index = d->childIndex(row, column);
1815  if (index == -1)
1816  return 0;
1817  return d->children.at(index);
1818 }
1819 
1831 {
1832  Q_D(QStandardItem);
1833  QStandardItem *item = 0;
1834  int index = d->childIndex(row, column);
1835  if (index != -1) {
1836  item = d->children.at(index);
1837  if (item)
1838  item->d_func()->setParentAndModel(0, 0);
1839  d->children.replace(index, 0);
1840  }
1841  return item;
1842 }
1843 
1852 {
1853  Q_D(QStandardItem);
1854  if ((row < 0) || (row >= rowCount()))
1855  return QList<QStandardItem*>();
1856  if (d->model)
1857  d->model->d_func()->rowsAboutToBeRemoved(this, row, row);
1858  QList<QStandardItem*> items;
1859  int index = d->childIndex(row, 0); // Will return -1 if there are no columns
1860  if (index != -1) {
1861  int col_count = d->columnCount();
1862  for (int column = 0; column < col_count; ++column) {
1863  QStandardItem *ch = d->children.at(index + column);
1864  if (ch)
1865  ch->d_func()->setParentAndModel(0, 0);
1866  items.append(ch);
1867  }
1868  d->children.remove(index, col_count);
1869  }
1870  d->rows--;
1871  if (d->model)
1872  d->model->d_func()->rowsRemoved(this, row, 1);
1873  return items;
1874 }
1875 
1884 {
1885  Q_D(QStandardItem);
1886  if ((column < 0) || (column >= columnCount()))
1887  return QList<QStandardItem*>();
1888  if (d->model)
1889  d->model->d_func()->columnsAboutToBeRemoved(this, column, column);
1890  QList<QStandardItem*> items;
1891 
1892  for (int row = d->rowCount() - 1; row >= 0; --row) {
1893  int index = d->childIndex(row, column);
1894  QStandardItem *ch = d->children.at(index);
1895  if (ch)
1896  ch->d_func()->setParentAndModel(0, 0);
1897  d->children.remove(index);
1898  items.prepend(ch);
1899  }
1900  d->columns--;
1901  if (d->model)
1902  d->model->d_func()->columnsRemoved(this, column, 1);
1903  return items;
1904 }
1905 
1918 bool QStandardItem::operator<(const QStandardItem &other) const
1919 {
1920  const int role = model() ? model()->sortRole() : Qt::DisplayRole;
1921  const QVariant l = data(role), r = other.data(role);
1922  // this code is copied from QSortFilterProxyModel::lessThan()
1923  switch (l.userType()) {
1924  case QVariant::Invalid:
1925  return (r.type() == QVariant::Invalid);
1926  case QVariant::Int:
1927  return l.toInt() < r.toInt();
1928  case QVariant::UInt:
1929  return l.toUInt() < r.toUInt();
1930  case QVariant::LongLong:
1931  return l.toLongLong() < r.toLongLong();
1932  case QVariant::ULongLong:
1933  return l.toULongLong() < r.toULongLong();
1934  case QMetaType::Float:
1935  return l.toFloat() < r.toFloat();
1936  case QVariant::Double:
1937  return l.toDouble() < r.toDouble();
1938  case QVariant::Char:
1939  return l.toChar() < r.toChar();
1940  case QVariant::Date:
1941  return l.toDate() < r.toDate();
1942  case QVariant::Time:
1943  return l.toTime() < r.toTime();
1944  case QVariant::DateTime:
1945  return l.toDateTime() < r.toDateTime();
1946  case QVariant::String:
1947  default:
1948  return l.toString().compare(r.toString()) < 0;
1949  }
1950 }
1951 
1962 {
1963  Q_D(QStandardItem);
1964  if ((column < 0) || (rowCount() == 0))
1965  return;
1966  if (d->model)
1967  emit d->model->layoutAboutToBeChanged();
1968  d->sortChildren(column, order);
1969  if (d->model)
1970  emit d->model->layoutChanged();
1971 }
1972 
1983 {
1984  return new QStandardItem(*this);
1985 }
1986 
1996 {
1997  return Type;
1998 }
1999 
2000 #ifndef QT_NO_DATASTREAM
2001 
2009 {
2010  Q_D(QStandardItem);
2011  in >> d->values;
2012  qint32 flags;
2013  in >> flags;
2014  setFlags(Qt::ItemFlags(flags));
2015 }
2016 
2024 {
2025  Q_D(const QStandardItem);
2026  out << d->values;
2027  out << flags();
2028 }
2029 
2044 {
2045  item.read(in);
2046  return in;
2047 }
2048 
2063 {
2064  item.write(out);
2065  return out;
2066 }
2067 
2068 #endif // QT_NO_DATASTREAM
2069 
2164 {
2166  d->init();
2167  d->root->d_func()->setModel(this);
2168 }
2169 
2176 {
2178  d->init();
2179  d->root->insertColumns(0, columns);
2180  d->columnHeaderItems.insert(0, columns, 0);
2181  d->root->insertRows(0, rows);
2182  d->rowHeaderItems.insert(0, rows, 0);
2183  d->root->d_func()->setModel(this);
2184 }
2185 
2190  : QAbstractItemModel(dd, parent)
2191 {
2193  d->init();
2194 }
2195 
2200 {
2201 }
2202 
2210 {
2212  d->root.reset(new QStandardItem);
2213  d->root->d_func()->setModel(this);
2214  qDeleteAll(d->columnHeaderItems);
2215  d->columnHeaderItems.clear();
2216  qDeleteAll(d->rowHeaderItems);
2217  d->rowHeaderItems.clear();
2218  reset();
2219 }
2220 
2244 {
2245  Q_D(const QStandardItemModel);
2246  if ((index.row() < 0) || (index.column() < 0) || (index.model() != this))
2247  return 0;
2248  QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
2249  if (parent == 0)
2250  return 0;
2251  QStandardItem *item = parent->child(index.row(), index.column());
2252  // lazy part
2253  if (item == 0) {
2254  item = d->createItem();
2255  parent->d_func()->setChild(index.row(), index.column(), item);
2256  }
2257  return item;
2258 }
2259 
2276 {
2277  if (item && item->d_func()->parent) {
2278  QPair<int, int> pos = item->d_func()->position();
2279  return createIndex(pos.first, pos.second, item->d_func()->parent);
2280  }
2281  return QModelIndex();
2282 }
2283 
2297 {
2299  d->root->setRowCount(rows);
2300 }
2301 
2315 {
2317  d->root->setColumnCount(columns);
2318 }
2319 
2334 {
2336  d->root->d_func()->setChild(row, column, item, true);
2337 }
2338 
2355 QStandardItem *QStandardItemModel::item(int row, int column) const
2356 {
2357  Q_D(const QStandardItemModel);
2358  return d->root->child(row, column);
2359 }
2360 
2378 {
2379  Q_D(const QStandardItemModel);
2380  return d->root.data();
2381 }
2382 
2397 {
2399  if (column < 0)
2400  return;
2401  if (columnCount() <= column)
2402  setColumnCount(column + 1);
2403 
2404  QStandardItem *oldItem = d->columnHeaderItems.at(column);
2405  if (item == oldItem)
2406  return;
2407 
2408  if (item) {
2409  if (item->model() == 0) {
2410  item->d_func()->setModel(this);
2411  } else {
2412  qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p",
2413  item);
2414  return;
2415  }
2416  }
2417 
2418  if (oldItem)
2419  oldItem->d_func()->setModel(0);
2420  delete oldItem;
2421 
2422  d->columnHeaderItems.replace(column, item);
2423  emit headerDataChanged(Qt::Horizontal, column, column);
2424 }
2425 
2438 {
2439  Q_D(const QStandardItemModel);
2440  if ((column < 0) || (column >= columnCount()))
2441  return 0;
2442  return d->columnHeaderItems.at(column);
2443 }
2444 
2459 {
2461  if (row < 0)
2462  return;
2463  if (rowCount() <= row)
2464  setRowCount(row + 1);
2465 
2466  QStandardItem *oldItem = d->rowHeaderItems.at(row);
2467  if (item == oldItem)
2468  return;
2469 
2470  if (item) {
2471  if (item->model() == 0) {
2472  item->d_func()->setModel(this);
2473  } else {
2474  qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p",
2475  item);
2476  return;
2477  }
2478  }
2479 
2480  if (oldItem)
2481  oldItem->d_func()->setModel(0);
2482  delete oldItem;
2483 
2484  d->rowHeaderItems.replace(row, item);
2485  emit headerDataChanged(Qt::Vertical, row, row);
2486 }
2487 
2500 {
2501  Q_D(const QStandardItemModel);
2502  if ((row < 0) || (row >= rowCount()))
2503  return 0;
2504  return d->rowHeaderItems.at(row);
2505 }
2506 
2519 {
2521  if (columnCount() < labels.count())
2522  setColumnCount(labels.count());
2523  for (int i = 0; i < labels.count(); ++i) {
2525  if (!item) {
2526  item = d->createItem();
2527  setHorizontalHeaderItem(i, item);
2528  }
2529  item->setText(labels.at(i));
2530  }
2531 }
2532 
2545 {
2547  if (rowCount() < labels.count())
2548  setRowCount(labels.count());
2549  for (int i = 0; i < labels.count(); ++i) {
2551  if (!item) {
2552  item = d->createItem();
2553  setVerticalHeaderItem(i, item);
2554  }
2555  item->setText(labels.at(i));
2556  }
2557 }
2558 
2578 {
2580  if (d->itemPrototype != item) {
2581  delete d->itemPrototype;
2582  d->itemPrototype = item;
2583  }
2584 }
2585 
2599 {
2600  Q_D(const QStandardItemModel);
2601  return d->itemPrototype;
2602 }
2603 
2614  Qt::MatchFlags flags, int column) const
2615 {
2616  QModelIndexList indexes = match(index(0, column, QModelIndex()),
2617  Qt::DisplayRole, text, -1, flags);
2618  QList<QStandardItem*> items;
2619  for (int i = 0; i < indexes.size(); ++i)
2620  items.append(itemFromIndex(indexes.at(i)));
2621  return items;
2622 }
2623 
2636 {
2637  invisibleRootItem()->appendRow(items);
2638 }
2639 
2652 {
2653  invisibleRootItem()->appendColumn(items);
2654 }
2655 
2677 {
2678  invisibleRootItem()->insertRow(row, items);
2679 }
2680 
2705 {
2706  invisibleRootItem()->insertColumn(column, items);
2707 }
2708 
2721 {
2723  return d->root->takeChild(row, column);
2724 }
2725 
2740 {
2742  return d->root->takeRow(row);
2743 }
2744 
2759 {
2761  return d->root->takeColumn(column);
2762 }
2763 
2777 {
2779  if ((column < 0) || (column >= columnCount()))
2780  return 0;
2781  QStandardItem *headerItem = d->columnHeaderItems.at(column);
2782  if (headerItem) {
2783  headerItem->d_func()->setParentAndModel(0, 0);
2784  d->columnHeaderItems.replace(column, 0);
2785  }
2786  return headerItem;
2787 }
2788 
2802 {
2804  if ((row < 0) || (row >= rowCount()))
2805  return 0;
2806  QStandardItem *headerItem = d->rowHeaderItems.at(row);
2807  if (headerItem) {
2808  headerItem->d_func()->setParentAndModel(0, 0);
2809  d->rowHeaderItems.replace(row, 0);
2810  }
2811  return headerItem;
2812 }
2813 
2826 int QStandardItemModel::sortRole() const
2827 {
2828  Q_D(const QStandardItemModel);
2829  return d->sortRole;
2830 }
2831 
2833 {
2835  d->sortRole = role;
2836 }
2837 
2842 {
2843  Q_D(const QStandardItemModel);
2844  QStandardItem *item = d->itemFromIndex(parent);
2845  return item ? item->columnCount() : 0;
2846 }
2847 
2852 {
2853  Q_D(const QStandardItemModel);
2854  QStandardItem *item = d->itemFromIndex(index);
2855  return item ? item->data(role) : QVariant();
2856 }
2857 
2861 Qt::ItemFlags QStandardItemModel::flags(const QModelIndex &index) const
2862 {
2863  Q_D(const QStandardItemModel);
2864  if (!d->indexValid(index))
2865  return d->root->flags();
2866  QStandardItem *item = d->itemFromIndex(index);
2867  if (item)
2868  return item->flags();
2869  return Qt::ItemIsSelectable
2874 }
2875 
2880 {
2881  Q_D(const QStandardItemModel);
2882  QStandardItem *item = d->itemFromIndex(parent);
2883  return item ? item->hasChildren() : false;
2884 }
2885 
2889 QVariant QStandardItemModel::headerData(int section, Qt::Orientation orientation, int role) const
2890 {
2891  Q_D(const QStandardItemModel);
2892  if ((section < 0)
2893  || ((orientation == Qt::Horizontal) && (section >= columnCount()))
2894  || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
2895  return QVariant();
2896  }
2897  QStandardItem *headerItem = 0;
2898  if (orientation == Qt::Horizontal)
2899  headerItem = d->columnHeaderItems.at(section);
2900  else if (orientation == Qt::Vertical)
2901  headerItem = d->rowHeaderItems.at(section);
2902  return headerItem ? headerItem->data(role)
2903  : QAbstractItemModel::headerData(section, orientation, role);
2904 }
2905 
2915 {
2916  return Qt::CopyAction | Qt::MoveAction;
2917 }
2918 
2922 QModelIndex QStandardItemModel::index(int row, int column, const QModelIndex &parent) const
2923 {
2924  Q_D(const QStandardItemModel);
2925  QStandardItem *parentItem = d->itemFromIndex(parent);
2926  if ((parentItem == 0)
2927  || (row < 0)
2928  || (column < 0)
2929  || (row >= parentItem->rowCount())
2930  || (column >= parentItem->columnCount())) {
2931  return QModelIndex();
2932  }
2933  return createIndex(row, column, parentItem);
2934 }
2935 
2939 bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent)
2940 {
2942  QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2943  if (item == 0)
2944  return false;
2945  return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
2946 }
2947 
2951 bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent)
2952 {
2954  QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2955  if (item == 0)
2956  return false;
2957  return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
2958 }
2959 
2964 {
2965  Q_D(const QStandardItemModel);
2966  QStandardItem *item = d->itemFromIndex(index);
2967  return item ? item->d_func()->itemData() : QMap<int, QVariant>();
2968 }
2969 
2974 {
2975  Q_D(const QStandardItemModel);
2976  if (!d->indexValid(child))
2977  return QModelIndex();
2978  QStandardItem *parentItem = static_cast<QStandardItem*>(child.internalPointer());
2979  return indexFromItem(parentItem);
2980 }
2981 
2985 bool QStandardItemModel::removeColumns(int column, int count, const QModelIndex &parent)
2986 {
2988  QStandardItem *item = d->itemFromIndex(parent);
2989  if ((item == 0) || (count < 1) || (column < 0) || ((column + count) > item->columnCount()))
2990  return false;
2991  item->removeColumns(column, count);
2992  return true;
2993 }
2994 
2998 bool QStandardItemModel::removeRows(int row, int count, const QModelIndex &parent)
2999 {
3001  QStandardItem *item = d->itemFromIndex(parent);
3002  if ((item == 0) || (count < 1) || (row < 0) || ((row + count) > item->rowCount()))
3003  return false;
3004  item->removeRows(row, count);
3005  return true;
3006 }
3007 
3012 {
3013  Q_D(const QStandardItemModel);
3014  QStandardItem *item = d->itemFromIndex(parent);
3015  return item ? item->rowCount() : 0;
3016 }
3017 
3021 bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
3022 {
3023  if (!index.isValid())
3024  return false;
3025  QStandardItem *item = itemFromIndex(index);
3026  if (item == 0)
3027  return false;
3028  item->setData(value, role);
3029  return true;
3030 }
3031 
3035 bool QStandardItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
3036 {
3038  if ((section < 0)
3039  || ((orientation == Qt::Horizontal) && (section >= columnCount()))
3040  || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
3041  return false;
3042  }
3043  QStandardItem *headerItem = 0;
3044  if (orientation == Qt::Horizontal) {
3045  headerItem = d->columnHeaderItems.at(section);
3046  if (headerItem == 0) {
3047  headerItem = d->createItem();
3048  headerItem->d_func()->setModel(this);
3049  d->columnHeaderItems.replace(section, headerItem);
3050  }
3051  } else if (orientation == Qt::Vertical) {
3052  headerItem = d->rowHeaderItems.at(section);
3053  if (headerItem == 0) {
3054  headerItem = d->createItem();
3055  headerItem->d_func()->setModel(this);
3056  d->rowHeaderItems.replace(section, headerItem);
3057  }
3058  }
3059  if (headerItem) {
3060  headerItem->setData(value, role);
3061  return true;
3062  }
3063  return false;
3064 }
3065 
3070 {
3071  QStandardItem *item = itemFromIndex(index);
3072  if (item == 0)
3073  return false;
3074  item->d_func()->setItemData(roles);
3075  return true;
3076 }
3077 
3082 {
3084  d->root->sortChildren(column, order);
3085 }
3086 
3097 {
3098  return QAbstractItemModel::mimeTypes() << QLatin1String("application/x-qstandarditemmodeldatalist");
3099 }
3100 
3105 {
3107  if(!data)
3108  return 0;
3109 
3110  QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
3111  if (!mimeTypes().contains(format))
3112  return data;
3113  QByteArray encoded;
3115 
3116  QSet<QStandardItem*> itemsSet;
3117  QStack<QStandardItem*> stack;
3118  itemsSet.reserve(indexes.count());
3119  stack.reserve(indexes.count());
3120  for (int i = 0; i < indexes.count(); ++i) {
3121  QStandardItem *item = itemFromIndex(indexes.at(i));
3122  itemsSet << item;
3123  stack.push(item);
3124  }
3125 
3126  //remove duplicates childrens
3127  {
3128  QSet<QStandardItem *> seen;
3129  while (!stack.isEmpty()) {
3130  QStandardItem *itm = stack.pop();
3131  if (seen.contains(itm))
3132  continue;
3133  seen.insert(itm);
3134 
3135  const QVector<QStandardItem*> &childList = itm->d_func()->children;
3136  for (int i = 0; i < childList.count(); ++i) {
3137  QStandardItem *chi = childList.at(i);
3138  if (chi) {
3139  QSet<QStandardItem *>::iterator it = itemsSet.find(chi);
3140  if (it != itemsSet.end()) {
3141  itemsSet.erase(it);
3142  }
3143  stack.push(chi);
3144  }
3145  }
3146  }
3147  }
3148 
3149  stack.reserve(itemsSet.count());
3150  foreach (QStandardItem *item, itemsSet) {
3151  stack.push(item);
3152  }
3153 
3154  //stream everything recursively
3155  while (!stack.isEmpty()) {
3156  QStandardItem *item = stack.pop();
3157  if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
3158  stream << item->row() << item->column();
3159  }
3160  if(item) {
3161  stream << *item << item->columnCount() << item->d_ptr->children.count();
3162  stack += item->d_ptr->children;
3163  } else {
3164  QStandardItem dummy;
3165  stream << dummy << 0 << 0;
3166  }
3167  }
3168 
3169  data->setData(format, encoded);
3170  return data;
3171 }
3172 
3173 
3174 /* \internal
3175  Used by QStandardItemModel::dropMimeData
3176  stream out an item and his children
3177  */
3179 {
3180  int colCount, childCount;
3181  stream >> *item;
3182  stream >> colCount >> childCount;
3183  item->setColumnCount(colCount);
3184 
3185  int childPos = childCount;
3186 
3187  while(childPos > 0) {
3188  childPos--;
3189  QStandardItem *child = createItem();
3190  decodeDataRecursive(stream, child);
3191  item->setChild( childPos / colCount, childPos % colCount, child);
3192  }
3193 }
3194 
3195 
3200  int row, int column, const QModelIndex &parent)
3201 {
3203  // check if the action is supported
3204  if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
3205  return false;
3206  // check if the format is supported
3207  QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
3208  if (!data->hasFormat(format))
3209  return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
3210 
3211  if (row > rowCount(parent))
3212  row = rowCount(parent);
3213  if (row == -1)
3214  row = rowCount(parent);
3215  if (column == -1)
3216  column = 0;
3217 
3218  // decode and insert
3219  QByteArray encoded = data->data(format);
3221 
3222 
3223  //code based on QAbstractItemModel::decodeData
3224  // adapted to work with QStandardItem
3225  int top = INT_MAX;
3226  int left = INT_MAX;
3227  int bottom = 0;
3228  int right = 0;
3229  QVector<int> rows, columns;
3231 
3232  while (!stream.atEnd()) {
3233  int r, c;
3234  QStandardItem *item = d->createItem();
3235  stream >> r >> c;
3236  d->decodeDataRecursive(stream, item);
3237 
3238  rows.append(r);
3239  columns.append(c);
3240  items.append(item);
3241  top = qMin(r, top);
3242  left = qMin(c, left);
3243  bottom = qMax(r, bottom);
3244  right = qMax(c, right);
3245  }
3246 
3247  // insert the dragged items into the table, use a bit array to avoid overwriting items,
3248  // since items from different tables can have the same row and column
3249  int dragRowCount = 0;
3250  int dragColumnCount = right - left + 1;
3251 
3252  // Compute the number of continuous rows upon insertion and modify the rows to match
3253  QVector<int> rowsToInsert(bottom + 1);
3254  for (int i = 0; i < rows.count(); ++i)
3255  rowsToInsert[rows.at(i)] = 1;
3256  for (int i = 0; i < rowsToInsert.count(); ++i) {
3257  if (rowsToInsert[i] == 1){
3258  rowsToInsert[i] = dragRowCount;
3259  ++dragRowCount;
3260  }
3261  }
3262  for (int i = 0; i < rows.count(); ++i)
3263  rows[i] = top + rowsToInsert[rows[i]];
3264 
3265  QBitArray isWrittenTo(dragRowCount * dragColumnCount);
3266 
3267  // make space in the table for the dropped data
3268  int colCount = columnCount(parent);
3269  if (colCount < dragColumnCount + column) {
3270  insertColumns(colCount, dragColumnCount + column - colCount, parent);
3271  colCount = columnCount(parent);
3272  }
3273  insertRows(row, dragRowCount, parent);
3274 
3275  row = qMax(0, row);
3276  column = qMax(0, column);
3277 
3278  QStandardItem *parentItem = itemFromIndex (parent);
3279  if (!parentItem)
3280  parentItem = invisibleRootItem();
3281 
3282  QVector<QPersistentModelIndex> newIndexes(items.size());
3283  // set the data in the table
3284  for (int j = 0; j < items.size(); ++j) {
3285  int relativeRow = rows.at(j) - top;
3286  int relativeColumn = columns.at(j) - left;
3287  int destinationRow = relativeRow + row;
3288  int destinationColumn = relativeColumn + column;
3289  int flat = (relativeRow * dragColumnCount) + relativeColumn;
3290  // if the item was already written to, or we just can't fit it in the table, create a new row
3291  if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
3292  destinationColumn = qBound(column, destinationColumn, colCount - 1);
3293  destinationRow = row + dragRowCount;
3294  insertRows(row + dragRowCount, 1, parent);
3295  flat = (dragRowCount * dragColumnCount) + relativeColumn;
3296  isWrittenTo.resize(++dragRowCount * dragColumnCount);
3297  }
3298  if (!isWrittenTo.testBit(flat)) {
3299  newIndexes[j] = index(destinationRow, destinationColumn, parentItem->index());
3300  isWrittenTo.setBit(flat);
3301  }
3302  }
3303 
3304  for(int k = 0; k < newIndexes.size(); k++) {
3305  if (newIndexes.at(k).isValid()) {
3306  parentItem->setChild(newIndexes.at(k).row(), newIndexes.at(k).column(), items.at(k));
3307  } else {
3308  delete items.at(k);
3309  }
3310  }
3311 
3312  return true;
3313 }
3314 
3316 
3317 #include "moc_qstandarditemmodel.cpp"
3318 
3319 #endif // QT_NO_STANDARDITEMMODEL
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QStandardItem * verticalHeaderItem(int row) const
Returns the vertical header item for row row if one has been set; otherwise returns 0...
double d
Definition: qnumeric_p.h:62
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
Returns a list of indexes for the items in the column of the start index where data stored under the ...
QMimeData * mimeData(const QModelIndexList &indexes) const
Reimplemented Function
bool isCheckable() const
Returns whether the item is user-checkable.
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
Definition: qmimedata.cpp:547
QStandardItem * horizontalHeaderItem(int column) const
Returns the horizontal header item for column if one has been set; otherwise returns 0...
QStandardItem * takeHorizontalHeaderItem(int column)
Removes the horizontal header item at column from the header without deleting it, and returns a point...
void removeRows(int row, int count)
Removes count rows at row row.
virtual ~QStandardItem()
Destructs the item.
QScopedPointer< QStandardItem > root
void appendRow(const QList< QStandardItem *> &items)
Appends a row containing items.
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
void setBit(int i)
Sets the bit at index position i to 1.
Definition: qbitarray.h:128
const QMap< int, QVariant > itemData() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
virtual bool operator<(const QStandardItem &other) const
Returns true if this item is less than other; otherwise returns false.
void removeColumns(int column, int count)
Removes count columns at column column.
void setTristate(bool tristate)
Sets whether the item is tristate.
void rowsAboutToBeInserted(QStandardItem *parent, int start, int end)
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
T * data() const
Returns the value of the pointer referenced by this object.
void setVerticalHeaderItem(int row, QStandardItem *item)
Sets the vertical header item for row to item.
QIcon icon() const
Returns the item&#39;s icon.
#define it(className, varName)
QStandardItemModel(QObject *parent=0)
Constructs a new item model with the given parent.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
~QStandardItemModel()
Destructs the model.
int columnCount() const
Returns the number of child item columns that the item has.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
Reimplemented Function
bool atEnd() const
Returns true if the I/O device has reached the end position (end of the stream or file) or if there i...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
#define SLOT(a)
Definition: qobjectdefs.h:226
T1 first
Definition: qpair.h:65
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:252
T2 second
Definition: qpair.h:66
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
void setModel(QStandardItemModel *mod)
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void setVerticalHeaderLabels(const QStringList &labels)
Sets the vertical header labels using labels.
void setRowCount(int rows)
Sets the number of child item rows to rows.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
Qt::DropActions supportedDropActions() const
QStandardItemModel supports both copy and move.
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
int row() const
Returns the row where the item is located in its parent&#39;s child table, or -1 if the item has no paren...
void columnsAboutToBeRemoved(QStandardItem *parent, int start, int end)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
const QStandardItem * itemPrototype
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
QStandardItem & operator=(const QStandardItem &other)
Assigns other&#39;s data and flags to this item.
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
const Key & key() const
Returns the current item&#39;s key.
Definition: qmap.h:324
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool hasChildren() const
Returns true if this item has any children; otherwise returns false.
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
The QStandardItemModel class provides a generic model for storing custom data.
void insertRow(int row, const QList< QStandardItem *> &items)
Inserts a row at row containing items.
virtual int type() const
Returns the type of this item.
QVector< QStandardItem * > children
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
void insertRow(int row, const QList< QStandardItem *> &items)
Inserts a row at row containing items.
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setChild(int row, int column, QStandardItem *item)
Sets the child item at (row, column) to item.
QList< QStandardItem * > takeColumn(int column)
Removes column without deleting the column items, and returns a list of pointers to the removed items...
bool testBit(int i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition: qbitarray.h:124
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder)
Reimplemented Function
QObjectList children
Definition: qobject.h:93
void rowsAboutToBeRemoved(QStandardItem *parent, int start, int end)
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void setIcon(const QIcon &icon)
Sets the item&#39;s icon to the icon specified.
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void appendColumn(const QList< QStandardItem *> &items)
Appends a column containing items.
bool operator()(const QPair< QStandardItem *, int > &l, const QPair< QStandardItem *, int > &r) const
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void setHorizontalHeaderItem(int column, QStandardItem *item)
Sets the horizontal header item for column to item.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:250
#define Q_Q(Class)
Definition: qglobal.h:2483
QStandardItem * takeVerticalHeaderItem(int row)
Removes the vertical header item at row from the header without deleting it, and returns a pointer to...
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
virtual QStandardItem * clone() const
Returns a copy of this item.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:249
void setItem(int row, int column, QStandardItem *item)
Sets the item for the given row and column to item.
SortOrder
Definition: qnamespace.h:189
void columnsRemoved(QStandardItem *parent, int column, int count)
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QStandardItem * > takeRow(int row)
Removes the given row without deleting the row items, and returns a list of pointers to the removed i...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
QPair< int, int > position() const
QVector< QStandardItem * > columnHeaderItems
static FILE * stream
void setEnabled(bool enabled)
Sets whether the item is enabled.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
int column() const
Returns the column where the item is located in its parent&#39;s child table, or -1 if the item has no pa...
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
bool contains(const T &value) const
Definition: qset.h:91
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void childDeleted(QStandardItem *child)
bool operator()(const QPair< QStandardItem *, int > &l, const QPair< QStandardItem *, int > &r) const
bool insertColumns(int column, int count, const QList< QStandardItem *> &items)
Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented Function
int row() const
Returns the row this model index refers to.
int sortRole
the item role that is used to query the model&#39;s data when sorting items
void prepend(const T &t)
Inserts value at the beginning of the list.
Definition: qlist.h:541
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QString text() const
Returns the item&#39;s text.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented Function
void resize(int size)
Resizes the bit array to size bits.
Definition: qbitarray.cpp:214
iterator end()
Definition: qset.h:169
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
Reimplemented Function
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
QStandardItem * takeItem(int row, int column=0)
Removes the item at (row, column) without deleting it.
bool insertRows(int row, int count, const QList< QStandardItem *> &items)
qulonglong toULongLong(bool *ok=0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong ...
Definition: qvariant.cpp:2675
Q_CORE_EXPORT void qWarning(const char *,...)
const_iterator insert(const T &value)
Definition: qset.h:179
void setSelectable(bool selectable)
Sets whether the item is selectable.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void setDragEnabled(bool dragEnabled)
Sets whether the item is drag enabled.
const QStandardItem * itemPrototype() const
Returns the item prototype used by the model.
int count() const
Definition: qset.h:178
QStandardItem()
Constructs an item.
void setDropEnabled(bool dropEnabled)
Sets whether the item is drop enabled.
DropAction
Definition: qnamespace.h:1597
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
quint16 values[128]
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qlist.h:609
QModelIndex index() const
Returns the QModelIndex associated with this item.
void _q_emitItemChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QStringList mimeTypes() const
Reimplemented Function
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
virtual void read(QDataStream &in)
Reads the item from stream in.
void setFlags(Qt::ItemFlags flags)
Sets the item flags for the item to flags.
The QAbstractItemModel class provides the abstract interface for item model classes.
QList< QStandardItem * > findItems(const QString &text, Qt::MatchFlags flags=Qt::MatchExactly, int column=0) const
Returns a list of items that match the given text, using the given flags, in the given column...
void appendRow(const QList< QStandardItem *> &items)
Appends a row containing items.
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
iterator begin()
Returns an STL-style iterator pointing to the first item in the map.
Definition: qmap.h:372
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
void columnsAboutToBeInserted(QStandardItem *parent, int start, int end)
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
virtual bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qmimedata.cpp:563
int rowCount() const
Returns the number of child item rows that the item has.
QDate toDate() const
Returns the variant as a QDate if the variant has type() Date , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2311
void setCheckable(bool checkable)
Sets whether the item is user-checkable.
QDataStream & operator<<(QDataStream &out, const QStandardItem &item)
Writes the QStandardItem item to stream out.
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
void removeRow(int row)
Removes the given row.
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
QVector< QStandardItem * > rowHeaderItems
int compare(const QString &s) const
Definition: qstring.cpp:5037
void itemChanged(QStandardItem *item)
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
void appendColumn(const QList< QStandardItem *> &items)
Appends a column containing items.
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void setItemPrototype(const QStandardItem *item)
Sets the item prototype for the model to the specified item.
QDataStream & operator>>(QDataStream &in, QStandardItem &item)
Reads a QStandardItem from stream in into item.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:375
void sortChildren(int column, Qt::SortOrder order=Qt::AscendingOrder)
Sorts the children of the item using the given order, by the values in the given column.
void rowsRemoved(QStandardItem *parent, int row, int count)
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
virtual QVariant data(int role=Qt::UserRole+1) const
Returns the item&#39;s data for the given role, or an invalid QVariant if there is no data for the role...
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
void insertColumn(int column, const QList< QStandardItem *> &items)
Inserts a column at column containing items.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QModelIndex indexFromItem(const QStandardItem *item) const
Returns the QModelIndex associated with the given item.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
The QModelIndex class is used to locate data in a data model.
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
Qt::ItemFlags flags() const
Returns the item flags for the item.
float toFloat(bool *ok=0) const
Returns the variant as a float if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2725
QList< QStandardItem * > takeRow(int row)
Removes row without deleting the row items, and returns a list of pointers to the removed items...
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Reimplemented Function
QMap< int, QVariant > itemData(const QModelIndex &index) const
Reimplemented Function
void rowsInserted(QStandardItem *parent, int row, int count)
Definition: qnamespace.h:54
QFactoryLoader * l
const T & value() const
Returns the current item&#39;s value.
Definition: qmap.h:325
void setChild(int row, int column, QStandardItem *item, bool emitChanged=false)
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Reimplemented Function
void sortChildren(int column, Qt::SortOrder order)
void changeFlags(bool enable, Qt::ItemFlags f)
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
void setItemData(const QMap< int, QVariant > &roles)
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
QStandardItemModel * model() const
Returns the QStandardItemModel that this item belongs to.
quint16 index
QObject * parent
Definition: qobject.h:92
void setColumnCount(int columns)
Sets the number of columns in this model to columns.
void setColumnCount(int columns)
Sets the number of child item columns to columns.
const T * const_iterator
The QVector::const_iterator typedef provides an STL-style const iterator for QVector and QStack...
Definition: qvector.h:245
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
void setEditable(bool editable)
Sets whether the item is editable.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QTime toTime() const
Returns the variant as a QTime if the variant has type() Time , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2330
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
The QStandardItem class provides an item for use with the QStandardItemModel class.
void setText(const QString &text)
Sets the item&#39;s text to the text specified.
QList< QStandardItem * > takeColumn(int column)
Removes the given column without deleting the column items, and returns a list of pointers to the rem...
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
void setHorizontalHeaderLabels(const QStringList &labels)
Sets the horizontal header labels using labels.
static const KeyPair *const end
iterator erase(iterator i)
Definition: qset.h:172
void clear()
Removes all items (including header items) from the model and sets the number of rows and columns to ...
void removeColumn(int column)
Removes the given column.
Orientation
Definition: qnamespace.h:174
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
QChar toChar() const
Returns the variant as a QChar if the variant has type() Char , Int , or UInt ; otherwise returns an ...
Definition: qvariant.cpp:2579
void insertColumn(int column, const QList< QStandardItem *> &items)
Inserts a column at column containing items.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
virtual void setData(const QVariant &value, int role=Qt::UserRole+1)
Sets the item&#39;s data for the given role to the specified value.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
void decodeDataRecursive(QDataStream &stream, QStandardItem *item)
void insertColumns(int column, int count)
Inserts count columns of child items at column column.
void setRowCount(int rows)
Sets the number of rows in this model to rows.
#define INT_MAX
QScopedPointer< QStandardItemPrivate > d_ptr
void insertRows(int row, const QList< QStandardItem *> &items)
Inserts items at row.
#define enabled
iterator find(const T &value)
Definition: qset.h:182
void columnsInserted(QStandardItem *parent, int column, int count)
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
virtual void write(QDataStream &out) const
Writes the item to stream out.
QStandardItem * itemFromIndex(const QModelIndex &index) const
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
void reset()
Resets the model to its original state in any attached views.
#define text
Definition: qobjectdefs.h:80
int column() const
Returns the column this model index refers to.
QStandardItem * child(int row, int column=0) const
Returns the child item at (row, column) if one has been set; otherwise returns 0. ...
int sortRole() const
QStandardItem * takeChild(int row, int column=0)
Removes the child item at (row, column) without deleting it, and returns a pointer to the item...
QStandardItem * parent() const
Returns the item&#39;s parent item, or 0 if the item has no parent.
void emitDataChanged()
Causes the model associated with this item to emit a dataChanged() signal for this item...
void reserve(int size)
Definition: qset.h:241
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60