Qt 4.8
qtablewidget.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 "qtablewidget.h"
43 
44 #ifndef QT_NO_TABLEWIDGET
45 #include <qitemdelegate.h>
46 #include <qpainter.h>
47 #include <private/qtablewidget_p.h>
48 
50 
51 QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
52  : QAbstractTableModel(parent),
53  prototype(0),
54  tableItems(rows * columns, 0),
55  verticalHeaderItems(rows, 0),
56  horizontalHeaderItems(columns, 0)
57 {}
58 
60 {
61  clear();
62  delete prototype;
63 }
64 
65 bool QTableModel::insertRows(int row, int count, const QModelIndex &)
66 {
67  if (count < 1 || row < 0 || row > verticalHeaderItems.count())
68  return false;
69 
70  beginInsertRows(QModelIndex(), row, row + count - 1);
71  int rc = verticalHeaderItems.count();
72  int cc = horizontalHeaderItems.count();
73  verticalHeaderItems.insert(row, count, 0);
74  if (rc == 0)
75  tableItems.resize(cc * count);
76  else
77  tableItems.insert(tableIndex(row, 0), cc * count, 0);
78  endInsertRows();
79  return true;
80 }
81 
82 bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
83 {
84  if (count < 1 || column < 0 || column > horizontalHeaderItems.count())
85  return false;
86 
87  beginInsertColumns(QModelIndex(), column, column + count - 1);
88  int rc = verticalHeaderItems.count();
89  int cc = horizontalHeaderItems.count();
90  horizontalHeaderItems.insert(column, count, 0);
91  if (cc == 0)
92  tableItems.resize(rc * count);
93  else
94  for (int row = 0; row < rc; ++row)
95  tableItems.insert(tableIndex(row, column), count, 0);
97  return true;
98 }
99 
100 bool QTableModel::removeRows(int row, int count, const QModelIndex &)
101 {
102  if (count < 1 || row < 0 || row + count > verticalHeaderItems.count())
103  return false;
104 
105  beginRemoveRows(QModelIndex(), row, row + count - 1);
106  int i = tableIndex(row, 0);
107  int n = count * columnCount();
108  QTableWidgetItem *oldItem = 0;
109  for (int j = i; j < n + i; ++j) {
110  oldItem = tableItems.at(j);
111  if (oldItem)
112  oldItem->view = 0;
113  delete oldItem;
114  }
115  tableItems.remove(qMax(i, 0), n);
116  for (int v = row; v < row + count; ++v) {
117  oldItem = verticalHeaderItems.at(v);
118  if (oldItem)
119  oldItem->view = 0;
120  delete oldItem;
121  }
122  verticalHeaderItems.remove(row, count);
123  endRemoveRows();
124  return true;
125 }
126 
127 bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
128 {
129  if (count < 1 || column < 0 || column + count > horizontalHeaderItems.count())
130  return false;
131 
132  beginRemoveColumns(QModelIndex(), column, column + count - 1);
133  QTableWidgetItem *oldItem = 0;
134  for (int row = rowCount() - 1; row >= 0; --row) {
135  int i = tableIndex(row, column);
136  for (int j = i; j < i + count; ++j) {
137  oldItem = tableItems.at(j);
138  if (oldItem)
139  oldItem->view = 0;
140  delete oldItem;
141  }
142  tableItems.remove(i, count);
143  }
144  for (int h=column; h<column+count; ++h) {
145  oldItem = horizontalHeaderItems.at(h);
146  if (oldItem)
147  oldItem->view = 0;
148  delete oldItem;
149  }
150  horizontalHeaderItems.remove(column, count);
152  return true;
153 }
154 
155 void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
156 {
157  int i = tableIndex(row, column);
158  if (i < 0 || i >= tableItems.count())
159  return;
160  QTableWidgetItem *oldItem = tableItems.at(i);
161  if (item == oldItem)
162  return;
163 
164  // remove old
165  if (oldItem)
166  oldItem->view = 0;
167  delete tableItems.at(i);
168 
170 
171  // set new
172  if (item)
173  item->d->id = i;
174  tableItems[i] = item;
175 
176  if (view && view->isSortingEnabled()
177  && view->horizontalHeader()->sortIndicatorSection() == column) {
178  // sorted insertion
180  QVector<QTableWidgetItem*> colItems = columnItems(column);
181  if (row < colItems.count())
182  colItems.remove(row);
183  int sortedRow;
184  if (item == 0) {
185  // move to after all non-0 (sortable) items
186  sortedRow = colItems.count();
187  } else {
189  it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
190  sortedRow = qMax((int)(it - colItems.begin()), 0);
191  }
192  if (sortedRow != row) {
194  // move the items @ row to sortedRow
195  int cc = columnCount();
196  QVector<QTableWidgetItem*> rowItems(cc);
197  for (int j = 0; j < cc; ++j)
198  rowItems[j] = tableItems.at(tableIndex(row, j));
199  tableItems.remove(tableIndex(row, 0), cc);
200  tableItems.insert(tableIndex(sortedRow, 0), cc, 0);
201  for (int j = 0; j < cc; ++j)
202  tableItems[tableIndex(sortedRow, j)] = rowItems.at(j);
203  QTableWidgetItem *header = verticalHeaderItems.at(row);
205  verticalHeaderItems.insert(sortedRow, header);
206  // update persistent indexes
207  QModelIndexList oldPersistentIndexes = persistentIndexList();
208  QModelIndexList newPersistentIndexes = oldPersistentIndexes;
209  updateRowIndexes(newPersistentIndexes, row, sortedRow);
210  changePersistentIndexList(oldPersistentIndexes,
211  newPersistentIndexes);
212 
214  return;
215  }
216  }
217  QModelIndex idx = QAbstractTableModel::index(row, column);
218  emit dataChanged(idx, idx);
219 }
220 
222 {
223  long i = tableIndex(row, column);
225  if (itm) {
226  itm->view = 0;
227  itm->d->id = -1;
228  tableItems[i] = 0;
229  QModelIndex ind = index(itm);
230  emit dataChanged(ind, ind);
231  }
232  return itm;
233 }
234 
235 QTableWidgetItem *QTableModel::item(int row, int column) const
236 {
237  return tableItems.value(tableIndex(row, column));
238 }
239 
241 {
242  if (!isValid(index))
243  return 0;
244  return tableItems.at(tableIndex(index.row(), index.column()));
245 }
246 
248 {
249  int i = tableItems.indexOf(item);
250  if (i != -1) {
251  tableItems[i] = 0;
252  QModelIndex idx = index(item);
253  emit dataChanged(idx, idx);
254  return;
255  }
256 
257  i = verticalHeaderItems.indexOf(item);
258 
259  if (i != -1) {
260  verticalHeaderItems[i] = 0;
262  return;
263  }
264  i = horizontalHeaderItems.indexOf(item);
265  if (i != -1) {
266  horizontalHeaderItems[i] = 0;
268  return;
269  }
270 }
271 
273 {
274  if (section < 0 || section >= horizontalHeaderItems.count())
275  return;
276  QTableWidgetItem *oldItem = horizontalHeaderItems.at(section);
277  if (item == oldItem)
278  return;
279 
280  if (oldItem)
281  oldItem->view = 0;
282  delete oldItem;
283 
285 
286  if (item) {
287  item->view = view;
288  item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
289  }
290  horizontalHeaderItems[section] = item;
291  emit headerDataChanged(Qt::Horizontal, section, section);
292 }
293 
295 {
296  if (section < 0 || section >= verticalHeaderItems.count())
297  return;
298  QTableWidgetItem *oldItem = verticalHeaderItems.at(section);
299  if (item == oldItem)
300  return;
301 
302  if (oldItem)
303  oldItem->view = 0;
304  delete oldItem;
305 
307 
308  if (item) {
309  item->view = view;
310  item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
311  }
312  verticalHeaderItems[section] = item;
313  emit headerDataChanged(Qt::Vertical, section, section);
314 }
315 
317 {
318  if (section < 0 || section >= horizontalHeaderItems.count())
319  return 0;
320  QTableWidgetItem *itm = horizontalHeaderItems.at(section);
321  if (itm) {
322  itm->view = 0;
323  itm->itemFlags &= ~ItemIsHeaderItem;
324  horizontalHeaderItems[section] = 0;
325  }
326  return itm;
327 }
328 
330 {
331  if (section < 0 || section >= verticalHeaderItems.count())
332  return 0;
333  QTableWidgetItem *itm = verticalHeaderItems.at(section);
334  if (itm) {
335  itm->view = 0;
336  itm->itemFlags &= ~ItemIsHeaderItem;
337  verticalHeaderItems[section] = 0;
338  }
339  return itm;
340 }
341 
343 {
344  return horizontalHeaderItems.value(section);
345 }
346 
348 {
349  return verticalHeaderItems.value(section);
350 }
351 
353 {
354  if (!item)
355  return QModelIndex();
356  int i = -1;
357  const int id = item->d->id;
358  if (id >= 0 && id < tableItems.count() && tableItems.at(id) == item) {
359  i = id;
360  } else { // we need to search for the item
361  i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item));
362  if (i == -1) // not found
363  return QModelIndex();
364  }
365  int row = i / columnCount();
366  int col = i % columnCount();
367  return QAbstractTableModel::index(row, col);
368 }
369 
371 {
372  int rc = verticalHeaderItems.count();
373  if (rows < 0 || rc == rows)
374  return;
375  if (rc < rows)
376  insertRows(qMax(rc, 0), rows - rc);
377  else
378  removeRows(qMax(rows, 0), rc - rows);
379 }
380 
382 {
383  int cc = horizontalHeaderItems.count();
384  if (columns < 0 || cc == columns)
385  return;
386  if (cc < columns)
387  insertColumns(qMax(cc, 0), columns - cc);
388  else
389  removeColumns(qMax(columns, 0), cc - columns);
390 }
391 
393 {
394  return parent.isValid() ? 0 : verticalHeaderItems.count();
395 }
396 
398 {
399  return parent.isValid() ? 0 : horizontalHeaderItems.count();
400 }
401 
403 {
404  QTableWidgetItem *itm = item(index);
405  if (itm)
406  return itm->data(role);
407  return QVariant();
408 }
409 
410 bool QTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
411 {
412  if (!index.isValid())
413  return false;
414 
415  QTableWidgetItem *itm = item(index);
416  if (itm) {
417  itm->setData(role, value);
418  return true;
419  }
420 
421  // don't create dummy table items for empty values
422  if (!value.isValid())
423  return false;
424 
426  if (!view)
427  return false;
428 
429  itm = createItem();
430  itm->setData(role, value);
431  view->setItem(index.row(), index.column(), itm);
432  return true;
433 }
434 
436 {
437  QMap<int, QVariant> roles;
438  QTableWidgetItem *itm = item(index);
439  if (itm) {
440  for (int i = 0; i < itm->values.count(); ++i) {
441  roles.insert(itm->values.at(i).role,
442  itm->values.at(i).value);
443  }
444  }
445  return roles;
446 }
447 
448 // reimplemented to ensure that only one dataChanged() signal is emitted
450 {
451  if (!index.isValid())
452  return false;
453 
455  QTableWidgetItem *itm = item(index);
456  if (itm) {
457  itm->view = 0; // prohibits item from calling itemChanged()
458  bool changed = false;
459  for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
460  if (itm->data(it.key()) != it.value()) {
461  itm->setData(it.key(), it.value());
462  changed = true;
463  }
464  }
465  itm->view = view;
466  if (changed)
467  itemChanged(itm);
468  return true;
469  }
470 
471  if (!view)
472  return false;
473 
474  itm = createItem();
475  for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
476  itm->setData(it.key(), it.value());
477  view->setItem(index.row(), index.column(), itm);
478  return true;
479 }
480 
481 Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
482 {
483  if (!index.isValid())
484  return Qt::ItemIsDropEnabled;
485  if (QTableWidgetItem *itm = item(index))
486  return itm->flags();
487  return (Qt::ItemIsEditable
493 }
494 
495 void QTableModel::sort(int column, Qt::SortOrder order)
496 {
498  QVector<int> unsortable;
499 
500  sortable.reserve(rowCount());
501  unsortable.reserve(rowCount());
502 
503  for (int row = 0; row < rowCount(); ++row) {
504  if (QTableWidgetItem *itm = item(row, column))
505  sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
506  else
507  unsortable.append(row);
508  }
509 
511  qStableSort(sortable.begin(), sortable.end(), compare);
512 
514  QModelIndexList from;
515  QModelIndexList to;
516  for (int i = 0; i < rowCount(); ++i) {
517  int r = (i < sortable.count()
518  ? sortable.at(i).second
519  : unsortable.at(i - sortable.count()));
520  for (int c = 0; c < columnCount(); ++c) {
521  sorted_table[tableIndex(i, c)] = item(r, c);
522  from.append(createIndex(r, c, 0));
523  to.append(createIndex(i, c, 0));
524  }
525  }
526 
528 
529  tableItems = sorted_table;
530  changePersistentIndexList(from, to); // ### slow
531 
533 }
534 
535 /*
536  \internal
537 
538  Ensures that rows in the interval [start, end] are
539  sorted according to the contents of column \a column
540  and the given sort \a order.
541 */
543  int start, int end)
544 {
545  int count = end - start + 1;
547  sorting.reserve(count);
548  for (int row = start; row <= end; ++row) {
549  QTableWidgetItem *itm = item(row, column);
550  if (itm == 0) {
551  // no more sortable items (all 0-items are
552  // at the end of the table when it is sorted)
553  break;
554  }
555  sorting.append(QPair<QTableWidgetItem*,int>(itm, row));
556  }
557 
559  qStableSort(sorting.begin(), sorting.end(), compare);
560 
561  QModelIndexList oldPersistentIndexes = persistentIndexList();
562  QModelIndexList newPersistentIndexes = oldPersistentIndexes;
565  QVector<QTableWidgetItem*> colItems = columnItems(column);
567  bool changed = false;
568  for (int i = 0; i < sorting.count(); ++i) {
569  int oldRow = sorting.at(i).second;
570  QTableWidgetItem *item = colItems.at(oldRow);
571  colItems.remove(oldRow);
572  vit = sortedInsertionIterator(vit, colItems.end(), order, item);
573  int newRow = qMax((int)(vit - colItems.begin()), 0);
574  if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item))
575  newRow = oldRow;
576  vit = colItems.insert(vit, item);
577  if (newRow != oldRow) {
578  changed = true;
579  // move the items @ oldRow to newRow
580  int cc = columnCount();
581  QVector<QTableWidgetItem*> rowItems(cc);
582  for (int j = 0; j < cc; ++j)
583  rowItems[j] = newTable.at(tableIndex(oldRow, j));
584  newTable.remove(tableIndex(oldRow, 0), cc);
585  newTable.insert(tableIndex(newRow, 0), cc, 0);
586  for (int j = 0; j < cc; ++j)
587  newTable[tableIndex(newRow, j)] = rowItems.at(j);
588  QTableWidgetItem *header = newVertical.at(oldRow);
589  newVertical.remove(oldRow);
590  newVertical.insert(newRow, header);
591  // update persistent indexes
592  updateRowIndexes(newPersistentIndexes, oldRow, newRow);
593  // the index of the remaining rows may have changed
594  for (int j = i + 1; j < sorting.count(); ++j) {
595  int otherRow = sorting.at(j).second;
596  if (oldRow < otherRow && newRow >= otherRow)
597  --sorting[j].second;
598  else if (oldRow > otherRow && newRow <= otherRow)
599  ++sorting[j].second;
600  }
601  }
602  }
603 
604  if (changed) {
606  tableItems = newTable;
607  verticalHeaderItems = newVertical;
608  changePersistentIndexList(oldPersistentIndexes,
609  newPersistentIndexes);
611  }
612 }
613 
614 /*
615  \internal
616 
617  Returns the non-0 items in column \a column.
618 */
620 {
622  int rc = rowCount();
623  items.reserve(rc);
624  for (int row = 0; row < rc; ++row) {
625  QTableWidgetItem *itm = item(row, column);
626  if (itm == 0) {
627  // no more sortable items (all 0-items are
628  // at the end of the table when it is sorted)
629  break;
630  }
631  items.append(itm);
632  }
633  return items;
634 }
635 
636 /*
637  \internal
638 
639  Adjusts the row of each index in \a indexes if necessary, given
640  that a row of items has been moved from row \a movedFrom to row
641  \a movedTo.
642 */
644  int movedFromRow, int movedToRow)
645 {
647  for (it = indexes.begin(); it != indexes.end(); ++it) {
648  int oldRow = (*it).row();
649  int newRow = oldRow;
650  if (oldRow == movedFromRow)
651  newRow = movedToRow;
652  else if (movedFromRow < oldRow && movedToRow >= oldRow)
653  newRow = oldRow - 1;
654  else if (movedFromRow > oldRow && movedToRow <= oldRow)
655  newRow = oldRow + 1;
656  if (newRow != oldRow)
657  *it = index(newRow, (*it).column(), (*it).parent());
658  }
659 }
660 
661 /*
662  \internal
663 
664  Returns an iterator to the item where \a item should be
665  inserted in the interval (\a begin, \a end) according to
666  the given sort \a order.
667 */
672 {
673  if (order == Qt::AscendingOrder)
674  return qLowerBound(begin, end, item, QTableModelLessThan());
675  return qLowerBound(begin, end, item, QTableModelGreaterThan());
676 }
677 
680 {
681  return *(left.first) < *(right.first);
682 }
683 
686 {
687  return (*(right.first) < *(left .first));
688 }
689 
690 QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
691 {
692  if (section < 0)
693  return QVariant();
694 
695  QTableWidgetItem *itm = 0;
696  if (orientation == Qt::Horizontal && section < horizontalHeaderItems.count())
697  itm = horizontalHeaderItems.at(section);
698  else if (orientation == Qt::Vertical && section < verticalHeaderItems.count())
699  itm = verticalHeaderItems.at(section);
700  else
701  return QVariant(); // section is out of bounds
702 
703  if (itm)
704  return itm->data(role);
705  if (role == Qt::DisplayRole)
706  return section + 1;
707  return QVariant();
708 }
709 
710 bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
711  const QVariant &value, int role)
712 {
713  if (section < 0 ||
714  (orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) ||
715  (orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
716  return false;
717 
718  QTableWidgetItem *itm = 0;
719  if (orientation == Qt::Horizontal)
720  itm = horizontalHeaderItems.at(section);
721  else
722  itm = verticalHeaderItems.at(section);
723  if (itm) {
724  itm->setData(role, value);
725  return true;
726  }
727  return false;
728 }
729 
731 {
732  return (index.isValid()
733  && index.row() < verticalHeaderItems.count()
734  && index.column() < horizontalHeaderItems.count());
735 }
736 
738 {
739  for (int j = 0; j < verticalHeaderItems.count(); ++j) {
740  if (verticalHeaderItems.at(j)) {
741  verticalHeaderItems.at(j)->view = 0;
742  delete verticalHeaderItems.at(j);
743  verticalHeaderItems[j] = 0;
744  }
745  }
746  for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
747  if (horizontalHeaderItems.at(k)) {
748  horizontalHeaderItems.at(k)->view = 0;
749  delete horizontalHeaderItems.at(k);
750  horizontalHeaderItems[k] = 0;
751  }
752  }
753  clearContents();
754 }
755 
757 {
758  for (int i = 0; i < tableItems.count(); ++i) {
759  if (tableItems.at(i)) {
760  tableItems.at(i)->view = 0;
761  delete tableItems.at(i);
762  tableItems[i] = 0;
763  }
764  }
765  reset();
766 }
767 
769 {
770  if (!item)
771  return;
772  if (item->flags() & ItemIsHeaderItem) {
773  int row = verticalHeaderItems.indexOf(item);
774  if (row >= 0) {
776  } else {
777  int column = horizontalHeaderItems.indexOf(item);
778  if (column >= 0)
779  emit headerDataChanged(Qt::Horizontal, column, column);
780  }
781  } else {
782  QModelIndex idx = index(item);
783  if (idx.isValid())
784  emit dataChanged(idx, idx);
785  }
786 }
787 
789 {
790  return prototype ? prototype->clone() : new QTableWidgetItem;
791 }
792 
794 {
795  return prototype;
796 }
797 
799 {
800  if (prototype != item) {
801  delete prototype;
802  prototype = item;
803  }
804 }
805 
807 {
808  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
809  return (view ? view->mimeTypes() : QStringList());
810 }
811 
813 {
815 }
816 
818 {
820  for (int i = 0; i < indexes.count(); ++i)
821  items << item(indexes.at(i));
822  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
823 
824  // cachedIndexes is a little hack to avoid copying from QModelIndexList to
825  // QList<QTreeWidgetItem*> and back again in the view
826  cachedIndexes = indexes;
827  QMimeData *mimeData = (view ? view->mimeData(items) : 0);
828  cachedIndexes.clear();
829  return mimeData;
830 }
831 
833  int row , int column, const QModelIndex &index)
834 {
835  if (index.isValid()) {
836  row = index.row();
837  column = index.column();
838  }else if (row == -1 || column == -1) { // The user dropped outside the table.
839  row = rowCount();
840  column = 0;
841  }
842 
844  return (view ? view->dropMimeData(row, column, data, action) : false);
845 }
846 
847 Qt::DropActions QTableModel::supportedDropActions() const
848 {
849  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
850  return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
851 }
852 
880  : top(-1), left(-1), bottom(-2), right(-2)
881 {
882 }
883 
891  : top(top), left(left), bottom(bottom), right(right)
892 {
893 }
894 
900  : top(other.top), left(other.left), bottom(other.bottom), right(other.right)
901 {
902 }
903 
908 {
909 }
910 
1151 void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
1152 {
1153  itemFlags = aflags;
1154  if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
1155  model->itemChanged(this);
1156 }
1157 
1158 
1411  : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
1412  itemFlags(Qt::ItemIsEditable
1415  |Qt::ItemIsEnabled
1418 {
1419 }
1420 
1427  : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
1431  |Qt::ItemIsEnabled
1434 {
1435  setData(Qt::DisplayRole, text);
1436 }
1437 
1444  : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
1448  |Qt::ItemIsEnabled
1451 {
1452  setData(Qt::DecorationRole, icon);
1453  setData(Qt::DisplayRole, text);
1454 }
1455 
1460 {
1461  if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
1462  model->removeItem(this);
1463  view = 0;
1464  delete d;
1465 }
1466 
1471 {
1472  return new QTableWidgetItem(*this);
1473 }
1474 
1480 void QTableWidgetItem::setData(int role, const QVariant &value)
1481 {
1482  bool found = false;
1483  role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1484  for (int i = 0; i < values.count(); ++i) {
1485  if (values.at(i).role == role) {
1486  if (values[i].value == value)
1487  return;
1488 
1489  values[i].value = value;
1490  found = true;
1491  break;
1492  }
1493  }
1494  if (!found)
1495  values.append(QWidgetItemData(role, value));
1496  if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
1497  model->itemChanged(this);
1498 }
1499 
1504 {
1505  role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1506  for (int i = 0; i < values.count(); ++i)
1507  if (values.at(i).role == role)
1508  return values.at(i).value;
1509  return QVariant();
1510 }
1511 
1517 {
1518  const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole);
1520 }
1521 
1522 #ifndef QT_NO_DATASTREAM
1523 
1530 {
1531  in >> values;
1532 }
1533 
1540 {
1541  out << values;
1542 }
1543 
1557 {
1558  item.read(in);
1559  return in;
1560 }
1561 
1575 {
1576  item.write(out);
1577  return out;
1578 }
1579 
1580 #endif // QT_NO_DATASTREAM
1581 
1596  : rtti(Type), values(other.values), view(0),
1597  d(new QTableWidgetItemPrivate(this)),
1598  itemFlags(other.itemFlags)
1599 {
1600 }
1601 
1611 {
1612  values = other.values;
1613  itemFlags = other.itemFlags;
1614  return *this;
1615 }
1616 
1703 {
1704  Q_Q(QTableWidget);
1705  // view signals
1706  QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex)));
1707  QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex)));
1708  QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
1709  q, SLOT(_q_emitItemDoubleClicked(QModelIndex)));
1710  QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(_q_emitItemActivated(QModelIndex)));
1711  QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
1712  // model signals
1713  QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
1714  q, SLOT(_q_emitItemChanged(QModelIndex)));
1715  // selection signals
1716  QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
1717  q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
1718  QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
1719  q, SIGNAL(itemSelectionChanged()));
1720  // sorting
1721  QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
1722  q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
1723  QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
1724 }
1725 
1727 {
1728  Q_Q(QTableWidget);
1729  if (QTableWidgetItem *item = tableModel()->item(index))
1730  emit q->itemPressed(item);
1731  emit q->cellPressed(index.row(), index.column());
1732 }
1733 
1735 {
1736  Q_Q(QTableWidget);
1737  if (QTableWidgetItem *item = tableModel()->item(index))
1738  emit q->itemClicked(item);
1739  emit q->cellClicked(index.row(), index.column());
1740 }
1741 
1743 {
1744  Q_Q(QTableWidget);
1745  if (QTableWidgetItem *item = tableModel()->item(index))
1746  emit q->itemDoubleClicked(item);
1747  emit q->cellDoubleClicked(index.row(), index.column());
1748 }
1749 
1751 {
1752  Q_Q(QTableWidget);
1753  if (QTableWidgetItem *item = tableModel()->item(index))
1754  emit q->itemActivated(item);
1755  emit q->cellActivated(index.row(), index.column());
1756 }
1757 
1759 {
1760  Q_Q(QTableWidget);
1761  if (QTableWidgetItem *item = tableModel()->item(index))
1762  emit q->itemEntered(item);
1763  emit q->cellEntered(index.row(), index.column());
1764 }
1765 
1767 {
1768  Q_Q(QTableWidget);
1769  if (QTableWidgetItem *item = tableModel()->item(index))
1770  emit q->itemChanged(item);
1771  emit q->cellChanged(index.row(), index.column());
1772 }
1773 
1775  const QModelIndex &previous)
1776 {
1777  Q_Q(QTableWidget);
1778  QTableWidgetItem *currentItem = tableModel()->item(current);
1779  QTableWidgetItem *previousItem = tableModel()->item(previous);
1780  if (currentItem || previousItem)
1781  emit q->currentItemChanged(currentItem, previousItem);
1782  emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());
1783 }
1784 
1786 {
1787  if (sortingEnabled) {
1788  int column = horizontalHeader->sortIndicatorSection();
1789  Qt::SortOrder order = horizontalHeader->sortIndicatorOrder();
1790  model->sort(column, order);
1791  }
1792 }
1793 
1795  const QModelIndex &bottomRight)
1796 {
1797  if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) {
1798  int column = horizontalHeader->sortIndicatorSection();
1799  if (column >= topLeft.column() && column <= bottomRight.column()) {
1800  Qt::SortOrder order = horizontalHeader->sortIndicatorOrder();
1801  tableModel()->ensureSorted(column, order, topLeft.row(), bottomRight.row());
1802  }
1803  }
1804 }
1805 
2000  : QTableView(*new QTableWidgetPrivate, parent)
2001 {
2002  Q_D(QTableWidget);
2003  QTableView::setModel(new QTableModel(0, 0, this));
2004  d->setup();
2005 }
2006 
2011  : QTableView(*new QTableWidgetPrivate, parent)
2012 {
2013  Q_D(QTableWidget);
2014  QTableView::setModel(new QTableModel(rows, columns, this));
2015  d->setup();
2016 }
2017 
2022 {
2023 }
2024 
2033 {
2034  Q_D(QTableWidget);
2035  d->tableModel()->setRowCount(rows);
2036 }
2037 
2042 int QTableWidget::rowCount() const
2043 {
2044  Q_D(const QTableWidget);
2045  return d->model->rowCount();
2046 }
2047 
2056 {
2057  Q_D(QTableWidget);
2058  d->tableModel()->setColumnCount(columns);
2059 }
2060 
2065 int QTableWidget::columnCount() const
2066 {
2067  Q_D(const QTableWidget);
2068  return d->model->columnCount();
2069 }
2070 
2075 {
2076  Q_D(const QTableWidget);
2077  return d->tableModel()->index(item).row();
2078 }
2079 
2084 {
2085  Q_D(const QTableWidget);
2086  return d->tableModel()->index(item).column();
2087 }
2088 
2089 
2097 {
2098  Q_D(const QTableWidget);
2099  return d->tableModel()->item(row, column);
2100 }
2101 
2121 {
2122  Q_D(QTableWidget);
2123  if (item) {
2124  if (item->view != 0) {
2125  qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget");
2126  } else {
2127  item->view = this;
2128  d->tableModel()->setItem(row, column, item);
2129  }
2130  } else {
2131  delete takeItem(row, column);
2132  }
2133 }
2134 
2139 {
2140  Q_D(QTableWidget);
2141  QTableWidgetItem *item = d->tableModel()->takeItem(row, column);
2142  if (item)
2143  item->view = 0;
2144  return item;
2145 }
2146 
2151 {
2152  Q_D(const QTableWidget);
2153  return d->tableModel()->verticalHeaderItem(row);
2154 }
2155 
2160 {
2161  Q_D(QTableWidget);
2162  if (item) {
2163  item->view = this;
2164  d->tableModel()->setVerticalHeaderItem(row, item);
2165  } else {
2166  delete takeVerticalHeaderItem(row);
2167  }
2168 }
2169 
2178 {
2179  Q_D(QTableWidget);
2180  QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row);
2181  if (itm)
2182  itm->view = 0;
2183  return itm;
2184 }
2185 
2191 {
2192  Q_D(const QTableWidget);
2193  return d->tableModel()->horizontalHeaderItem(column);
2194 }
2195 
2200 {
2201  Q_D(QTableWidget);
2202  if (item) {
2203  item->view = this;
2204  d->tableModel()->setHorizontalHeaderItem(column, item);
2205  } else {
2206  delete takeHorizontalHeaderItem(column);
2207  }
2208 }
2209 
2218 {
2219  Q_D(QTableWidget);
2220  QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column);
2221  if (itm)
2222  itm->view = 0;
2223  return itm;
2224 }
2225 
2230 {
2231  Q_D(QTableWidget);
2232  QTableModel *model = d->tableModel();
2233  QTableWidgetItem *item = 0;
2234  for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) {
2235  item = model->verticalHeaderItem(i);
2236  if (!item) {
2237  item = model->createItem();
2238  setVerticalHeaderItem(i, item);
2239  }
2240  item->setText(labels.at(i));
2241  }
2242 }
2243 
2248 {
2249  Q_D(QTableWidget);
2250  QTableModel *model = d->tableModel();
2251  QTableWidgetItem *item = 0;
2252  for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) {
2253  item = model->horizontalHeaderItem(i);
2254  if (!item) {
2255  item = model->createItem();
2256  setHorizontalHeaderItem(i, item);
2257  }
2258  item->setText(labels.at(i));
2259  }
2260 }
2261 
2268 {
2269  return currentIndex().row();
2270 }
2271 
2278 {
2279  return currentIndex().column();
2280 }
2281 
2288 {
2289  Q_D(const QTableWidget);
2290  return d->tableModel()->item(currentIndex());
2291 }
2292 
2302 {
2303  Q_D(QTableWidget);
2304  setCurrentIndex(d->tableModel()->index(item));
2305 }
2306 
2317 void QTableWidget::setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command)
2318 {
2319  Q_D(QTableWidget);
2320  d->selectionModel->setCurrentIndex(d->tableModel()->index(item), command);
2321 }
2322 
2338 {
2339  setCurrentIndex(model()->index(row, column, QModelIndex()));
2340 }
2341 
2353 void QTableWidget::setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command)
2354 {
2355  Q_D(QTableWidget);
2356  d->selectionModel->setCurrentIndex(model()->index(row, column, QModelIndex()), command);
2357 }
2358 
2363 {
2364  Q_D(QTableWidget);
2365  d->model->sort(column, order);
2366  horizontalHeader()->setSortIndicator(column, order);
2367 }
2368 
2373 {
2375 }
2376 
2381 {
2383 }
2384 
2390 {
2391  Q_D(QTableWidget);
2392  if (!item)
2393  return;
2394  edit(d->tableModel()->index(item));
2395 }
2396 
2403 {
2404  Q_D(QTableWidget);
2405  if (!item)
2406  return;
2407  QModelIndex index = d->tableModel()->index(item);
2409 }
2410 
2417 {
2418  Q_D(QTableWidget);
2419  if (!item)
2420  return;
2421  QModelIndex index = d->tableModel()->index(item);
2423 }
2424 
2438 {
2439  QModelIndex index = model()->index(row, column, QModelIndex());
2440  return QAbstractItemView::indexWidget(index);
2441 }
2442 
2461 {
2462  QModelIndex index = model()->index(row, column, QModelIndex());
2463  QAbstractItemView::setIndexWidget(index, widget);
2464 }
2465 
2475 {
2476  Q_D(const QTableWidget);
2477  QModelIndex index = d->tableModel()->index(item);
2478  return selectionModel()->isSelected(index);
2479 }
2480 
2489 {
2490  Q_D(QTableWidget);
2491  QModelIndex index = d->tableModel()->index(item);
2493 }
2494 
2499 {
2500  if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
2501  !model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
2502  return;
2503 
2504  QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
2505  QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
2506 
2507  selectionModel()->select(QItemSelection(topLeft, bottomRight),
2509 }
2510 
2518 {
2521  for (int i = 0; i < ranges.count(); ++i)
2522  result.append(QTableWidgetSelectionRange(ranges.at(i).top(),
2523  ranges.at(i).left(),
2524  ranges.at(i).bottom(),
2525  ranges.at(i).right()));
2526  return result;
2527 }
2528 
2540 {
2541  Q_D(QTableWidget);
2544  for (int i = 0; i < indexes.count(); ++i) {
2545  QModelIndex index = indexes.at(i);
2546  if (isIndexHidden(index))
2547  continue;
2548  QTableWidgetItem *item = d->tableModel()->item(index);
2549  if (item)
2550  items.append(item);
2551  }
2552  return items;
2553 }
2554 
2560 {
2561  Q_D(const QTableWidget);
2562  QModelIndexList indexes;
2563  for (int column = 0; column < columnCount(); ++column)
2564  indexes += d->model->match(model()->index(0, column, QModelIndex()),
2565  Qt::DisplayRole, text, -1, flags);
2567  for (int i = 0; i < indexes.size(); ++i)
2568  items.append(d->tableModel()->item(indexes.at(i)));
2569  return items;
2570 }
2571 
2576 int QTableWidget::visualRow(int logicalRow) const
2577 {
2578  return verticalHeader()->visualIndex(logicalRow);
2579 }
2580 
2585 int QTableWidget::visualColumn(int logicalColumn) const
2586 {
2587  return horizontalHeader()->visualIndex(logicalColumn);
2588 }
2589 
2600 {
2601  Q_D(const QTableWidget);
2602  return d->tableModel()->item(indexAt(p));
2603 }
2604 
2609 {
2610  Q_D(const QTableWidget);
2611  if (!item)
2612  return QRect();
2613  QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2614  Q_ASSERT(index.isValid());
2615  return visualRect(index);
2616 }
2617 
2625 {
2626  Q_D(QTableWidget);
2627  if (!item)
2628  return;
2629  QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2630  Q_ASSERT(index.isValid());
2631  QTableView::scrollTo(index, hint);
2632 }
2633 
2640 {
2641  Q_D(const QTableWidget);
2642  return d->tableModel()->itemPrototype();
2643 }
2644 
2659 {
2660  Q_D(QTableWidget);
2661  d->tableModel()->setItemPrototype(item);
2662 }
2663 
2668 {
2669  Q_D(QTableWidget);
2670  d->tableModel()->insertRows(row);
2671 }
2672 
2677 {
2678  Q_D(QTableWidget);
2679  d->tableModel()->insertColumns(column);
2680 }
2681 
2686 {
2687  Q_D(QTableWidget);
2688  d->tableModel()->removeRows(row);
2689 }
2690 
2695 {
2696  Q_D(QTableWidget);
2697  d->tableModel()->removeColumns(column);
2698 }
2699 
2707 {
2708  Q_D(QTableWidget);
2709  selectionModel()->clear();
2710  d->tableModel()->clear();
2711 }
2712 
2724 {
2725  Q_D(QTableWidget);
2726  selectionModel()->clear();
2727  d->tableModel()->clearContents();
2728 }
2729 
2737 {
2738  return d_func()->tableModel()->QAbstractTableModel::mimeTypes();
2739 }
2740 
2750 {
2751  return d_func()->tableModel()->internalMimeData();
2752 }
2753 
2763 {
2764  QModelIndex idx;
2765 #ifndef QT_NO_DRAGANDDROP
2767  // QAbstractTableModel::dropMimeData will overwrite on the index if row == -1 and column == -1
2768  idx = model()->index(row, column);
2769  row = -1;
2770  column = -1;
2771  }
2772 #endif
2773  return d_func()->tableModel()->QAbstractTableModel::dropMimeData(data, action , row, column, idx);
2774 }
2775 
2781 Qt::DropActions QTableWidget::supportedDropActions() const
2782 {
2783  return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
2784 }
2785 
2793 {
2795  if (twd)
2796  return twd->items;
2797  return QList<QTableWidgetItem*>();
2798 }
2799 
2805 {
2806  Q_D(const QTableWidget);
2807  return d->tableModel()->index(item);
2808 }
2809 
2815 {
2816  Q_D(const QTableWidget);
2817  return d->tableModel()->item(index);
2818 }
2819 
2824 {
2825  Q_ASSERT(!"QTableWidget::setModel() - Changing the model of the QTableWidget is not allowed.");
2826 }
2827 
2830 {
2831  return QTableView::event(e);
2832 }
2833 
2834 #ifndef QT_NO_DRAGANDDROP
2835 
2837  Q_D(QTableWidget);
2838  if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
2840  QModelIndex topIndex;
2841  int col = -1;
2842  int row = -1;
2843  if (d->dropOn(event, &row, &col, &topIndex)) {
2844  QModelIndexList indexes = selectedIndexes();
2845  int top = INT_MAX;
2846  int left = INT_MAX;
2847  for (int i = 0; i < indexes.count(); ++i) {
2848  top = qMin(indexes.at(i).row(), top);
2849  left = qMin(indexes.at(i).column(), left);
2850  }
2851 
2853  for (int i = 0; i < indexes.count(); ++i)
2854  taken.append(takeItem(indexes.at(i).row(), indexes.at(i).column()));
2855 
2856  for (int i = 0; i < indexes.count(); ++i) {
2857  QModelIndex index = indexes.at(i);
2858  int r = index.row() - top + topIndex.row();
2859  int c = index.column() - left + topIndex.column();
2860  setItem(r, c, taken.takeFirst());
2861  }
2862 
2863  event->accept();
2864  // Don't want QAbstractItemView to delete it because it was "moved" we already did it
2865  event->setDropAction(Qt::CopyAction);
2866  }
2867  }
2868 
2869  QTableView::dropEvent(event);
2870 }
2871 #endif
2872 
2874 
2875 #include "moc_qtablewidget.cpp"
2876 
2877 #endif // QT_NO_TABLEWIDGET
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool insertRows(int row, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count rows into the model before the given row.
double d
Definition: qnumeric_p.h:62
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
The QTableWidgetSelectionRange class provides a way to interact with selection in a model without usi...
Definition: qtablewidget.h:58
void dropEvent(QDropEvent *event)
Reimplemented Function
bool isIndexHidden(const QModelIndex &index) const
Reimplemented Function
void openPersistentEditor(const QModelIndex &index)
Opens a persistent editor on the item at the given index.
void removeColumn(int column)
Removes the column column and all its items from the table.
bool isSortingEnabled() const
int type
Definition: qmetatype.cpp:239
int left() const
Returns the column index corresponding to the leftmost selected column in the selection range...
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.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QDataStream & operator>>(QDataStream &in, QTableWidgetItem &item)
Reads a table widget item from stream in into item.
void clear()
Removes all the MIME type and data entries in the object.
Definition: qmimedata.cpp:613
#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.
QTableWidgetItem & operator=(const QTableWidgetItem &other)
Assigns other&#39;s data and flags to this item.
QPointer< QWidget > widget
int column() const
Returns the column of the item in the table.
Definition: qtablewidget.h:362
QModelIndex indexFromItem(QTableWidgetItem *item) const
Returns the QModelIndex assocated with the given item.
virtual void read(QDataStream &in)
Reads the item from stream in.
void setItemPrototype(const QTableWidgetItem *item)
~QTableWidget()
Destroys this QTableWidget.
bool isItemSelected(const QTableWidgetItem *item) const
Returns true if the item is selected, otherwise returns false.
bool setData(const QModelIndex &index, const QVariant &value, int role)
Sets the role data for the item at index to value.
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
void setVerticalHeaderLabels(const QStringList &labels)
Sets the vertical header labels using labels.
static bool itemGreaterThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
void setVerticalHeaderItem(int row, QTableWidgetItem *item)
Sets the vertical header item for row row to item.
#define it(className, varName)
int sortIndicatorSection() const
Returns the logical index of the section that has a sort indicator.
const QTableWidgetItem * itemPrototype() const
Returns the item prototype used by the table.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
int visualIndex(int logicalIndex) const
Returns the visual index position of the section specified by the given logicalIndex, or -1 otherwise.
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this view.
bool event(QEvent *e)
Reimplemented Function
void setModel(QAbstractItemModel *model)
Reimplemented Function
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...
const QTableWidgetItem * itemPrototype() const
void sortItems(int column, Qt::SortOrder order=Qt::AscendingOrder)
Sorts all the rows in the table widget based on column and order.
QModelIndexList selectedIndexes() const
Returns a list of all selected model item indexes.
QVector< QTableWidgetItem * > verticalHeaderItems
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
Sets the data for the given role and section in the header with the specified orientation to the valu...
void setCurrentCell(int row, int column)
Sets the current cell to be the cell at position (row, column).
#define SLOT(a)
Definition: qobjectdefs.h:226
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
int bottomRow() const
Returns the bottom row of the range.
Definition: qtablewidget.h:67
T1 first
Definition: qpair.h:65
QRect visualRect(const QModelIndex &index) const
Returns the rectangle on the viewport occupied by the given index.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
DragDropMode dragDropMode() const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
bool removeRows(int row, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, removes count rows starting with the given row under parent parent from ...
QHeaderView * horizontalHeader() const
Returns the table view&#39;s horizontal header.
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...
const QItemSelection selection() const
Returns the selection ranges stored in the selection model.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Reimplemented Function
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. ...
QTableWidget * view
Definition: qtablewidget.h:186
void ensureSorted(int column, Qt::SortOrder order, int start, int end)
void setFlags(Qt::ItemFlags flags)
Sets the flags for the item to the given flags.
bool removeColumns(int column, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, removes count columns starting with the given column under parent parent...
long tableIndex(int row, int column) const
int bottom() const
Returns the row index corresponding to the lowermost selected row in the selection range...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
bool hasIndex(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns true if the model returns a valid QModelIndex for row and column with parent, otherwise returns false.
int right() const
Returns the column index corresponding to the rightmost selected column in the selection range...
int visualRow(int logicalRow) const
Returns the visual row of the given logicalRow.
void endInsertRows()
Ends a row insertion operation.
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles)
Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole...
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QList< QTableWidgetSelectionRange > selectedRanges() const
Returns a list of all selected ranges.
T * qobject_cast(QObject *object)
Definition: qobject.h:375
void setItem(int row, int column, QTableWidgetItem *item)
void openPersistentEditor(QTableWidgetItem *item)
Opens an editor for the give item.
DropIndicatorPosition dropIndicatorPosition() const
Returns the position of the drop indicator in relation to the closest item.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QTableWidgetItem * takeVerticalHeaderItem(int section)
void removeRow(int row)
Removes the row row and all its items from the table.
#define Q_D(Class)
Definition: qglobal.h:2482
~QTableWidgetSelectionRange()
Destroys the table selection range.
QVector< QWidgetItemData > values
Definition: qtablewidget.h:185
void _q_emitItemActivated(const QModelIndex &index)
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
QWidget * indexWidget(const QModelIndex &index) const
Returns the widget for the item at the given index.
void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow)
void setCellWidget(int row, int column, QWidget *widget)
Sets the given widget to be displayed in the cell in the given row and column, passing the ownership ...
void _q_emitItemChanged(const QModelIndex &index)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void setItemSelected(const QTableWidgetItem *item, bool select)
Selects or deselects item depending on select.
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 setItemPrototype(const QTableWidgetItem *item)
Sets the item prototype for the table to the specified item.
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QDataStream & operator<<(QDataStream &out, const QTableWidgetItem &item)
Writes the table widget item item to stream out.
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
QTableWidgetItem * verticalHeaderItem(int row) const
Returns the vertical header item for row row.
int leftColumn() const
Returns the left column of the range.
Definition: qtablewidget.h:68
QTableWidgetItemPrivate * d
Definition: qtablewidget.h:187
void dropEvent(QDropEvent *event)
This function is called with the given event when a drop event occurs over the widget.
void setSortingEnabled(bool enable)
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...
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void editItem(QTableWidgetItem *item)
Starts editing the item if it is editable.
QTableWidgetItem * item(int row, int column) const
QTableWidgetItem * takeVerticalHeaderItem(int row)
Removes the vertical header item at row from the header without deleting it.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
SortOrder
Definition: qnamespace.h:189
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
const QTableWidgetItem * prototype
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void clearContents()
QRect visualItemRect(const QTableWidgetItem *item) const
Returns the rectangle on the viewport occupied by the item at item.
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
void setCurrentItem(QTableWidgetItem *item)
Sets the current item to item.
int rowCount() const
virtual bool operator<(const QTableWidgetItem &other) const
Returns true if the item is less than the other item; otherwise returns false.
void _q_emitItemClicked(const QModelIndex &index)
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
virtual ~QTableWidgetItem()
Destroys the table item.
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.
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
bool isSelected(const QModelIndex &index) const
Returns true if the given model item index is selected.
int columnCount() const
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.
QTableWidgetItem * takeHorizontalHeaderItem(int column)
Removes the horizontal header item at column from the header without deleting it. ...
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
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int currentColumn() const
Returns the column of the current item.
virtual QVariant data(int role) const
Returns the item&#39;s data for the given role.
virtual QTableWidgetItem * clone() const
Creates a copy of the item.
QTableWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the given point, or returns 0 if point is not covered by an item in ...
QWidgetData * data
Definition: qwidget.h:815
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.
Q_CORE_EXPORT void qWarning(const char *,...)
void closePersistentEditor(QTableWidgetItem *item)
Closes the persistent editor for item.
void setRangeSelected(const QTableWidgetSelectionRange &range, bool select)
Selects or deselects the range depending on select.
void _q_emitItemEntered(const QModelIndex &index)
Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
QHeaderView * verticalHeader() const
Returns the table view&#39;s vertical header.
QModelIndex currentIndex() const
Returns the model index of the current item.
static bool itemLessThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
friend class iterator
Definition: qlist.h:226
DropAction
Definition: qnamespace.h:1597
void endRemoveRows()
Ends a row removal operation.
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
QTableWidgetItem * horizontalHeaderItem(int column) const
Returns the horizontal header item for column, column, if one has been set; otherwise returns 0...
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
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
QString text() const
Returns the item&#39;s text.
Definition: qtablewidget.h:107
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
void setColumnCount(int columns)
Sets the number of columns in this table&#39;s model to columns.
void sort(int column, Qt::SortOrder order)
Sorts the model by column in the given order.
QList< QTableWidgetItem * > items(const QMimeData *data) const
Returns a list of pointers to the items contained in the data object.
The QTableView class provides a default model/view implementation of a table view.
Definition: qtableview.h:58
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QTableWidgetItem * verticalHeaderItem(int section)
void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current)
QVector< QTableWidgetItem * > horizontalHeaderItems
void setRowCount(int rows)
Sets the number of rows in this table&#39;s model to rows.
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.
QVector< QTableWidgetItem * > columnItems(int column) const
Qt::ItemFlags flags() const
Returns the flags used to describe the item.
Definition: qtablewidget.h:104
friend class QTableModel
Definition: qtablewidget.h:226
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
void setColumnCount(int columns)
static QByteArray prototype(const QList< QByteArray > &parameterTypes, const QList< QByteArray > &parameterNames, bool *ok)
Definition: qaxserver.cpp:685
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
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
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
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Returns the data for the given role and section in the header with the specified orientation.
void setModel(QAbstractItemModel *model)
virtual bool dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
Handles the data supplied by a drag and drop operation that ended with the given action in the given ...
QTableWidgetItem(int type=Type)
Constructs a table item of the specified type that does not belong to any table.
QTableWidgetItem * takeItem(int row, int column)
Removes the item at row and column from the table without deleting it.
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void setRowCount(int rows)
QTableWidgetItem * item(int row, int column) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
virtual QMimeData * mimeData(const QList< QTableWidgetItem *> items) const
Returns an object that contains a serialized description of the specified items.
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
void insertColumn(int column)
Inserts an empty column into the table at column.
void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible)
Makes sure that the given item is visible in the table view, scrolling if necessary.
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
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QModelIndexList cachedIndexes
QTableWidgetItem * createItem() const
void setHorizontalHeaderLabels(const QStringList &labels)
Sets the horizontal header labels using labels.
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setHorizontalHeaderItem(int section, QTableWidgetItem *item)
int row(const QTableWidgetItem *item) const
Returns the row for the item.
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
int visualColumn(int logicalColumn) const
Returns the visual column of the given logicalColumn.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
bool isValid(const QModelIndex &index) const
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
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data stored under the given role for the item referred to by the index.
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
Definition: qnamespace.h:54
void edit(const QModelIndex &index)
Starts editing the item corresponding to the given index if it is editable.
void setHorizontalHeaderItem(int column, QTableWidgetItem *item)
Sets the horizontal header item for column column to item.
QTableWidgetItem * horizontalHeaderItem(int section)
QMimeData * internalMimeData() const
void _q_emitItemDoubleClicked(const QModelIndex &index)
QTableWidgetItem * takeHorizontalHeaderItem(int section)
void endInsertColumns()
Ends a column insertion operation.
quint16 index
QList< QTableWidgetItem * > items
int top() const
Returns the row index corresponding to the uppermost selected row in the selection range...
void setSortingEnabled(bool enable)
If enabled true enables sorting for the table and immediately trigger a call to sortByColumn() with t...
QTableWidget(QWidget *parent=0)
Creates a new table view with the given parent.
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of tablewidget items.
int topRow() const
Returns the top row of the range.
Definition: qtablewidget.h:66
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void setText(const QString &text)
Sets the item&#39;s text to the text specified.
Definition: qtablewidget.h:191
void insertRow(int row)
Inserts an empty row into the table at row.
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
void closePersistentEditor(const QModelIndex &index)
Closes the persistent editor for the item at the given index.
int currentRow() const
Returns the row of the current item.
QTableWidgetItem * currentItem() const
Returns the current item.
void setSortIndicator(int logicalIndex, Qt::SortOrder order)
Sets the sort indicator for the section specified by the given logicalIndex in the direction specifie...
int type() const
Returns the type passed to the QTableWidgetItem constructor.
Definition: qtablewidget.h:181
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
int rightColumn() const
Returns the right column of the range.
Definition: qtablewidget.h:69
QModelIndex rootIndex() const
Returns the model index of the model&#39;s root item.
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.
bool(* LessThan)(const QPair< QListWidgetItem *, int > &, const QPair< QListWidgetItem *, int > &)
Definition: qlistwidget.cpp:53
static const KeyPair *const end
void _q_emitItemPressed(const QModelIndex &index)
QWidget * cellWidget(int row, int column) const
Returns the widget displayed in the cell in the given row and column.
static QVector< QTableWidgetItem * >::iterator sortedInsertionIterator(const QVector< QTableWidgetItem *>::iterator &begin, const QVector< QTableWidgetItem *>::iterator &end, Qt::SortOrder order, QTableWidgetItem *item)
Orientation
Definition: qnamespace.h:174
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::SortOrder sortIndicatorOrder() const
Returns the order for the sort indicator.
bool event(QEvent *event)
Reimplemented Function
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
bool insertColumns(int column, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count new columns into the model before the given column...
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
bool isSortingEnabled() const
QIcon icon() const
Returns the item&#39;s icon.
Definition: qtablewidget.h:111
QTableWidgetItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QTableWidgetItem assocated with the given index.
void scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint=EnsureVisible)
Scrolls the view if necessary to ensure that the item is visible.
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
QModelIndex indexAt(const QPoint &p) const
Returns the index position of the model item corresponding to the table item at position pos in conte...
void clearContents()
Removes all items not in the headers from the view.
#define INT_MAX
QModelIndexList selectedIndexes() const
Reimplemented Function
QList< QTableWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags) const
Finds items that matches the text using the given flags.
virtual void setData(int role, const QVariant &value)
Sets the item&#39;s data for the given role to the specified value.
The QAbstractTableModel class provides an abstract model that can be subclassed to create table model...
QList< QTableWidgetItem * > selectedItems()
Returns a list of all selected items.
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 reset()
Resets the model to its original state in any attached views.
QTableWidgetItem * takeItem(int row, int column)
#define text
Definition: qobjectdefs.h:80
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
Qt::ItemFlags itemFlags
Definition: qtablewidget.h:188
int column() const
Returns the column this model index refers to.
void setVerticalHeaderItem(int section, QTableWidgetItem *item)
int column(const QTableWidgetItem *item) const
Returns the column for the item.
void endRemoveColumns()
Ends a column removal operation.
void itemChanged(QTableWidgetItem *item)
void removeItem(QTableWidgetItem *item)
void clear()
Removes all items in the view.
QTableWidgetSelectionRange()
Constructs an table selection range, i.e.
virtual void write(QDataStream &out) const
Writes the item to stream out.
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60
QTableModel(int rows, int columns, QTableWidget *parent)