Qt 4.8
qtreewidget.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 "qtreewidget.h"
43 
44 #ifndef QT_NO_TREEWIDGET
45 #include <qheaderview.h>
46 #include <qpainter.h>
47 #include <qitemdelegate.h>
48 #include <qstack.h>
49 #include <qdebug.h>
50 #include <private/qtreewidget_p.h>
51 #include <private/qwidgetitemdata_p.h>
52 #include <private/qtreewidgetitemiterator_p.h>
53 
55 
56 // workaround for VC++ 6.0 linker bug (?)
58 
60 {
61 public:
62  inline bool operator()(QTreeWidgetItem *i1, QTreeWidgetItem *i2) const
63  { return *i1 < *i2; }
64 };
65 
67 {
68 public:
69  inline bool operator()(QTreeWidgetItem *i1, QTreeWidgetItem *i2) const
70  { return *i2 < *i1; }
71 };
72 
73 /*
74  \class QTreeModel
75  \brief The QTreeModel class manages the items stored in a tree view.
76 
77  \ingroup model-view
78 
79 */
80 
124 QTreeModel::QTreeModel(int columns, QTreeWidget *parent)
125  : QAbstractItemModel(parent), rootItem(new QTreeWidgetItem),
126  headerItem(new QTreeWidgetItem), skipPendingSort(false)
127 {
128  rootItem->view = parent;
131  setColumnCount(columns);
132 }
133 
140  : QAbstractItemModel(dd, parent), rootItem(new QTreeWidgetItem),
142 {
143  rootItem->view = parent;
146 }
147 
158 {
159  clear();
160  delete headerItem;
161  rootItem->view = 0;
162  delete rootItem;
163 }
164 
175 {
176  SkipSorting skipSorting(this);
177  for (int i = 0; i < rootItem->childCount(); ++i) {
179  item->par = 0;
180  item->view = 0;
181  delete item;
182  }
185  reset();
186 }
187 
197 void QTreeModel::setColumnCount(int columns)
198 {
199  SkipSorting skipSorting(this);
200  if (columns < 0)
201  return;
202  if (!headerItem) {
203  headerItem = new QTreeWidgetItem();
204  headerItem->view = view();
205  }
206  int count = columnCount();
207  if (count == columns)
208  return;
209 
210  if (columns < count) {
211  beginRemoveColumns(QModelIndex(), columns, count - 1);
212  headerItem->values.resize(columns);
214  } else {
215  beginInsertColumns(QModelIndex(), count, columns - 1);
216  headerItem->values.resize(columns);
217  for (int i = count; i < columns; ++i) {// insert data without emitting the dataChanged signal
220  }
222  }
223 }
224 
237 {
238  if (!index.isValid())
239  return 0;
240  return static_cast<QTreeWidgetItem*>(index.internalPointer());
241 }
242 
254 {
256 
257  if (!item || (item == rootItem))
258  return QModelIndex();
259  const QTreeWidgetItem *par = item->parent();
260  QTreeWidgetItem *itm = const_cast<QTreeWidgetItem*>(item);
261  if (!par)
262  par = rootItem;
263  int row;
264  int guess = item->d->rowGuess;
265  if (guess >= 0
266  && par->children.count() > guess
267  && par->children.at(guess) == itm) {
268  row = guess;
269  } else {
270  row = par->children.lastIndexOf(itm);
271  itm->d->rowGuess = row;
272  }
273  return createIndex(row, column, itm);
274 }
275 
287 QModelIndex QTreeModel::index(int row, int column, const QModelIndex &parent) const
288 {
290 
291  int c = columnCount(parent);
292  if (row < 0 || column < 0 || column >= c)
293  return QModelIndex();
294 
295  QTreeWidgetItem *parentItem = parent.isValid() ? item(parent) : rootItem;
296  if (parentItem && row < parentItem->childCount()) {
297  QTreeWidgetItem *itm = parentItem->child(row);
298  if (itm)
299  return createIndex(row, column, itm);
300  return QModelIndex();
301  }
302 
303  return QModelIndex();
304 }
305 
318 {
319  SkipSorting skipSorting(this); //The reason we don't sort here is that this might be called from a valid QPersistentModelIndex
320  //We don't want it to become suddenly invalid
321 
322  if (!child.isValid())
323  return QModelIndex();
324  QTreeWidgetItem *itm = static_cast<QTreeWidgetItem *>(child.internalPointer());
325  if (!itm || itm == rootItem)
326  return QModelIndex();
327  QTreeWidgetItem *parent = itm->parent();
328  return index(parent, 0);
329 }
330 
342 {
343  if (!parent.isValid())
344  return rootItem->childCount();
345 
346  QTreeWidgetItem *parentItem = item(parent);
347  if (parentItem)
348  return parentItem->childCount();
349  return 0;
350 }
351 
364 {
365  Q_UNUSED(index);
366  if (!headerItem)
367  return 0;
368  return headerItem->columnCount();
369 }
370 
372 {
373  if (!parent.isValid())
374  return (rootItem->childCount() > 0);
375 
376  QTreeWidgetItem *itm = item(parent);
377  if (!itm)
378  return false;
379  switch (itm->d->policy) {
381  return true;
383  return false;
385  return (itm->childCount() > 0);
386  }
387  return false;
388 }
389 
402 {
403  if (!index.isValid())
404  return QVariant();
405  QTreeWidgetItem *itm = item(index);
406  if (itm)
407  return itm->data(index.column(), role);
408  return QVariant();
409 }
410 
424 bool QTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
425 {
426  if (!index.isValid())
427  return false;
428  QTreeWidgetItem *itm = item(index);
429  if (itm) {
430  itm->setData(index.column(), role, value);
431  return true;
432  }
433  return false;
434 }
435 
437 {
438  QMap<int, QVariant> roles;
439  QTreeWidgetItem *itm = item(index);
440  if (itm) {
441  int column = index.column();
442  if (column < itm->values.count()) {
443  for (int i = 0; i < itm->values.at(column).count(); ++i) {
444  roles.insert(itm->values.at(column).at(i).role,
445  itm->values.at(column).at(i).value);
446  }
447  }
448 
449  // the two special cases
450  QVariant displayValue = itm->data(column, Qt::DisplayRole);
451  if (displayValue.isValid())
452  roles.insert(Qt::DisplayRole, displayValue);
453 
454  QVariant checkValue = itm->data(column, Qt::CheckStateRole);
455  if (checkValue.isValid())
456  roles.insert(Qt::CheckStateRole, checkValue);
457  }
458  return roles;
459 }
460 
465 bool QTreeModel::insertRows(int row, int count, const QModelIndex &parent)
466 {
467  SkipSorting skipSorting(this);
468  if (count < 1 || row < 0 || row > rowCount(parent) || parent.column() > 0)
469  return false;
470 
471  beginInsertRows(parent, row, row + count - 1);
472  QTreeWidgetItem *par = item(parent);
473  while (count > 0) {
475  item->view = view();
476  item->par = par;
477  if (par)
478  par->children.insert(row++, item);
479  else
480  rootItem->children.insert(row++, item);
481  --count;
482  }
483  endInsertRows();
484  return true;
485 }
486 
491 bool QTreeModel::insertColumns(int column, int count, const QModelIndex &parent)
492 {
493  SkipSorting skipSorting(this);
494  if (count < 1 || column < 0 || column > columnCount(parent) || parent.column() > 0 || !headerItem)
495  return false;
496 
497  beginInsertColumns(parent, column, column + count - 1);
498 
499  int oldCount = columnCount(parent);
500  column = qBound(0, column, oldCount);
501  headerItem->values.resize(oldCount + count);
502  for (int i = oldCount; i < oldCount + count; ++i) {
505  }
506 
507  QStack<QTreeWidgetItem*> itemstack;
508  itemstack.push(0);
509  while (!itemstack.isEmpty()) {
510  QTreeWidgetItem *par = itemstack.pop();
512  for (int row = 0; row < children.count(); ++row) {
513  QTreeWidgetItem *child = children.at(row);
514  if (child->children.count())
515  itemstack.push(child);
516  child->values.insert(column, count, QVector<QWidgetItemData>());
517  }
518  }
519 
521  return true;
522 }
523 
528 bool QTreeModel::removeRows(int row, int count, const QModelIndex &parent) {
529  if (count < 1 || row < 0 || (row + count) > rowCount(parent))
530  return false;
531 
532  beginRemoveRows(parent, row, row + count - 1);
533 
534  bool blockSignal = signalsBlocked();
535  blockSignals(true);
536 
537  QTreeWidgetItem *itm = item(parent);
538  for (int i = row + count - 1; i >= row; --i) {
539  QTreeWidgetItem *child = itm ? itm->takeChild(i) : rootItem->children.takeAt(i);
540  Q_ASSERT(child);
541  child->view = 0;
542  delete child;
543  child = 0;
544  }
545  blockSignals(blockSignal);
546 
547  endRemoveRows();
548  return true;
549 }
550 
562 QVariant QTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
563 {
564  if (orientation != Qt::Horizontal)
565  return QVariant();
566 
567  if (headerItem)
568  return headerItem->data(section, role);
569  if (role == Qt::DisplayRole)
570  return QString::number(section + 1);
571  return QVariant();
572 }
573 
587 bool QTreeModel::setHeaderData(int section, Qt::Orientation orientation,
588  const QVariant &value, int role)
589 {
590  if (section < 0 || orientation != Qt::Horizontal || !headerItem || section >= columnCount())
591  return false;
592 
593  headerItem->setData(section, role, value);
594  return true;
595 }
596 
607 Qt::ItemFlags QTreeModel::flags(const QModelIndex &index) const
608 {
609  if (!index.isValid())
610  return rootItem->flags();
611  QTreeWidgetItem *itm = item(index);
612  Q_ASSERT(itm);
613  return itm->flags();
614 }
615 
626 void QTreeModel::sort(int column, Qt::SortOrder order)
627 {
628  SkipSorting skipSorting(this);
630 
631  if (column < 0 || column >= columnCount())
632  return;
633 
634  //layoutAboutToBeChanged and layoutChanged will be called by sortChildren
635  rootItem->sortChildren(column, order, true);
636 }
637 
642  int start, int end, const QModelIndex &parent)
643 {
644  if (isChanging())
645  return;
646 
648 
649  if (column < 0 || column >= columnCount())
650  return;
651 
652  SkipSorting skipSorting(this);
653 
654  QTreeWidgetItem *itm = item(parent);
655  if (!itm)
656  itm = rootItem;
658 
659  int count = end - start + 1;
660  QVector < QPair<QTreeWidgetItem*,int> > sorting(count);
661  for (int i = 0; i < count; ++i) {
662  sorting[i].first = lst.at(start + i);
663  sorting[i].second = start + i;
664  }
665 
667  qStableSort(sorting.begin(), sorting.end(), compare);
668 
669  QModelIndexList oldPersistentIndexes;
670  QModelIndexList newPersistentIndexes;
672  bool changed = false;
673 
674  for (int i = 0; i < count; ++i) {
675  int oldRow = sorting.at(i).second;
676  QTreeWidgetItem *item = lst.takeAt(oldRow);
677  lit = sortedInsertionIterator(lit, lst.end(), order, item);
678  int newRow = qMax(lit - lst.begin(), 0);
679 
680  if ((newRow < oldRow) && !(*item < *lst.at(oldRow - 1)) && !(*lst.at(oldRow - 1) < *item ))
681  newRow = oldRow;
682 
683  lit = lst.insert(lit, item);
684  if (newRow != oldRow) {
685  // we are going to change the persistent indexes, so we need to prepare
686  if (!changed) { // this will only happen once
687  changed = true;
688  emit layoutAboutToBeChanged(); // the selection model needs to know
689  oldPersistentIndexes = persistentIndexList();
690  newPersistentIndexes = oldPersistentIndexes;
691  }
692  for (int j = i + 1; j < count; ++j) {
693  int otherRow = sorting.at(j).second;
694  if (oldRow < otherRow && newRow >= otherRow)
695  --sorting[j].second;
696  else if (oldRow > otherRow && newRow <= otherRow)
697  ++sorting[j].second;
698  }
699  for (int k = 0; k < newPersistentIndexes.count(); ++k) {
700  QModelIndex pi = newPersistentIndexes.at(k);
701  if (pi.parent() != parent)
702  continue;
703  int oldPersistentRow = pi.row();
704  int newPersistentRow = oldPersistentRow;
705  if (oldPersistentRow == oldRow)
706  newPersistentRow = newRow;
707  else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
708  newPersistentRow = oldPersistentRow - 1;
709  else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
710  newPersistentRow = oldPersistentRow + 1;
711  if (newPersistentRow != oldPersistentRow)
712  newPersistentIndexes[k] = createIndex(newPersistentRow,
713  pi.column(), pi.internalPointer());
714  }
715  }
716  }
717 
718  if (changed) {
719  itm->children = lst;
720  changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
722  }
723 }
724 
739 {
740  return *(left.first) < *(right.first);
741 }
742 
757 {
758  return *(right.first) < *(left.first);
759 }
760 
768 {
769  if (order == Qt::AscendingOrder)
770  return qLowerBound(begin, end, item, QTreeModelLessThan());
771  return qLowerBound(begin, end, item, QTreeModelGreaterThan());
772 }
773 
775 {
776  return view()->mimeTypes();
777 }
778 
780 {
782 }
783 
785 {
787  for (int i = 0; i < indexes.count(); ++i) {
788  if (indexes.at(i).column() == 0) // only one item per row
789  items << item(indexes.at(i));
790  }
791 
792  // cachedIndexes is a little hack to avoid copying from QModelIndexList to
793  // QList<QTreeWidgetItem*> and back again in the view
794  cachedIndexes = indexes;
795  QMimeData *mimeData = view()->mimeData(items);
797  return mimeData;
798 }
799 
801  int row, int column, const QModelIndex &parent)
802 {
803  if (row == -1 && column == -1)
804  row = rowCount(parent); // append
805  return view()->dropMimeData(item(parent), row, data, action);
806 }
807 
808 Qt::DropActions QTreeModel::supportedDropActions() const
809 {
810  return view()->supportedDropActions();
811 }
812 
814 {
815  SkipSorting skipSorting(this); //this is kind of wrong, but not doing this would kill performence
816  QModelIndex left = index(item, 0);
817  QModelIndex right = index(item, item->columnCount() - 1);
818  emit dataChanged(left, right);
819 }
820 
822 {
823  Q_D(const QTreeModel);
824  return !d->changes.isEmpty();
825 }
826 
837 {
838  if (signalsBlocked())
839  return;
840 
841  if (headerItem == item && column < item->columnCount()) {
842  if (column == -1)
844  else
845  emit headerDataChanged(Qt::Horizontal, column, column);
846  return;
847  }
848 
849  SkipSorting skipSorting(this); //This is a little bit wrong, but not doing it would kill performence
850 
851  QModelIndex bottomRight, topLeft;
852  if (column == -1) {
853  topLeft = index(item, 0);
854  bottomRight = createIndex(topLeft.row(), columnCount() - 1, item);
855  } else {
856  topLeft = index(item, column);
857  bottomRight = topLeft;
858  }
859  emit dataChanged(topLeft, bottomRight);
860 }
861 
863 {
864  QModelIndex par = index(parent, 0);
865  beginInsertRows(par, row, row + count - 1);
866 }
867 
869 {
870  endInsertRows();
871 }
872 
874 {
875  Q_ASSERT(row >= 0);
876  Q_ASSERT(count > 0);
877  beginRemoveRows(index(parent, 0), row, row + count - 1);
878  if (!parent)
879  parent = rootItem;
880  // now update the iterators
881  for (int i = 0; i < iterators.count(); ++i) {
882  for (int j = 0; j < count; j++) {
883  QTreeWidgetItem *c = parent->child(row + j);
884  iterators[i]->d_func()->ensureValidIterator(c);
885  }
886  }
887 }
888 
890 {
891  endRemoveRows();
892 }
893 
895 {
896  // see QTreeViewItem::operator<
897  Q_UNUSED(column);
898  if (isChanging())
899  return;
900 
901  // store the original order of indexes
902  QVector< QPair<QTreeWidgetItem*,int> > sorting(items->count());
903  for (int i = 0; i < sorting.count(); ++i) {
904  sorting[i].first = items->at(i);
905  sorting[i].second = i;
906  }
907 
908  // do the sorting
910  qStableSort(sorting.begin(), sorting.end(), compare);
911 
912  QModelIndexList fromList;
914  int colCount = columnCount();
915  for (int r = 0; r < sorting.count(); ++r) {
916  int oldRow = sorting.at(r).second;
917  if (oldRow == r)
918  continue;
919  QTreeWidgetItem *item = sorting.at(r).first;
920  items->replace(r, item);
921  for (int c = 0; c < colCount; ++c) {
922  QModelIndex from = createIndex(oldRow, c, item);
923  if (static_cast<QAbstractItemModelPrivate *>(d_ptr.data())->persistent.indexes.contains(from)) {
924  QModelIndex to = createIndex(r, c, item);
925  fromList << from;
926  toList << to;
927  }
928  }
929  }
930  changePersistentIndexList(fromList, toList);
931 }
932 
934 {
935  if (ev->timerId() == sortPendingTimer.timerId()) {
937  } else {
939  }
940 }
941 
1490  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1491  itemFlags(Qt::ItemIsSelectable
1493  |Qt::ItemIsEnabled
1496 {
1497 }
1498 
1499 
1509  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1512  |Qt::ItemIsEnabled
1515 {
1516  for (int i = 0; i < strings.count(); ++i)
1517  setText(i, strings.at(i));
1518 }
1519 
1530  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1533  |Qt::ItemIsEnabled
1536 {
1537  if (view && view->model()) {
1538  QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
1539  model->rootItem->addChild(this);
1540  values.reserve(model->headerItem->columnCount());
1541  }
1542 }
1543 
1555  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1558  |Qt::ItemIsEnabled
1561 {
1562  for (int i = 0; i < strings.count(); ++i)
1563  setText(i, strings.at(i));
1564  if (view && view->model()) {
1565  QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
1566  model->rootItem->addChild(this);
1567  values.reserve(model->headerItem->columnCount());
1568  }
1569 }
1570 
1580  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1583  |Qt::ItemIsEnabled
1586 {
1587  if (view) {
1588  QTreeModel *model = qobject_cast<QTreeModel*>(view->model());
1589  if (model) {
1590  int i = model->rootItem->children.indexOf(after) + 1;
1591  model->rootItem->insertChild(i, this);
1592  values.reserve(model->headerItem->columnCount());
1593  }
1594  }
1595 }
1596 
1603  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1606  |Qt::ItemIsEnabled
1609 {
1610  if (parent)
1611  parent->addChild(this);
1612 }
1613 
1621  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1624  |Qt::ItemIsEnabled
1627 {
1628  for (int i = 0; i < strings.count(); ++i)
1629  setText(i, strings.at(i));
1630  if (parent)
1631  parent->addChild(this);
1632 }
1633 
1643  : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0),
1646  |Qt::ItemIsEnabled
1649 {
1650  if (parent) {
1651  int i = parent->children.indexOf(after) + 1;
1652  parent->insertChild(i, this);
1653  }
1654 }
1655 
1665 {
1666  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
1667  bool wasSkipSort = false;
1668  if (model) {
1669  wasSkipSort = model->skipPendingSort;
1670  model->skipPendingSort = true;
1671  }
1672  if (par) {
1673  int i = par->children.indexOf(this);
1674  if (i >= 0) {
1675  if (model) model->beginRemoveItems(par, i, 1);
1676  // users _could_ do changes when connected to rowsAboutToBeRemoved,
1677  // so we check again to make sure 'i' is valid
1678  if (!par->children.isEmpty() && par->children.at(i) == this)
1679  par->children.takeAt(i);
1680  if (model) model->endRemoveItems();
1681  }
1682  } else if (model) {
1683  if (this == model->headerItem) {
1684  model->headerItem = 0;
1685  } else {
1686  int i = model->rootItem->children.indexOf(this);
1687  if (i >= 0) {
1688  model->beginRemoveItems(0, i, 1);
1689  // users _could_ do changes when connected to rowsAboutToBeRemoved,
1690  // so we check again to make sure 'i' is valid
1691  if (!model->rootItem->children.isEmpty() && model->rootItem->children.at(i) == this)
1692  model->rootItem->children.takeAt(i);
1693  model->endRemoveItems();
1694  }
1695  }
1696  }
1697  // at this point the persistent indexes for the children should also be invalidated
1698  // since we invalidated the parent
1699  for (int i = 0; i < children.count(); ++i) {
1701  // make sure the child does not try to remove itself from our children list
1702  child->par = 0;
1703  // make sure the child does not try to remove itself from the top level list
1704  child->view = 0;
1705  delete child;
1706  }
1707 
1708  children.clear();
1709  delete d;
1710  if (model) {
1711  model->skipPendingSort = wasSkipSort;
1712  }
1713 }
1714 
1719 {
1720  QTreeWidgetItem *copy = 0;
1721 
1723  QStack<QTreeWidgetItem*> parentStack;
1724  stack.push(this);
1725  parentStack.push(0);
1726 
1727  QTreeWidgetItem *root = 0;
1728  const QTreeWidgetItem *item = 0;
1729  QTreeWidgetItem *parent = 0;
1730  while (!stack.isEmpty()) {
1731  // get current item, and copied parent
1732  item = stack.pop();
1733  parent = parentStack.pop();
1734 
1735  // copy item
1736  copy = new QTreeWidgetItem(*item);
1737  if (!root)
1738  root = copy;
1739 
1740  // set parent and add to parents children list
1741  if (parent) {
1742  copy->par = parent;
1743  parent->children.insert(0, copy);
1744  }
1745 
1746  for (int i = 0; i < item->childCount(); ++i) {
1747  stack.push(item->child(i));
1748  parentStack.push(copy);
1749  }
1750  }
1751  return root;
1752 }
1753 
1762 {
1763  if (d->policy == policy)
1764  return;
1765  d->policy = policy;
1766 
1767  if (!view)
1768  return;
1769 
1771 }
1772 
1780 {
1781  return d->policy;
1782 }
1783 
1796 {
1797  const bool enable = (flags & Qt::ItemIsEnabled);
1798  const bool changedState = bool(itemFlags & Qt::ItemIsEnabled) != enable;
1799  const bool changedExplicit = d->disabled != !enable;
1800 
1801  d->disabled = !enable;
1802 
1803  if (enable && par && !(par->itemFlags & Qt::ItemIsEnabled)) // inherit from parent
1804  itemFlags = flags & ~Qt::ItemIsEnabled;
1805  else // this item is explicitly disabled or has no parent
1806  itemFlags = flags;
1807 
1808  if (changedState && changedExplicit) { // if the propagate the change to the children
1809  QStack<QTreeWidgetItem*> parents;
1810  parents.push(this);
1811  while (!parents.isEmpty()) {
1812  QTreeWidgetItem *parent = parents.pop();
1813  for (int i = 0; i < parent->children.count(); ++i) {
1814  QTreeWidgetItem *child = parent->children.at(i);
1815  if (!child->d->disabled) { // if not explicitly disabled
1816  parents.push(child);
1817  if (enable)
1818  child->itemFlags = child->itemFlags | Qt::ItemIsEnabled;
1819  else
1820  child->itemFlags = child->itemFlags & ~Qt::ItemIsEnabled;
1821  child->itemChanged(); // ### we may want to optimize this
1822  }
1823  }
1824  }
1825  }
1826  itemChanged();
1827 }
1828 
1830 {
1831  Q_ASSERT(item);
1832  const bool enable = item->par ? (item->par->itemFlags.testFlag(Qt::ItemIsEnabled)) : true;
1833 
1834  QStack<QTreeWidgetItem*> parents;
1835  parents.push(item);
1836  while (!parents.isEmpty()) {
1837  QTreeWidgetItem *parent = parents.pop();
1838  if (!parent->d->disabled) { // if not explicitly disabled
1839  Qt::ItemFlags oldFlags = parent->itemFlags;
1840  if (enable)
1841  parent->itemFlags = parent->itemFlags | Qt::ItemIsEnabled;
1842  else
1843  parent->itemFlags = parent->itemFlags & ~Qt::ItemIsEnabled;
1844  if (parent->itemFlags != oldFlags)
1845  parent->itemChanged();
1846  }
1847 
1848  for (int i = 0; i < parent->children.count(); ++i) {
1849  QTreeWidgetItem *child = parent->children.at(i);
1850  parents.push(child);
1851  }
1852  }
1853 }
1869 Qt::ItemFlags QTreeWidgetItem::flags() const
1870 {
1871  return itemFlags;
1872 }
1873 
1881 void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
1882 {
1883  if (column < 0)
1884  return;
1885 
1886  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
1887  switch (role) {
1888  case Qt::EditRole:
1889  case Qt::DisplayRole: {
1890  if (values.count() <= column) {
1891  if (model && this == model->headerItem)
1892  model->setColumnCount(column + 1);
1893  else
1894  values.resize(column + 1);
1895  }
1896  if (d->display.count() <= column) {
1897  for (int i = d->display.count() - 1; i < column - 1; ++i)
1898  d->display.append(QVariant());
1899  d->display.append(value);
1900  } else if (d->display[column] != value) {
1901  d->display[column] = value;
1902  } else {
1903  return; // value is unchanged
1904  }
1905  } break;
1906  case Qt::CheckStateRole:
1907  if (itemFlags & Qt::ItemIsTristate) {
1908  for (int i = 0; i < children.count(); ++i) {
1910  if (child->data(column, role).isValid()) {// has a CheckState
1911  Qt::ItemFlags f = itemFlags; // a little hack to avoid multiple dataChanged signals
1913  child->setData(column, role, value);
1914  itemFlags = f;
1915  }
1916  }
1917  }
1918  // Don't break, but fall through
1919  default:
1920  if (column < values.count()) {
1921  bool found = false;
1922  QVector<QWidgetItemData> column_values = values.at(column);
1923  for (int i = 0; i < column_values.count(); ++i) {
1924  if (column_values.at(i).role == role) {
1925  if (column_values.at(i).value == value)
1926  return; // value is unchanged
1927  values[column][i].value = value;
1928  found = true;
1929  break;
1930  }
1931  }
1932  if (!found)
1933  values[column].append(QWidgetItemData(role, value));
1934  } else {
1935  if (model && this == model->headerItem)
1936  model->setColumnCount(column + 1);
1937  else
1938  values.resize(column + 1);
1939  values[column].append(QWidgetItemData(role, value));
1940  }
1941  }
1942 
1943  if (model) {
1944  model->emitDataChanged(this, column);
1945  if (role == Qt::CheckStateRole) {
1946  QTreeWidgetItem *p;
1947  for (p = par; p && (p->itemFlags & Qt::ItemIsTristate); p = p->par)
1948  model->emitDataChanged(p, column);
1949  }
1950  }
1951 }
1952 
1956 QVariant QTreeWidgetItem::data(int column, int role) const
1957 {
1958  switch (role) {
1959  case Qt::EditRole:
1960  case Qt::DisplayRole:
1961  if (column >= 0 && column < d->display.count())
1962  return d->display.at(column);
1963  break;
1964  case Qt::CheckStateRole:
1965  // special case for check state in tristate
1967  return childrenCheckState(column);
1968  // fallthrough intended
1969  default:
1970  if (column >= 0 && column < values.size()) {
1971  const QVector<QWidgetItemData> &column_values = values.at(column);
1972  for (int i = 0; i < column_values.count(); ++i)
1973  if (column_values.at(i).role == role)
1974  return column_values.at(i).value;
1975  }
1976  }
1977  return QVariant();
1978 }
1979 
1986 {
1987  int column = view ? view->sortColumn() : 0;
1988  const QVariant v1 = data(column, Qt::DisplayRole);
1989  const QVariant v2 = other.data(column, Qt::DisplayRole);
1991 }
1992 
1993 #ifndef QT_NO_DATASTREAM
1994 
2001 {
2002  // convert from streams written before we introduced display (4.2.0)
2003  if (in.version() < QDataStream::Qt_4_2) {
2004  d->display.clear();
2005  in >> values;
2006  // move the display value over to the display string list
2007  for (int column = 0; column < values.count(); ++column) {
2008  d->display << QVariant();
2009  for (int i = 0; i < values.at(column).count(); ++i) {
2010  if (values.at(column).at(i).role == Qt::DisplayRole) {
2011  d->display[column] = values.at(column).at(i).value;
2012  values[column].remove(i--);
2013  }
2014  }
2015  }
2016  } else {
2017  in >> values >> d->display;
2018  }
2019 }
2020 
2027 {
2028  out << values << d->display;
2029 }
2030 #endif // QT_NO_DATASTREAM
2031 
2046  : rtti(Type), values(other.values), view(0),
2047  d(new QTreeWidgetItemPrivate(this)), par(0),
2048  itemFlags(other.itemFlags)
2049 {
2050  d->display = other.d->display;
2051 }
2052 
2062 {
2063  values = other.values;
2064  d->display = other.d->display;
2065  d->policy = other.d->policy;
2066  itemFlags = other.itemFlags;
2067  return *this;
2068 }
2069 
2076 {
2077  if (child) {
2079  child->d->rowGuess = children.count() - 1;
2080  }
2081 }
2082 
2089 {
2090  if (index < 0 || index > children.count() || child == 0 || child->view != 0 || child->par != 0)
2091  return;
2092 
2093  if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0)) {
2094  const bool wasSkipSort = model->skipPendingSort;
2095  model->skipPendingSort = true;
2096  if (model->rootItem == this)
2097  child->par = 0;
2098  else
2099  child->par = this;
2100  if (view->isSortingEnabled()) {
2101  // do a delayed sort instead
2102  if (!model->sortPendingTimer.isActive())
2103  model->sortPendingTimer.start(0, model);
2104  }
2105  model->beginInsertItems(this, index, 1);
2106  int cols = model->columnCount();
2108  stack.push(child);
2109  while (!stack.isEmpty()) {
2110  QTreeWidgetItem *i = stack.pop();
2111  i->view = view;
2112  i->values.reserve(cols);
2113  for (int c = 0; c < i->children.count(); ++c)
2114  stack.push(i->children.at(c));
2115  }
2116  children.insert(index, child);
2117  model->endInsertItems();
2118  model->skipPendingSort = wasSkipSort;
2119  } else {
2120  child->par = this;
2121  children.insert(index, child);
2122  }
2123  if (child->par)
2124  d->propagateDisabled(child);
2125 }
2126 
2132 {
2133  (void)takeChild(children.indexOf(child));
2134 }
2135 
2140 {
2141  // we move this outside the check of the index to allow executing
2142  // pending sorts from inline functions, using this function (hack)
2143  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
2144  if (model) {
2145  // This will trigger a layoutChanged signal, thus we might want to optimize
2146  // this function by not emitting the rowsRemoved signal etc to the view.
2147  // On the other hand we also need to make sure that the selectionmodel
2148  // is updated in case we take an item that is selected.
2149  model->skipPendingSort = false;
2150  model->executePendingSort();
2151  }
2152  if (index >= 0 && index < children.count()) {
2153  if (model) model->beginRemoveItems(this, index, 1);
2154  QTreeWidgetItem *item = children.takeAt(index);
2155  item->par = 0;
2157  stack.push(item);
2158  while (!stack.isEmpty()) {
2159  QTreeWidgetItem *i = stack.pop();
2160  i->view = 0;
2161  for (int c = 0; c < i->children.count(); ++c)
2162  stack.push(i->children.at(c));
2163  }
2164  d->propagateDisabled(item);
2165  if (model) model->endRemoveRows();
2166  return item;
2167  }
2168  return 0;
2169 }
2170 
2182 {
2183  insertChildren(this->children.count(), children);
2184 }
2185 
2197 {
2198  if (view && view->isSortingEnabled()) {
2199  for (int n = 0; n < children.count(); ++n)
2200  insertChild(index, children.at(n));
2201  return;
2202  }
2203  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
2205  QList<QTreeWidgetItem*> itemsToInsert;
2206  for (int n = 0; n < children.count(); ++n) {
2207  QTreeWidgetItem *child = children.at(n);
2208  if (child->view || child->par)
2209  continue;
2210  itemsToInsert.append(child);
2211  if (view && model) {
2212  if (child->childCount() == 0)
2213  child->view = view;
2214  else
2215  stack.push(child);
2216  }
2217  if (model && (model->rootItem == this))
2218  child->par = 0;
2219  else
2220  child->par = this;
2221  }
2222  if (!itemsToInsert.isEmpty()) {
2223  while (!stack.isEmpty()) {
2224  QTreeWidgetItem *i = stack.pop();
2225  i->view = view;
2226  for (int c = 0; c < i->children.count(); ++c)
2227  stack.push(i->children.at(c));
2228  }
2229  if (model) model->beginInsertItems(this, index, itemsToInsert.count());
2230  for (int n = 0; n < itemsToInsert.count(); ++n) {
2231  QTreeWidgetItem *child = itemsToInsert.at(n);
2232  this->children.insert(index + n, child);
2233  if (child->par)
2234  d->propagateDisabled(child);
2235  }
2236  if (model) model->endInsertItems();
2237  }
2238 }
2239 
2249 {
2250  QList<QTreeWidgetItem*> removed;
2251  if (children.count() > 0) {
2252  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
2253  if (model) {
2254  // This will trigger a layoutChanged signal, thus we might want to optimize
2255  // this function by not emitting the rowsRemoved signal etc to the view.
2256  // On the other hand we also need to make sure that the selectionmodel
2257  // is updated in case we take an item that is selected.
2258  model->executePendingSort();
2259  }
2260  if (model) model->beginRemoveItems(this, 0, children.count());
2261  for (int n = 0; n < children.count(); ++n) {
2262  QTreeWidgetItem *item = children.at(n);
2263  item->par = 0;
2265  stack.push(item);
2266  while (!stack.isEmpty()) {
2267  QTreeWidgetItem *i = stack.pop();
2268  i->view = 0;
2269  for (int c = 0; c < i->children.count(); ++c)
2270  stack.push(i->children.at(c));
2271  }
2272  d->propagateDisabled(item);
2273  }
2274  removed = children;
2275  children.clear(); // detach
2276  if (model) model->endRemoveItems();
2277  }
2278  return removed;
2279 }
2280 
2281 
2282 void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool climb)
2283 {
2284  QTreeModel *model = (q->view ? qobject_cast<QTreeModel*>(q->view->model()) : 0);
2285  if (!model)
2286  return;
2287  model->sortItems(&q->children, column, order);
2288  if (climb) {
2289  QList<QTreeWidgetItem*>::iterator it = q->children.begin();
2290  for (; it != q->children.end(); ++it) {
2291  //here we call the private object's method to avoid emitting
2292  //the layoutAboutToBeChanged and layoutChanged signals
2293  (*it)->d->sortChildren(column, order, climb);
2294  }
2295  }
2296 }
2297 
2308 void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order, bool climb)
2309 {
2310  QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0);
2311  if (!model)
2312  return;
2313  if (model->isChanging())
2314  return;
2315  QTreeModel::SkipSorting skipSorting(model);
2316  int oldSortColumn = view->d_func()->explicitSortColumn;
2317  view->d_func()->explicitSortColumn = column;
2318  emit model->layoutAboutToBeChanged();
2319  d->sortChildren(column, order, climb);
2320  emit model->layoutChanged();
2321  view->d_func()->explicitSortColumn = oldSortColumn;
2322 }
2323 
2336 {
2337  if (column < 0)
2338  return QVariant();
2339  bool checkedChildren = false;
2340  bool uncheckedChildren = false;
2341  for (int i = 0; i < children.count(); ++i) {
2342  QVariant value = children.at(i)->data(column, Qt::CheckStateRole);
2343  if (!value.isValid())
2344  return QVariant();
2345 
2346  switch (static_cast<Qt::CheckState>(value.toInt()))
2347  {
2348  case Qt::Unchecked:
2349  uncheckedChildren = true;
2350  break;
2351  case Qt::Checked:
2352  checkedChildren = true;
2353  break;
2354  case Qt::PartiallyChecked:
2355  default:
2356  return Qt::PartiallyChecked;
2357  }
2358  }
2359 
2360  if (uncheckedChildren && checkedChildren)
2361  return Qt::PartiallyChecked;
2362  if (uncheckedChildren)
2363  return Qt::Unchecked;
2364  else if (checkedChildren)
2365  return Qt::Checked;
2366  else
2367  return QVariant(); // value was not defined
2368 }
2369 
2386 {
2387  itemChanged();
2388 }
2389 
2394 {
2395  if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0))
2396  model->itemChanged(this);
2397 }
2398 
2403 {
2404  if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0))
2405  model->executePendingSort();
2406 }
2407 
2408 
2409 #ifndef QT_NO_DATASTREAM
2410 
2423 {
2424  item.write(out);
2425  return out;
2426 }
2427 
2441 {
2442  item.read(in);
2443  return in;
2444 }
2445 #endif // QT_NO_DATASTREAM
2446 
2447 
2449 {
2450  Q_Q(QTreeWidget);
2451  emit q->itemPressed(item(index), index.column());
2452 }
2453 
2455 {
2456  Q_Q(QTreeWidget);
2457  emit q->itemClicked(item(index), index.column());
2458 }
2459 
2461 {
2462  Q_Q(QTreeWidget);
2463  emit q->itemDoubleClicked(item(index), index.column());
2464 }
2465 
2467 {
2468  Q_Q(QTreeWidget);
2469  emit q->itemActivated(item(index), index.column());
2470 }
2471 
2473 {
2474  Q_Q(QTreeWidget);
2475  emit q->itemEntered(item(index), index.column());
2476 }
2477 
2479 {
2480  Q_Q(QTreeWidget);
2481  QTreeWidgetItem *indexItem = item(index);
2482  if (indexItem)
2483  emit q->itemChanged(indexItem, index.column());
2484 }
2485 
2487 {
2488  Q_Q(QTreeWidget);
2489  emit q->itemExpanded(item(index));
2490 }
2491 
2493 {
2494  Q_Q(QTreeWidget);
2495  emit q->itemCollapsed(item(index));
2496 }
2497 
2499  const QModelIndex &previous)
2500 {
2501  Q_Q(QTreeWidget);
2502  QTreeWidgetItem *currentItem = item(current);
2503  QTreeWidgetItem *previousItem = item(previous);
2504  emit q->currentItemChanged(currentItem, previousItem);
2505 }
2506 
2508 {
2509  if (sortingEnabled) {
2510  int column = header->sortIndicatorSection();
2511  Qt::SortOrder order = header->sortIndicatorOrder();
2512  treeModel()->sort(column, order);
2513  }
2514 }
2515 
2517 {
2518  Q_Q(QTreeWidget);
2519  QModelIndexList indices = selected.indexes();
2520  int i;
2521  QTreeModel *m = treeModel();
2522  for (i = 0; i < indices.count(); ++i) {
2523  QTreeWidgetItem *item = m->item(indices.at(i));
2524  item->d->selected = true;
2525  }
2526 
2527  indices = deselected.indexes();
2528  for (i = 0; i < indices.count(); ++i) {
2529  QTreeWidgetItem *item = m->item(indices.at(i));
2530  item->d->selected = false;
2531  }
2532 
2533  emit q->itemSelectionChanged();
2534 }
2535 
2537  const QModelIndex &bottomRight)
2538 {
2539  if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()
2540  && !treeModel()->sortPendingTimer.isActive()) {
2541  int column = header->sortIndicatorSection();
2542  if (column >= topLeft.column() && column <= bottomRight.column()) {
2543  Qt::SortOrder order = header->sortIndicatorOrder();
2544  treeModel()->ensureSorted(column, order, topLeft.row(),
2545  bottomRight.row(), topLeft.parent());
2546  }
2547  }
2548 }
2549 
2734  : QTreeView(*new QTreeWidgetPrivate(), parent)
2735 {
2736  QTreeView::setModel(new QTreeModel(1, this));
2738  SLOT(_q_emitItemPressed(QModelIndex)));
2740  SLOT(_q_emitItemClicked(QModelIndex)));
2742  SLOT(_q_emitItemDoubleClicked(QModelIndex)));
2744  SLOT(_q_emitItemActivated(QModelIndex)));
2746  SLOT(_q_emitItemEntered(QModelIndex)));
2748  SLOT(_q_emitItemExpanded(QModelIndex)));
2750  SLOT(_q_emitItemCollapsed(QModelIndex)));
2752  this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
2754  this, SLOT(_q_emitItemChanged(QModelIndex)));
2756  this, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
2757  connect(model(), SIGNAL(columnsRemoved(QModelIndex,int,int)),
2758  this, SLOT(_q_sort()));
2760  this, SLOT(_q_selectionChanged(QItemSelection,QItemSelection)));
2761  header()->setClickable(false);
2762 }
2763 
2769 {
2770 }
2771 
2772 /*
2773  Retuns the number of header columns in the view.
2774 
2775  \sa sortColumn(), currentColumn(), topLevelItemCount()
2776 */
2777 
2778 int QTreeWidget::columnCount() const
2779 {
2780  Q_D(const QTreeWidget);
2781  return d->model->columnCount();
2782 }
2783 
2784 /*
2785  Sets the number of header \a columns in the tree widget.
2786 */
2787 
2789 {
2790  Q_D(QTreeWidget);
2791  if (columns < 0)
2792  return;
2793  d->treeModel()->setColumnCount(columns);
2794 }
2795 
2811 {
2812  Q_D(const QTreeWidget);
2813  return d->treeModel()->rootItem;
2814 }
2815 
2824 {
2825  Q_D(const QTreeWidget);
2826  return d->treeModel()->rootItem->child(index);
2827 }
2828 
2842 {
2843  Q_D(const QTreeWidget);
2844  return d->treeModel()->rootItem->childCount();
2845 }
2846 
2856 {
2857  Q_D(QTreeWidget);
2858  d->treeModel()->rootItem->insertChild(index, item);
2859 }
2860 
2872 {
2874 }
2875 
2884 {
2885  Q_D(QTreeWidget);
2886  return d->treeModel()->rootItem->takeChild(index);
2887 }
2888 
2893 {
2894  Q_D(QTreeWidget);
2895  d->treeModel()->executePendingSort();
2896  return d->treeModel()->rootItem->children.indexOf(item);
2897 }
2898 
2906 {
2907  Q_D(const QTreeWidget);
2908  d->treeModel()->executePendingSort();
2909  return d->treeModel()->rootItem->children.indexOf(item);
2910 }
2911 
2925 {
2926  Q_D(QTreeWidget);
2927  d->treeModel()->rootItem->insertChildren(index, items);
2928 }
2929 
2936 {
2938 }
2939 
2947 {
2948  Q_D(const QTreeWidget);
2949  return d->treeModel()->headerItem;
2950 }
2951 
2962 {
2963  Q_D(QTreeWidget);
2964  if (!item)
2965  return;
2966  item->view = this;
2967 
2968  int oldCount = columnCount();
2969  if (oldCount < item->columnCount())
2970  d->treeModel()->beginInsertColumns(QModelIndex(), oldCount, item->columnCount());
2971  else
2972  d->treeModel()->beginRemoveColumns(QModelIndex(), item->columnCount(), oldCount);
2973  delete d->treeModel()->headerItem;
2974  d->treeModel()->headerItem = item;
2975  if (oldCount < item->columnCount())
2976  d->treeModel()->endInsertColumns();
2977  else
2978  d->treeModel()->endRemoveColumns();
2979  d->treeModel()->headerDataChanged(Qt::Horizontal, 0, oldCount);
2980 }
2981 
2982 
2992 {
2993  Q_D(QTreeWidget);
2994  if (columnCount() < labels.count())
2995  setColumnCount(labels.count());
2996  QTreeWidgetItem *item = d->treeModel()->headerItem;
2997  for (int i = 0; i < labels.count(); ++i)
2998  item->setText(i, labels.at(i));
2999 }
3000 
3017 {
3018  Q_D(const QTreeWidget);
3019  return d->item(currentIndex());
3020 }
3021 
3032 {
3033  return currentIndex().column();
3034 }
3035 
3045 {
3046  setCurrentItem(item, 0);
3047 }
3048 
3059 {
3060  Q_D(QTreeWidget);
3061  setCurrentIndex(d->index(item, column));
3062 }
3063 
3075  QItemSelectionModel::SelectionFlags command)
3076 {
3077  Q_D(QTreeWidget);
3078  d->selectionModel->setCurrentIndex(d->index(item, column), command);
3079 }
3080 
3081 
3089 {
3090  Q_D(const QTreeWidget);
3091  return d->item(indexAt(p));
3092 }
3093 
3108 {
3109  Q_D(const QTreeWidget);
3110  //the visual rect for an item is across all columns. So we need to determine
3111  //what is the first and last column and get their visual index rects
3112  QModelIndex base = d->index(item);
3113  const int firstVisiblesection = header()->logicalIndexAt(- header()->offset());
3114  const int lastVisibleSection = header()->logicalIndexAt(header()->length() - header()->offset() - 1);
3115  QModelIndex first = base.sibling(base.row(), header()->logicalIndex(firstVisiblesection));
3116  QModelIndex last = base.sibling(base.row(), header()->logicalIndex(lastVisibleSection));
3117  return visualRect(first) | visualRect(last);
3118 }
3119 
3131 {
3132  Q_D(const QTreeWidget);
3133  return (d->explicitSortColumn != -1
3134  ? d->explicitSortColumn
3135  : header()->sortIndicatorSection());
3136 }
3137 
3145 void QTreeWidget::sortItems(int column, Qt::SortOrder order)
3146 {
3147  Q_D(QTreeWidget);
3148  header()->setSortIndicator(column, order);
3149  d->model->sort(column, order);
3150 }
3151 
3158 {
3160 }
3161 
3168 {
3169  return QTreeView::isSortingEnabled();
3170 }
3171 
3177 {
3178  Q_D(QTreeWidget);
3179  edit(d->index(item, column));
3180 }
3181 
3189 {
3190  Q_D(QTreeWidget);
3191  QAbstractItemView::openPersistentEditor(d->index(item, column));
3192 }
3193 
3204 {
3205  Q_D(QTreeWidget);
3206  QAbstractItemView::closePersistentEditor(d->index(item, column));
3207 }
3208 
3221 {
3222  Q_D(const QTreeWidget);
3223  return QAbstractItemView::indexWidget(d->index(item, column));
3224 }
3225 
3253 {
3254  Q_D(QTreeWidget);
3255  QAbstractItemView::setIndexWidget(d->index(item, column), widget);
3256 }
3257 
3268 {
3269  if (!item)
3270  return false;
3271  return item->d->selected;
3272 }
3273 
3285 {
3286  Q_D(QTreeWidget);
3287 
3288  if (!item)
3289  return;
3290 
3291  selectionModel()->select(d->index(item), (select ? QItemSelectionModel::Select
3294  item->d->selected = select;
3295 }
3296 
3303 {
3304  Q_D(const QTreeWidget);
3307  items.reserve(indexes.count());
3309  seen.reserve(indexes.count());
3310  for (int i = 0; i < indexes.count(); ++i) {
3311  QTreeWidgetItem *item = d->item(indexes.at(i));
3312  if (isItemHidden(item) || seen.contains(item))
3313  continue;
3314  seen.insert(item);
3315  items.append(item);
3316  }
3317  return items;
3318 }
3319 
3323 QList<QTreeWidgetItem*> QTreeWidget::findItems(const QString &text, Qt::MatchFlags flags, int column) const
3324 {
3325  Q_D(const QTreeWidget);
3326  QModelIndexList indexes = d->model->match(model()->index(0, column, QModelIndex()),
3327  Qt::DisplayRole, text, -1, flags);
3329  for (int i = 0; i < indexes.size(); ++i)
3330  items.append(d->item(indexes.at(i)));
3331  return items;
3332 }
3333 
3342 {
3343  Q_D(const QTreeWidget);
3344  if (item == d->treeModel()->headerItem)
3345  return header()->isHidden();
3346  if (d->hiddenIndexes.isEmpty())
3347  return false;
3348  QTreeModel::SkipSorting skipSorting(d->treeModel());
3349  return d->isRowHidden(d->index(item));
3350 }
3351 
3362 {
3363  Q_D(QTreeWidget);
3364  if (item == d->treeModel()->headerItem) {
3365  header()->setHidden(hide);
3366  } else {
3367  const QModelIndex index = d->index(item);
3368  setRowHidden(index.row(), index.parent(), hide);
3369  }
3370 }
3371 
3382 {
3383  Q_D(const QTreeWidget);
3384  QTreeModel::SkipSorting skipSorting(d->treeModel());
3385  return isExpanded(d->index(item));
3386 }
3387 
3399 {
3400  Q_D(QTreeWidget);
3401  QTreeModel::SkipSorting skipSorting(d->treeModel());
3402  setExpanded(d->index(item), expand);
3403 }
3404 
3417 {
3418  Q_D(const QTreeWidget);
3419  if (item == d->treeModel()->headerItem)
3420  return false; // We can't set the header items to spanning
3421  const QModelIndex index = d->index(item);
3422  return isFirstColumnSpanned(index.row(), index.parent());
3423 }
3424 
3437 {
3438  Q_D(QTreeWidget);
3439  if (item == d->treeModel()->headerItem)
3440  return; // We can't set header items to spanning
3441  const QModelIndex index = d->index(item);
3442  setFirstColumnSpanned(index.row(), index.parent(), span);
3443 }
3444 
3454 {
3455  Q_D(const QTreeWidget);
3456  if (item == d->treeModel()->headerItem)
3457  return 0;
3458  const QModelIndex index = d->index(item);
3459  const QModelIndex above = indexAbove(index);
3460  return d->item(above);
3461 }
3462 
3472 {
3473  Q_D(const QTreeWidget);
3474  if (item == d->treeModel()->headerItem)
3475  return 0;
3476  const QModelIndex index = d->index(item);
3477  const QModelIndex below = indexBelow(index);
3478  return d->item(below);
3479 }
3480 
3485 {
3486  Q_D(QTreeWidget);
3487  QTreeView::setSelectionModel(selectionModel);
3488  QItemSelection newSelection = selectionModel->selection();
3489  if (!newSelection.isEmpty())
3490  d->_q_selectionChanged(newSelection, QItemSelection());
3491 }
3492 
3500 {
3501  Q_D(QTreeWidget);
3502  QTreeView::scrollTo(d->index(item), hint);
3503 }
3504 
3512 {
3513  Q_D(QTreeWidget);
3514  QTreeModel::SkipSorting skipSorting(d->treeModel());
3515  expand(d->index(item));
3516 }
3517 
3525 {
3526  Q_D(QTreeWidget);
3527  QTreeModel::SkipSorting skipSorting(d->treeModel());
3528  collapse(d->index(item));
3529 }
3530 
3541 {
3542  Q_D(QTreeWidget);
3543  selectionModel()->clear();
3544  d->treeModel()->clear();
3545 }
3546 
3554 {
3555  return model()->QAbstractItemModel::mimeTypes();
3556 }
3557 
3567 {
3568  Q_D(const QTreeWidget);
3569  if (d->treeModel()->cachedIndexes.isEmpty()) {
3570  QList<QModelIndex> indexes;
3571  for (int i = 0; i < items.count(); ++i) {
3572  QTreeWidgetItem *item = items.at(i);
3573  for (int c = 0; c < item->values.count(); ++c) {
3574  indexes << indexFromItem(item, c);
3575  }
3576  }
3577  return d->model->QAbstractItemModel::mimeData(indexes);
3578  }
3579  return d->treeModel()->internalMimeData();
3580 }
3581 
3593  const QMimeData *data, Qt::DropAction action)
3594 {
3595  QModelIndex idx;
3596  if (parent) idx = indexFromItem(parent);
3597  return model()->QAbstractItemModel::dropMimeData(data, action , index, 0, idx);
3598 }
3599 
3605 Qt::DropActions QTreeWidget::supportedDropActions() const
3606 {
3607  return model()->QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
3608 }
3609 
3617 {
3618  Q_UNUSED(data);
3619  return QList<QTreeWidgetItem*>();
3620 }
3621 
3628 {
3629  Q_D(const QTreeWidget);
3630  return d->index(item, column);
3631 }
3632 
3639 {
3640  Q_D(const QTreeWidget);
3641  return d->item(index);
3642 }
3643 
3644 #ifndef QT_NO_DRAGANDDROP
3645 
3647  Q_D(QTreeWidget);
3648  if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
3650  QModelIndex topIndex;
3651  int col = -1;
3652  int row = -1;
3653  if (d->dropOn(event, &row, &col, &topIndex)) {
3656  for (int i = 0; i < idxs.count(); i++)
3657  indexes.append(idxs.at(i));
3658 
3659  if (indexes.contains(topIndex))
3660  return;
3661 
3662  // When removing items the drop location could shift
3663  QPersistentModelIndex dropRow = model()->index(row, col, topIndex);
3664 
3665  // Remove the items
3667  for (int i = indexes.count() - 1; i >= 0; --i) {
3668  QTreeWidgetItem *parent = itemFromIndex(indexes.at(i));
3669  if (!parent || !parent->parent()) {
3670  taken.append(takeTopLevelItem(indexes.at(i).row()));
3671  } else {
3672  taken.append(parent->parent()->takeChild(indexes.at(i).row()));
3673  }
3674  }
3675 
3676  // insert them back in at their new positions
3677  for (int i = 0; i < indexes.count(); ++i) {
3678  // Either at a specific point or appended
3679  if (row == -1) {
3680  if (topIndex.isValid()) {
3681  QTreeWidgetItem *parent = itemFromIndex(topIndex);
3682  parent->insertChild(parent->childCount(), taken.takeFirst());
3683  } else {
3685  }
3686  } else {
3687  int r = dropRow.row() >= 0 ? dropRow.row() : row;
3688  if (topIndex.isValid()) {
3689  QTreeWidgetItem *parent = itemFromIndex(topIndex);
3690  parent->insertChild(qMin(r, parent->childCount()), taken.takeFirst());
3691  } else {
3693  }
3694  }
3695  }
3696 
3697  event->accept();
3698  // Don't want QAbstractItemView to delete it because it was "moved" we already did it
3699  event->setDropAction(Qt::CopyAction);
3700  }
3701  }
3702 
3703  QTreeView::dropEvent(event);
3704 }
3705 #endif
3706 
3712 {
3713  Q_ASSERT(!"QTreeWidget::setModel() - Changing the model of the QTreeWidget is not allowed.");
3714 }
3715 
3720 {
3721  Q_D(QTreeWidget);
3722  if (e->type() == QEvent::Polish)
3723  d->treeModel()->executePendingSort();
3724  return QTreeView::event(e);
3725 }
3726 
3728 
3729 #include "moc_qtreewidget.cpp"
3730 
3731 #endif // QT_NO_TREEWIDGET
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget)
Sets the given widget to be displayed in the cell specified by the given item and column...
QList< QTreeWidgetItem * > items(const QMimeData *data) const
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
bool insertRows(int row, int count, const QModelIndex &)
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
void _q_emitItemCollapsed(const QModelIndex &index)
QTreeWidget(QWidget *parent=0)
Constructs a tree widget with the given parent.
QTreeWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the coordinates p.
QModelIndex index(const QTreeWidgetItem *item, int column) const
Returns the model index that refers to the tree view item and column.
int row() const
Returns the row this persistent model index refers to.
void openPersistentEditor(const QModelIndex &index)
Opens a persistent editor on the item at the given index.
bool blockSignals(bool b)
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke...
Definition: qobject.cpp:1406
The QItemSelectionModel class keeps track of a view&#39;s selected items.
int type
Definition: qmetatype.cpp:239
bool executePendingSort() const
QTreeModel(int columns=0, QTreeWidget *parent=0)
Constructs a tree model with a parent object and the given number of columns.
virtual void clear()
Clears the selection model.
unsigned char c[8]
Definition: qnumeric_p.h:62
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
QVariant childrenCheckState(int column) const
Calculates the checked state of the item based on the checked state of its children.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
bool isItemSelected(const QTreeWidgetItem *item) const
Returns true if the item is selected; otherwise returns false.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
void setRowHidden(int row, const QModelIndex &parent, bool hide)
If hide is true the row with the given parent is hidden, otherwise the row is shown.
Definition: qtreeview.cpp:622
bool isSortingEnabled() const
QPointer< QWidget > widget
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
Reimplemented Function
Definition: qtreeview.cpp:3821
virtual void setData(int column, int role, const QVariant &value)
Sets the value for the item&#39;s column and role to the given value.
void setText(int column, const QString &text)
Sets the text to be displayed in the given column to the given text.
Definition: qtreewidget.h:226
T * data() const
Returns the value of the pointer referenced by this object.
QModelIndexList selectedIndexes() const
Reimplemented Function
Definition: qtreeview.cpp:2427
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
void dropEvent(QDropEvent *event)
Reimplemented Function
Qt::ItemFlags flags() const
Returns the flags used to describe the item.
void collapseItem(const QTreeWidgetItem *item)
Closes the item.
void insertChild(int index, QTreeWidgetItem *child)
Inserts the child item at index in the list of children.
#define it(className, varName)
The QTreeWidget class provides a tree view that uses a predefined tree model.
Definition: qtreewidget.h:260
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Returns the header data corresponding to the given header section, orientation and data role...
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
void clicked(const QModelIndex &index)
This signal is emitted when a mouse button is clicked.
void sortChildren(int column, Qt::SortOrder order, bool climb)
void removeChild(QTreeWidgetItem *child)
Removes the given item indicated by child.
T & first()
Returns a reference to the first item in the vector.
Definition: qvector.h:260
QModelIndexList selectedIndexes() const
Returns a list of all selected model item indexes.
QTreeWidgetItem * item(const QModelIndex &index) const
Returns the tree view item corresponding to the index given.
void setItemHidden(const QTreeWidgetItem *item, bool hide)
Hides the given item if hide is true; otherwise shows the item.
#define SLOT(a)
Definition: qobjectdefs.h:226
int childCount() const
Returns the number of child items.
Definition: qtreewidget.h:190
void setSelectionModel(QItemSelectionModel *selectionModel)
Reimplemented Function
int topLevelItemCount() const
T1 first
Definition: qpair.h:65
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qobject.cpp:1294
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
DragDropMode dragDropMode() const
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
void setIndexWidget(const QModelIndex &index, QWidget *widget)
Sets the given widget on the item at the given index, passing the ownership of the widget to the view...
void setHeaderLabels(const QStringList &labels)
Adds a column in the header for each item in the labels list, and sets the label for each column...
const QItemSelection selection() const
Returns the selection ranges stored in the selection model.
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. ...
void _q_emitItemDoubleClicked(const QModelIndex &index)
QTreeWidgetItem & operator=(const QTreeWidgetItem &other)
Assigns other&#39;s data and flags to this item.
bool isFirstColumnSpanned(int row, const QModelIndex &parent) const
Returns true if the item in first column in the given row of the parent is spanning all the columns; ...
Definition: qtreeview.cpp:651
void insert(int i, const T &t)
Inserts value at index position i in the list.
Definition: qlist.h:575
QModelIndex indexBelow(const QModelIndex &index) const
Returns the model index of the item below index.
Definition: qtreeview.cpp:2060
void clear()
Clears the tree widget by removing all of its items and selections.
bool hasChildren(const QModelIndex &parent) const
Returns true if parent has any children; otherwise returns false.
void _q_emitItemClicked(const QModelIndex &index)
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
void emitDataChanged()
Causes the model associated with this item to emit a dataChanged() signal for this item...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void itemChanged(QTreeWidgetItem *item)
QTreeWidgetItem * itemBelow(const QTreeWidgetItem *item) const
Returns the item visually below the given item.
void setSelectionModel(QItemSelectionModel *selectionModel)
Reimplemented Function
Definition: qtreeview.cpp:277
void clear()
Removes all items in the model.
void endInsertRows()
Ends a row insertion operation.
void setModel(QAbstractItemModel *model)
Reimplemented Function
Definition: qtreeview.cpp:223
bool isExpanded(const QModelIndex &index) const
Returns true if the model item index is expanded; otherwise returns false.
Definition: qtreeview.cpp:859
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort.
QTreeWidgetItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QTreeWidgetItem assocated with the given index.
void editItem(QTreeWidgetItem *item, int column=0)
Starts editing the item in the given column if it is editable.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QTreeWidget * view
Definition: qtreewidget.h:219
virtual ~QTreeWidgetItem()
Destroys this tree widget item.
static QList< QVariant > toList(char **buf, int count, T *=0)
Definition: qsql_ibase.cpp:472
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QTreeWidgetItem * currentItem() const
Returns the current item in the tree widget.
void scheduleDelayedItemsLayout()
Schedules a layout of the items in the view to be executed when the event processing starts...
#define Q_D(Class)
Definition: qglobal.h:2482
QTreeWidgetItem * headerItem
void _q_emitItemExpanded(const QModelIndex &index)
static const uint base
Definition: qurl.cpp:268
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data corresponding to the given model index and role.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
Sets the header data for the item specified by the header section, orientation and data role to the g...
void executePendingSort() const
void sort(int column, Qt::SortOrder order)
Sorts the entire tree in the model in the given order, by the values in the given column...
int indexOfTopLevelItem(QTreeWidgetItem *item)
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
bool isFirstItemColumnSpanned(const QTreeWidgetItem *item) const
Returns true if the given item is set to show only one section over all columns; otherwise returns fa...
QWidget * indexWidget(const QModelIndex &index) const
Returns the widget for the item at the given index.
void _q_emitItemEntered(const QModelIndex &index)
QDataStream & operator<<(QDataStream &out, const QTreeWidgetItem &item)
Writes the tree widget item item to stream out.
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
void collapse(const QModelIndex &index)
Collapses the model item specified by the index.
Definition: qtreeview.cpp:821
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
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
void setModel(QAbstractItemModel *model)
Reimplemented Function
QTreeWidgetItem * topLevelItem(int index) const
Returns the top level item at the given index, or 0 if the item does not exist.
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
void addChild(QTreeWidgetItem *child)
Appends the child item to the list of children.
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
virtual void write(QDataStream &out) const
Writes the item to stream out.
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Reimplemented Function
Definition: qtreeview.cpp:3857
void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible)
Scroll the contents of the tree view until the given model item index is visible. ...
Definition: qtreeview.cpp:1141
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
void dropEvent(QDropEvent *event)
This function is called with the given event when a drop event occurs over the widget.
friend class QTreeModel
Definition: qtreewidget.h:266
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const =0
Returns the index of the item in the model specified by the given row, column and parent index...
void beginInsertItems(QTreeWidgetItem *parent, int row, int count)
void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void expanded(const QModelIndex &index)
This signal is emitted when the item specified by index is expanded.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QTreeWidgetItem(int type=Type)
Constructs a tree widget item of the specified type.
void setFirstColumnSpanned(int row, const QModelIndex &parent, bool span)
If span is true the item in the first column in the row with the given parent is set to span all colu...
Definition: qtreeview.cpp:675
SortOrder
Definition: qnamespace.h:189
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QTreeWidgetItem * par
Definition: qtreewidget.h:221
QTreeWidgetItem * invisibleRootItem() const
Returns the tree widget&#39;s invisible root item.
QMap< int, QVariant > itemData(const QModelIndex &index) const
Returns a map with values for all predefined roles in the model for the item at the given index...
void entered(const QModelIndex &index)
This signal is emitted when the mouse cursor enters the item specified by index.
void propagateDisabled(QTreeWidgetItem *item)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this view.
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
friend class QTreeWidgetItem
Definition: qtreewidget_p.h:76
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of treewidget items.
void setClickable(bool clickable)
If clickable is true, the header will respond to single clicks.
Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
QList< QTreeWidgetItemIterator * > iterators
int lastIndexOf(const T &t, int from=-1) const
Returns the index position of the last occurrence of value in the list, searching backward from index...
Definition: qlist.h:862
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
static bool itemGreaterThan(const QPair< QTreeWidgetItem *, int > &left, const QPair< QTreeWidgetItem *, int > &right)
Returns true if the value of the left item is greater than the value of the right item...
QTreeWidgetItem * parent() const
Returns the item&#39;s parent.
Definition: qtreewidget.h:183
int type() const
Returns the type passed to the QTreeWidgetItem constructor.
Definition: qtreewidget.h:203
void closePersistentEditor(QTreeWidgetItem *item, int column=0)
Closes the persistent editor for the item in the given column.
bool contains(const T &value) const
Definition: qset.h:91
T takeFirst()
Removes the first item in the list and returns it.
Definition: qlist.h:489
int rowCount(const QModelIndex &parent) const
Returns the number of rows in the parent model index.
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
QItemSelectionModel * selectionModel() const
Returns the current selection model.
int currentColumn() const
Returns the current column in the tree widget.
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
void insertTopLevelItems(int index, const QList< QTreeWidgetItem *> &items)
Inserts the list of items at index in the top level in the view.
The QTreeView class provides a default model/view implementation of a tree view.
Definition: qtreeview.h:58
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
int row() const
Returns the row this model index refers to.
QTreeWidgetItem * takeChild(int index)
Removes the item at index and returns it, otherwise return 0.
QTreeWidget * view() const
Definition: qtreewidget_p.h:85
void addTopLevelItems(const QList< QTreeWidgetItem *> &items)
Appends the list of items as a top-level items in the widget.
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
bool operator()(QTreeWidgetItem *i1, QTreeWidgetItem *i2) const
Definition: qtreewidget.cpp:69
QTreeWidgetItem * rootItem
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QMimeData * internalMimeData() const
QWidgetData * data
Definition: qwidget.h:815
void setHidden(bool hidden)
Convenience function, equivalent to setVisible(!hidden).
Definition: qwidget.h:495
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
void setCurrentIndex(const QModelIndex &index)
Sets the current item to be the item at index.
bool operator()(QTreeWidgetItem *i1, QTreeWidgetItem *i2) const
Definition: qtreewidget.cpp:62
const_iterator insert(const T &value)
Definition: qset.h:179
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
void setSortingEnabled(bool enable)
Definition: qtreeview.cpp:898
void endInsertItems()
void _q_emitItemActivated(const QModelIndex &index)
virtual QMimeData * mimeData(const QList< QTreeWidgetItem *> items) const
Returns an object that contains a serialized description of the specified items.
QModelIndex currentIndex() const
Returns the model index of the current item.
QTreeWidgetItem * takeTopLevelItem(int index)
Removes the top-level item at the given index in the tree and returns it, otherwise returns 0;...
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns in the item referred to by the given index.
bool isItemExpanded(const QTreeWidgetItem *item) const
Returns true if the given item is open; otherwise returns false.
DropAction
Definition: qnamespace.h:1597
void endRemoveRows()
Ends a row removal operation.
void openPersistentEditor(QTreeWidgetItem *item, int column=0)
Opens a persistent editor for the item in the given column.
QTreeWidgetItem::ChildIndicatorPolicy policy
void clear()
Removes all items from the list.
Definition: qlist.h:764
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
void activated(const QModelIndex &index)
This signal is emitted when the item specified by index is activated by the user. ...
void expandItem(const QTreeWidgetItem *item)
Expands the item.
virtual void read(QDataStream &in)
Reads the item from stream in.
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
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.
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.
void expand(const QModelIndex &index)
Expands the model item specified by the index.
Definition: qtreeview.cpp:787
virtual bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
Handles the data supplied by a drag and drop operation that ended with the given action in the index ...
void hide()
Hides the widget.
Definition: qwidget.h:501
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QAbstractItemModel class provides the abstract interface for item model classes.
void stop()
Stops the timer.
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
void setCurrentItem(QTreeWidgetItem *item)
Sets the current item in the tree widget.
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
void setHeaderItem(QTreeWidgetItem *item)
Sets the header item for the tree widget.
void addTopLevelItem(QTreeWidgetItem *item)
Appends the item as a top-level item in the widget.
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class...
Definition: qtreewidget.h:63
void sortChildren(int column, Qt::SortOrder order)
Sorts the children of the item using the given order, by the values in the given column.
Definition: qtreewidget.h:204
int columnCount() const
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qLowerBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:227
QList< QTreeWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags, int column=0) const
Returns a list of items that match the given text, using the given flags, in the given column...
QModelIndex indexFromItem(QTreeWidgetItem *item, int column=0) const
Returns the QModelIndex assocated with the given item in the given column.
void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy)
Sets the item indicator policy.
QModelIndexList cachedIndexes
Qt::ItemFlags itemFlags
Definition: qtreewidget.h:223
bool isSortingEnabled() const
Definition: qtreeview.cpp:916
QTreeWidgetItem * child(int index) const
Returns the item at the given index in the list of the item&#39;s children.
Definition: qtreewidget.h:184
The QItemSelection class manages information about selected items in a model.
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
void setFlags(Qt::ItemFlags flags)
Sets the flags for the item to the given flags.
void _q_emitItemPressed(const QModelIndex &index)
QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const
Returns the item indicator policy.
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the list, searching forward from index...
Definition: qlist.h:847
int logicalIndex(int visualIndex) const
Returns the logicalIndex for the section at the given visualIndex position, or -1 if visualIndex < 0 ...
bool(* LessThan)(const QPair< QTreeWidgetItem *, int > &, const QPair< QTreeWidgetItem *, int > &)
Definition: qtreewidget.cpp:57
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
void pressed(const QModelIndex &index)
This signal is emitted when a mouse button is pressed.
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
The QPersistentModelIndex class is used to locate data in a data model.
void _q_emitItemChanged(const QModelIndex &index)
void setItemSelected(const QTreeWidgetItem *item, bool select)
If select is true, the given item is selected; otherwise it is deselected.
QTreeWidgetItemPrivate * d
Definition: qtreewidget.h:220
void insertChildren(int index, const QList< QTreeWidgetItem *> &children)
Inserts the given list of children into the list of the item children at index .
QList< QTreeWidgetItem * > takeChildren()
Removes the list of children and returns it, otherwise returns an empty list.
bool isChanging() const
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
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
QWidget * source() const
If the source of the drag operation is a widget in this application, this function returns that sourc...
Definition: qevent.cpp:2739
static QList< QTreeWidgetItem * >::iterator sortedInsertionIterator(const QList< QTreeWidgetItem *>::iterator &begin, const QList< QTreeWidgetItem *>::iterator &end, Qt::SortOrder order, QTreeWidgetItem *item)
QList< QTreeWidgetItem * > selectedItems() const
Returns a list of all selected non-hidden items.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QDataStream & operator>>(QDataStream &in, QTreeWidgetItem &item)
Reads a tree widget item from stream in into item.
QRect visualRect(const QModelIndex &index) const
Returns the rectangle on the viewport occupied by the item at index.
Definition: qtreeview.cpp:1101
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
QHeaderView * header() const
Returns the header for the tree view.
Definition: qtreeview.cpp:302
The QModelIndex class is used to locate data in a data model.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
int sortColumn() const
Returns the column used to sort the contents of the widget.
QWidget * itemWidget(QTreeWidgetItem *item, int column) const
Returns the widget displayed in the cell specified by item and the given column.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
bool setData(const QModelIndex &index, const QVariant &value, int role)
Sets the data for the item specified by the index and role to that referred to by the value...
int timerId() const
Returns the timer&#39;s ID.
Definition: qbasictimer.h:63
Definition: qnamespace.h:54
QVector< QVector< QWidgetItemData > > values
Definition: qtreewidget.h:218
void edit(const QModelIndex &index)
Starts editing the item corresponding to the given index if it is editable.
void setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span)
Sets the given item to only show one section for all columns if span is true; otherwise the item will...
const QObjectList & children() const
Returns a list of child objects.
Definition: qobject.h:197
~QTreeWidget()
Destroys the tree widget and all its items.
T takeAt(int i)
Removes the item at index position i and returns it.
Definition: qlist.h:484
void insertTopLevelItem(int index, QTreeWidgetItem *item)
Inserts the item at index in the top level in the view.
void beginRemoveItems(QTreeWidgetItem *parent, int row, int count)
QTreeWidgetItem * headerItem() const
Returns the item used for the tree widget&#39;s header.
void setExpanded(const QModelIndex &index, bool expand)
Sets the item referred to by index to either collapse or expanded, depending on the value of expanded...
Definition: qtreeview.cpp:871
QTreeWidgetItem * itemAbove(const QTreeWidgetItem *item) const
Returns the item above the given item.
void endInsertColumns()
Ends a column insertion operation.
quint16 index
QBasicTimer sortPendingTimer
QScopedPointer< QObjectData > d_ptr
Definition: qobject.h:320
QRect visualItemRect(const QTreeWidgetItem *item) const
Returns the rectangle on the viewport occupied by the item at item.
void sortItems(int column, Qt::SortOrder order)
Sorts the items in the widget in the specified order by the values in the given column.
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
void setItemExpanded(const QTreeWidgetItem *item, bool expand)
Sets the item referred to by item to either closed or opened, depending on the value of expand...
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
QModelIndexList indexes() const
Returns a list of model indexes that correspond to the selected items.
void doubleClicked(const QModelIndex &index)
This signal is emitted when a mouse button is double-clicked.
void closePersistentEditor(const QModelIndex &index)
Closes the persistent editor for the item at the given index.
bool signalsBlocked() const
Returns true if signals are blocked; otherwise returns false.
Definition: qobject.h:148
void setSortIndicator(int logicalIndex, Qt::SortOrder order)
Sets the sort indicator for the section specified by the given logicalIndex in the direction specifie...
bool insertColumns(int column, int count, const QModelIndex &)
QList< QTreeWidgetItem * > children
Definition: qtreewidget.h:222
void scrollToItem(const QTreeWidgetItem *item, QAbstractItemView::ScrollHint hint=EnsureVisible)
Ensures that the item is visible, scrolling the view if necessary using the specified hint...
int logicalIndexAt(int position) const
Returns the section that covers the given position in the viewport.
void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index)
QModelIndex indexAbove(const QModelIndex &index) const
Returns the model index of the item above index.
Definition: qtreeview.cpp:2045
T value() const
Returns the stored value converted to the template type T.
Definition: qvariant.h:332
void setColumnCount(int columns)
Sets the number of columns in the tree model.
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
QModelIndex indexAt(const QPoint &p) const
Reimplemented Function
Definition: qtreeview.cpp:2021
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
Definition: qtestcase.cpp:2183
QAbstractItemModel * model() const
Returns the model that this view is presenting.
static const KeyPair *const end
Orientation
Definition: qnamespace.h:174
static bool itemLessThan(const QPair< QTreeWidgetItem *, int > &left, const QPair< QTreeWidgetItem *, int > &right)
Returns true if the value of the left item is less than the value of the right item.
void collapsed(const QModelIndex &index)
This signal is emitted when the item specified by index is collapsed.
bool event(QEvent *event)
Reimplemented Function
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
void setColumnCount(int columns)
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the flags for the item referred to the given index.
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
Reimplemented Function
Definition: qtreeview.cpp:706
bool event(QEvent *e)
Reimplemented Function
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
int columnCount() const
Returns the number of columns in the item.
Definition: qtreewidget.h:191
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void endRemoveItems()
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
~QTreeModel()
Destroys this tree model.
void addChildren(const QList< QTreeWidgetItem *> &children)
Appends the given list of children to the item.
bool skipPendingSort
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
void reserve(int size)
Reserve space for alloc elements.
Definition: qlist.h:496
void reset()
Resets the model to its original state in any attached views.
#define text
Definition: qobjectdefs.h:80
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
int column() const
Returns the column this model index refers to.
virtual bool operator<(const QTreeWidgetItem &other) const
Returns true if the text in the item is less than the text in the other item, otherwise returns false...
virtual QTreeWidgetItem * clone() const
Creates a deep copy of the item and of its children.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void ensureSorted(int column, Qt::SortOrder order, int start, int end, const QModelIndex &parent)
bool isItemHidden(const QTreeWidgetItem *item) const
Returns true if the item is explicitly hidden, otherwise returns false.
void setSortingEnabled(bool enable)
void emitDataChanged(QTreeWidgetItem *item, int column)
Emits the dataChanged() signal for the given item.
void endRemoveColumns()
Ends a column removal operation.
virtual QVariant data(int column, int role) const
Returns the value for the item&#39;s column and role.
void sortItems(QList< QTreeWidgetItem *> *items, int column, Qt::SortOrder order)
void reserve(int size)
Definition: qset.h:241