Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtablewidget.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtablewidget.h"
5
6#include <qpainter.h>
7#include <private/qtablewidget_p.h>
8
9#include <algorithm>
10
12
13QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
14 : QAbstractTableModel(parent),
15 prototype(nullptr),
16 tableItems(rows * columns, 0),
17 verticalHeaderItems(rows, 0),
18 horizontalHeaderItems(columns, 0)
19{}
20
22{
23 clear();
24 delete prototype;
25}
26
28{
29 if (count < 1 || row < 0 || row > verticalHeaderItems.size())
30 return false;
31
33 int rc = verticalHeaderItems.size();
34 int cc = horizontalHeaderItems.size();
35 verticalHeaderItems.insert(row, count, 0);
36 if (rc == 0)
37 tableItems.resize(cc * count);
38 else
39 tableItems.insert(tableIndex(row, 0), cc * count, 0);
41 return true;
42}
43
45{
46 if (count < 1 || column < 0 || column > horizontalHeaderItems.size())
47 return false;
48
50 int rc = verticalHeaderItems.size();
51 int cc = horizontalHeaderItems.size();
52 horizontalHeaderItems.insert(column, count, 0);
53 if (cc == 0)
54 tableItems.resize(rc * count);
55 else
56 for (int row = 0; row < rc; ++row)
57 tableItems.insert(tableIndex(row, column), count, 0);
59 return true;
60}
61
63{
64 if (count < 1 || row < 0 || row + count > verticalHeaderItems.size())
65 return false;
66
68 int i = tableIndex(row, 0);
69 int n = count * columnCount();
70 QTableWidgetItem *oldItem = nullptr;
71 for (int j = i; j < n + i; ++j) {
72 oldItem = tableItems.at(j);
73 if (oldItem)
74 oldItem->view = nullptr;
75 delete oldItem;
76 }
77 tableItems.remove(qMax(i, 0), n);
78 for (int v = row; v < row + count; ++v) {
79 oldItem = verticalHeaderItems.at(v);
80 if (oldItem)
81 oldItem->view = nullptr;
82 delete oldItem;
83 }
84 verticalHeaderItems.remove(row, count);
86 return true;
87}
88
90{
91 if (count < 1 || column < 0 || column + count > horizontalHeaderItems.size())
92 return false;
93
95 QTableWidgetItem *oldItem = nullptr;
96 for (int row = rowCount() - 1; row >= 0; --row) {
97 int i = tableIndex(row, column);
98 for (int j = i; j < i + count; ++j) {
99 oldItem = tableItems.at(j);
100 if (oldItem)
101 oldItem->view = nullptr;
102 delete oldItem;
103 }
104 tableItems.remove(i, count);
105 }
106 for (int h=column; h<column+count; ++h) {
107 oldItem = horizontalHeaderItems.at(h);
108 if (oldItem)
109 oldItem->view = nullptr;
110 delete oldItem;
111 }
112 horizontalHeaderItems.remove(column, count);
114 return true;
115}
116
118{
119 int i = tableIndex(row, column);
120 if (i < 0 || i >= tableItems.size())
121 return;
122 QTableWidgetItem *oldItem = tableItems.at(i);
123 if (item == oldItem)
124 return;
125
126 // remove old
127 if (oldItem)
128 oldItem->view = nullptr;
129 delete tableItems.at(i);
130
131 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
132
133 // set new
134 if (item)
135 item->d->id = i;
136 tableItems[i] = item;
137
138 if (view && view->isSortingEnabled()
139 && view->horizontalHeader()->sortIndicatorSection() == column) {
140 // sorted insertion
141 Qt::SortOrder order = view->horizontalHeader()->sortIndicatorOrder();
142 QList<QTableWidgetItem *> colItems = columnItems(column);
143 if (row < colItems.size())
144 colItems.remove(row);
145 int sortedRow;
146 if (item == nullptr) {
147 // move to after all non-0 (sortable) items
148 sortedRow = colItems.size();
149 } else {
151 it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
152 sortedRow = qMax((int)(it - colItems.begin()), 0);
153 }
154 if (sortedRow != row) {
156 // move the items @ row to sortedRow
157 int cc = columnCount();
158 QList<QTableWidgetItem *> rowItems(cc);
159 for (int j = 0; j < cc; ++j)
160 rowItems[j] = tableItems.at(tableIndex(row, j));
161 tableItems.remove(tableIndex(row, 0), cc);
162 tableItems.insert(tableIndex(sortedRow, 0), cc, 0);
163 for (int j = 0; j < cc; ++j)
164 tableItems[tableIndex(sortedRow, j)] = rowItems.at(j);
165 QTableWidgetItem *header = verticalHeaderItems.at(row);
166 verticalHeaderItems.remove(row);
167 verticalHeaderItems.insert(sortedRow, header);
168 // update persistent indexes
169 QModelIndexList oldPersistentIndexes = persistentIndexList();
170 QModelIndexList newPersistentIndexes = oldPersistentIndexes;
171 updateRowIndexes(newPersistentIndexes, row, sortedRow);
172 changePersistentIndexList(oldPersistentIndexes,
173 newPersistentIndexes);
174
176 return;
177 }
178 }
180 emit dataChanged(idx, idx);
181}
182
184{
185 long i = tableIndex(row, column);
186 QTableWidgetItem *itm = tableItems.value(i);
187 if (itm) {
188 itm->view = nullptr;
189 itm->d->id = -1;
190 tableItems[i] = 0;
191 const QModelIndex ind = index(row, column);
192 emit dataChanged(ind, ind);
193 }
194 return itm;
195}
196
198{
199 return item(index(row, column));
200}
201
203{
204 if (!isValid(index))
205 return nullptr;
206 return tableItems.at(tableIndex(index.row(), index.column()));
207}
208
210{
211 int i = tableItems.indexOf(item);
212 if (i != -1) {
213 QModelIndex idx = index(item);
214 tableItems[i] = nullptr;
215 emit dataChanged(idx, idx);
216 return;
217 }
218
219 i = verticalHeaderItems.indexOf(item);
220
221 if (i != -1) {
222 verticalHeaderItems[i] = 0;
224 return;
225 }
226 i = horizontalHeaderItems.indexOf(item);
227 if (i != -1) {
228 horizontalHeaderItems[i] = 0;
230 return;
231 }
232}
233
235{
236 if (section < 0 || section >= horizontalHeaderItems.size())
237 return;
238 QTableWidgetItem *oldItem = horizontalHeaderItems.at(section);
239 if (item == oldItem)
240 return;
241
242 if (oldItem)
243 oldItem->view = nullptr;
244 delete oldItem;
245
246 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
247
248 if (item) {
249 item->view = view;
250 item->d->headerItem = true;
251 }
252 horizontalHeaderItems[section] = item;
253 emit headerDataChanged(Qt::Horizontal, section, section);
254}
255
257{
258 if (section < 0 || section >= verticalHeaderItems.size())
259 return;
260 QTableWidgetItem *oldItem = verticalHeaderItems.at(section);
261 if (item == oldItem)
262 return;
263
264 if (oldItem)
265 oldItem->view = nullptr;
266 delete oldItem;
267
268 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
269
270 if (item) {
271 item->view = view;
272 item->d->headerItem = true;
273 }
274 verticalHeaderItems[section] = item;
275 emit headerDataChanged(Qt::Vertical, section, section);
276}
277
279{
280 if (section < 0 || section >= horizontalHeaderItems.size())
281 return nullptr;
282 QTableWidgetItem *itm = horizontalHeaderItems.at(section);
283 if (itm) {
284 itm->view = nullptr;
285 itm->d->headerItem = false;
286 horizontalHeaderItems[section] = 0;
287 }
288 return itm;
289}
290
292{
293 if (section < 0 || section >= verticalHeaderItems.size())
294 return nullptr;
295 QTableWidgetItem *itm = verticalHeaderItems.at(section);
296 if (itm) {
297 itm->view = nullptr;
298 itm->d->headerItem = false;
299 verticalHeaderItems[section] = 0;
300 }
301 return itm;
302}
303
305{
306 return horizontalHeaderItems.value(section);
307}
308
310{
311 return verticalHeaderItems.value(section);
312}
313
315{
316 if (!item)
317 return QModelIndex();
318 int i = -1;
319 const int id = item->d->id;
320 if (id >= 0 && id < tableItems.size() && tableItems.at(id) == item) {
321 i = id;
322 } else { // we need to search for the item
323 i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item));
324 if (i == -1) // not found
325 return QModelIndex();
326 }
327 int row = i / columnCount();
328 int col = i % columnCount();
329 return QAbstractTableModel::index(row, col);
330}
331
333{
334 int rc = verticalHeaderItems.size();
335 if (rows < 0 || rc == rows)
336 return;
337 if (rc < rows)
338 insertRows(qMax(rc, 0), rows - rc);
339 else
340 removeRows(qMax(rows, 0), rc - rows);
341}
342
344{
345 int cc = horizontalHeaderItems.size();
346 if (columns < 0 || cc == columns)
347 return;
348 if (cc < columns)
349 insertColumns(qMax(cc, 0), columns - cc);
350 else
351 removeColumns(qMax(columns, 0), cc - columns);
352}
353
354int QTableModel::rowCount(const QModelIndex &parent) const
355{
356 return parent.isValid() ? 0 : verticalHeaderItems.size();
357}
358
359int QTableModel::columnCount(const QModelIndex &parent) const
360{
361 return parent.isValid() ? 0 : horizontalHeaderItems.size();
362}
363
365{
367 if (itm)
368 return itm->data(role);
369 return QVariant();
370}
371
373{
374 if (!index.isValid())
375 return false;
376
378 if (itm) {
379 itm->setData(role, value);
380 return true;
381 }
382
383 // don't create dummy table items for empty values
384 if (!value.isValid())
385 return false;
386
387 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
388 if (!view)
389 return false;
390
391 itm = createItem();
392 itm->setData(role, value);
393 view->setItem(index.row(), index.column(), itm);
394 return true;
395}
396
397QMap<int, QVariant> QTableModel::itemData(const QModelIndex &index) const
398{
399 QMap<int, QVariant> roles;
401 if (itm) {
402 for (int i = 0; i < itm->values.size(); ++i) {
403 roles.insert(itm->values.at(i).role,
404 itm->values.at(i).value);
405 }
406 }
407 return roles;
408}
409
410// reimplemented to ensure that only one dataChanged() signal is emitted
411bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
412{
413 if (!index.isValid())
414 return false;
415
416 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
418 if (itm) {
419 itm->view = nullptr; // prohibits item from calling itemChanged()
420 QList<int> rolesVec;
421 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
422 const int role = (it.key() == Qt::EditRole ? Qt::DisplayRole : it.key());
423 if (itm->data(role) != it.value()) {
424 itm->setData(role, it.value());
425 rolesVec += role;
426 if (role == Qt::DisplayRole)
427 rolesVec += Qt::EditRole;
428 }
429 }
430 itm->view = view;
431 if (!rolesVec.isEmpty())
432 itemChanged(itm, rolesVec);
433 return true;
434 }
435
436 if (!view)
437 return false;
438
439 itm = createItem();
440 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
441 itm->setData(it.key(), it.value());
442 view->setItem(index.row(), index.column(), itm);
443 return true;
444}
445
447{
449 return false;
451 if (!itm)
452 return false;
453 const auto beginIter = itm->values.cbegin();
454 const auto endIter = itm->values.cend();
455 if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
456 return true; //it's already cleared
457 itm->values.clear();
458 emit dataChanged(index, index, QList<int> {});
459 return true;
460}
461
462Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
463{
464 if (!index.isValid())
466 if (QTableWidgetItem *itm = item(index))
467 return itm->flags();
468 return (Qt::ItemIsEditable
474}
475
477{
478 QList<QPair<QTableWidgetItem *, int>> sortable;
479 QList<int> unsortable;
480
481 sortable.reserve(rowCount());
482 unsortable.reserve(rowCount());
483
484 for (int row = 0; row < rowCount(); ++row) {
485 if (QTableWidgetItem *itm = item(row, column))
486 sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
487 else
488 unsortable.append(row);
489 }
490
492 std::stable_sort(sortable.begin(), sortable.end(), compare);
493
494 QList<QTableWidgetItem *> sorted_table(tableItems.size());
495 QModelIndexList from;
497 const int numRows = rowCount();
498 const int numColumns = columnCount();
499 from.reserve(numRows * numColumns);
500 to.reserve(numRows * numColumns);
501 for (int i = 0; i < numRows; ++i) {
502 int r = (i < sortable.size()
503 ? sortable.at(i).second
504 : unsortable.at(i - sortable.size()));
505 for (int c = 0; c < numColumns; ++c) {
506 sorted_table[tableIndex(i, c)] = item(r, c);
507 from.append(createIndex(r, c));
508 to.append(createIndex(i, c));
509 }
510 }
511
513
514 tableItems = sorted_table;
515 changePersistentIndexList(from, to); // ### slow
516
518}
519
520/*
521 \internal
522
523 Ensures that rows in the interval [start, end] are
524 sorted according to the contents of column \a column
525 and the given sort \a order.
526*/
528 int start, int end)
529{
530 int count = end - start + 1;
531 QList<QPair<QTableWidgetItem *, int>> sorting;
532 sorting.reserve(count);
533 for (int row = start; row <= end; ++row) {
535 if (itm == nullptr) {
536 // no more sortable items (all 0-items are
537 // at the end of the table when it is sorted)
538 break;
539 }
540 sorting.append(QPair<QTableWidgetItem*,int>(itm, row));
541 }
542
544 std::stable_sort(sorting.begin(), sorting.end(), compare);
545 QModelIndexList oldPersistentIndexes, newPersistentIndexes;
546 QList<QTableWidgetItem *> newTable = tableItems;
547 QList<QTableWidgetItem *> newVertical = verticalHeaderItems;
548 QList<QTableWidgetItem *> colItems = columnItems(column);
549 QList<QTableWidgetItem *>::iterator vit = colItems.begin();
550 qsizetype distanceFromBegin = 0;
551 bool changed = false;
552 for (int i = 0; i < sorting.size(); ++i) {
553 distanceFromBegin = std::distance(colItems.begin(), vit);
554 int oldRow = sorting.at(i).second;
555 QTableWidgetItem *item = colItems.at(oldRow);
556 colItems.remove(oldRow);
557 vit = sortedInsertionIterator(colItems.begin() + distanceFromBegin, colItems.end(), order,
558 item);
559 int newRow = qMax((int)(vit - colItems.begin()), 0);
560 if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item))
561 newRow = oldRow;
562 vit = colItems.insert(vit, item);
563 if (newRow != oldRow) {
564 if (!changed) {
566 oldPersistentIndexes = persistentIndexList();
567 newPersistentIndexes = oldPersistentIndexes;
568 changed = true;
569 }
570 // move the items @ oldRow to newRow
571 int cc = columnCount();
572 QList<QTableWidgetItem *> rowItems(cc);
573 for (int j = 0; j < cc; ++j)
574 rowItems[j] = newTable.at(tableIndex(oldRow, j));
575 newTable.remove(tableIndex(oldRow, 0), cc);
576 newTable.insert(tableIndex(newRow, 0), cc, 0);
577 for (int j = 0; j < cc; ++j)
578 newTable[tableIndex(newRow, j)] = rowItems.at(j);
579 QTableWidgetItem *header = newVertical.at(oldRow);
580 newVertical.remove(oldRow);
581 newVertical.insert(newRow, header);
582 // update persistent indexes
583 updateRowIndexes(newPersistentIndexes, oldRow, newRow);
584 // the index of the remaining rows may have changed
585 for (int j = i + 1; j < sorting.size(); ++j) {
586 int otherRow = sorting.at(j).second;
587 if (oldRow < otherRow && newRow >= otherRow)
588 --sorting[j].second;
589 else if (oldRow > otherRow && newRow <= otherRow)
590 ++sorting[j].second;
591 }
592 }
593 }
594
595 if (changed) {
596 tableItems = newTable;
597 verticalHeaderItems = newVertical;
598 changePersistentIndexList(oldPersistentIndexes,
599 newPersistentIndexes);
601 }
602}
603
604/*
605 \internal
606
607 Returns the non-0 items in column \a column.
608*/
609QList<QTableWidgetItem *> QTableModel::columnItems(int column) const
610{
611 QList<QTableWidgetItem *> items;
612 int rc = rowCount();
613 items.reserve(rc);
614 for (int row = 0; row < rc; ++row) {
616 if (itm == nullptr) {
617 // no more sortable items (all 0-items are
618 // at the end of the table when it is sorted)
619 break;
620 }
621 items.append(itm);
622 }
623 return items;
624}
625
626/*
627 \internal
628
629 Adjusts the row of each index in \a indexes if necessary, given
630 that a row of items has been moved from row \a movedFrom to row
631 \a movedTo.
632*/
634 int movedFromRow, int movedToRow)
635{
636 QModelIndexList::iterator it;
637 for (it = indexes.begin(); it != indexes.end(); ++it) {
638 int oldRow = (*it).row();
639 int newRow = oldRow;
640 if (oldRow == movedFromRow)
641 newRow = movedToRow;
642 else if (movedFromRow < oldRow && movedToRow >= oldRow)
643 newRow = oldRow - 1;
644 else if (movedFromRow > oldRow && movedToRow <= oldRow)
645 newRow = oldRow + 1;
646 if (newRow != oldRow)
647 *it = index(newRow, (*it).column(), (*it).parent());
648 }
649}
650
651/*
652 \internal
653
654 Returns an iterator to the item where \a item should be
655 inserted in the interval (\a begin, \a end) according to
656 the given sort \a order.
657*/
667
668bool QTableModel::itemLessThan(const QPair<QTableWidgetItem*,int> &left,
669 const QPair<QTableWidgetItem*,int> &right)
670{
671 return *(left.first) < *(right.first);
672}
673
674bool QTableModel::itemGreaterThan(const QPair<QTableWidgetItem*,int> &left,
675 const QPair<QTableWidgetItem*,int> &right)
676{
677 return (*(right.first) < *(left .first));
678}
679
680QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
681{
682 if (section < 0)
683 return QVariant();
684
685 QTableWidgetItem *itm = nullptr;
686 if (orientation == Qt::Horizontal && section < horizontalHeaderItems.size())
687 itm = horizontalHeaderItems.at(section);
688 else if (orientation == Qt::Vertical && section < verticalHeaderItems.size())
689 itm = verticalHeaderItems.at(section);
690 else
691 return QVariant(); // section is out of bounds
692
693 if (itm)
694 return itm->data(role);
695 if (role == Qt::DisplayRole)
696 return section + 1;
697 return QVariant();
698}
699
700bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
701 const QVariant &value, int role)
702{
703 if (section < 0 ||
704 (orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) ||
705 (orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
706 return false;
707
708 QTableWidgetItem *itm = nullptr;
709 if (orientation == Qt::Horizontal)
710 itm = horizontalHeaderItems.at(section);
711 else
712 itm = verticalHeaderItems.at(section);
713 if (itm) {
714 itm->setData(role, value);
715 return true;
716 }
717 return false;
718}
719
721{
722 return (index.isValid()
723 && index.row() < verticalHeaderItems.size()
724 && index.column() < horizontalHeaderItems.size());
725}
726
728{
729 for (int j = 0; j < verticalHeaderItems.size(); ++j) {
730 if (verticalHeaderItems.at(j)) {
731 verticalHeaderItems.at(j)->view = nullptr;
732 delete verticalHeaderItems.at(j);
733 verticalHeaderItems[j] = 0;
734 }
735 }
736 for (int k = 0; k < horizontalHeaderItems.size(); ++k) {
737 if (horizontalHeaderItems.at(k)) {
738 horizontalHeaderItems.at(k)->view = nullptr;
739 delete horizontalHeaderItems.at(k);
740 horizontalHeaderItems[k] = 0;
741 }
742 }
744}
745
747{
749 for (int i = 0; i < tableItems.size(); ++i) {
750 if (tableItems.at(i)) {
751 tableItems.at(i)->view = nullptr;
752 delete tableItems.at(i);
753 tableItems[i] = 0;
754 }
755 }
757}
758
759void QTableModel::itemChanged(QTableWidgetItem *item, const QList<int> &roles)
760{
761 if (!item)
762 return;
763 if (item->d->headerItem) {
764 int row = verticalHeaderItems.indexOf(item);
765 if (row >= 0) {
767 } else {
768 int column = horizontalHeaderItems.indexOf(item);
769 if (column >= 0)
771 }
772 } else {
773 QModelIndex idx = index(item);
774 if (idx.isValid())
775 emit dataChanged(idx, idx, roles);
776 }
777}
778
780{
781 return prototype ? prototype->clone() : new QTableWidgetItem;
782}
783
785{
786 return prototype;
787}
788
790{
791 if (prototype != item) {
792 delete prototype;
793 prototype = item;
794 }
795}
796
798{
799 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
800 return (view ? view->mimeTypes() : QStringList());
801}
802
804{
805 return QAbstractTableModel::mimeData(cachedIndexes);
806}
807
809{
810 QList<QTableWidgetItem*> items;
811 const int indexesCount = indexes.size();
812 items.reserve(indexesCount);
813 for (int i = 0; i < indexesCount; ++i)
814 items << item(indexes.at(i));
815 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
816
817 // cachedIndexes is a little hack to avoid copying from QModelIndexList to
818 // QList<QTreeWidgetItem*> and back again in the view
819 cachedIndexes = indexes;
820 QMimeData *mimeData = (view ? view->mimeData(items) : nullptr);
821 cachedIndexes.clear();
822 return mimeData;
823}
824
826 int row , int column, const QModelIndex &index)
827{
828 if (index.isValid()) {
829 row = index.row();
830 column = index.column();
831 }else if (row == -1 || column == -1) { // The user dropped outside the table.
832 row = rowCount();
833 column = 0;
834 }
835
836 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
837 return (view ? view->dropMimeData(row, column, data, action) : false);
838}
839
841{
842 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
843 return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
844}
845
1062{
1063 if (!view || !view->selectionModel())
1064 return false;
1065 const QTableModel *model = qobject_cast<const QTableModel*>(view->model());
1066 if (!model)
1067 return false;
1068 const QModelIndex index = model->index(this);
1069 return view->selectionModel()->isSelected(index);
1070}
1071
1081{
1082 if (!view || !view->selectionModel())
1083 return;
1084 const QTableModel *model = qobject_cast<const QTableModel*>(view->model());
1085 if (!model)
1086 return;
1087 const QModelIndex index = model->index(this);
1089}
1090
1108void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
1109{
1110 itemFlags = aflags;
1111 if (QTableModel *model = tableModel())
1112 model->itemChanged(this);
1113}
1114
1115
1307 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1308 itemFlags(Qt::ItemIsEditable
1309 |Qt::ItemIsSelectable
1310 |Qt::ItemIsUserCheckable
1311 |Qt::ItemIsEnabled
1312 |Qt::ItemIsDragEnabled
1313 |Qt::ItemIsDropEnabled)
1314{
1315}
1316
1323 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1324 itemFlags(Qt::ItemIsEditable
1325 |Qt::ItemIsSelectable
1326 |Qt::ItemIsUserCheckable
1327 |Qt::ItemIsEnabled
1328 |Qt::ItemIsDragEnabled
1329 |Qt::ItemIsDropEnabled)
1330{
1332}
1333
1340 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1341 itemFlags(Qt::ItemIsEditable
1342 |Qt::ItemIsSelectable
1343 |Qt::ItemIsUserCheckable
1344 |Qt::ItemIsEnabled
1345 |Qt::ItemIsDragEnabled
1346 |Qt::ItemIsDropEnabled)
1347{
1350}
1351
1356{
1357 if (QTableModel *model = tableModel())
1358 model->removeItem(this);
1359 delete d;
1360}
1361
1366{
1367 return new QTableWidgetItem(*this);
1368}
1369
1379{
1380 bool found = false;
1381 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1382 for (int i = 0; i < values.size(); ++i) {
1383 if (values.at(i).role == role) {
1384 if (values[i].value == value)
1385 return;
1386
1387 values[i].value = value;
1388 found = true;
1389 break;
1390 }
1391 }
1392 if (!found)
1393 values.append(QWidgetItemData(role, value));
1394 if (QTableModel *model = tableModel())
1395 {
1396 const QList<int> roles((role == Qt::DisplayRole)
1397 ? QList<int>({ Qt::DisplayRole, Qt::EditRole })
1398 : QList<int>({ role }));
1399 model->itemChanged(this, roles);
1400 }
1401}
1402
1407{
1408 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1409 for (const auto &value : values) {
1410 if (value.role == role)
1411 return value.value;
1412 }
1413 return QVariant();
1414}
1415
1425
1426#ifndef QT_NO_DATASTREAM
1427
1434{
1435 in >> values;
1436}
1437
1444{
1445 out << values;
1446}
1447
1452QTableModel *QTableWidgetItem::tableModel() const
1453{
1454 return (view ? qobject_cast<QTableModel*>(view->model()) : nullptr);
1455}
1456
1457
1468{
1469 item.read(in);
1470 return in;
1471}
1472
1483{
1484 item.write(out);
1485 return out;
1486}
1487
1488#endif // QT_NO_DATASTREAM
1489
1501 : rtti(Type), values(other.values), view(nullptr),
1503 itemFlags(other.itemFlags)
1504{
1505}
1506
1516{
1517 values = other.values;
1518 itemFlags = other.itemFlags;
1519 return *this;
1520}
1521
1621
1627
1629{
1630 Q_Q(QTableWidget);
1632 emit q->itemPressed(item);
1633 emit q->cellPressed(index.row(), index.column());
1634}
1635
1637{
1638 Q_Q(QTableWidget);
1640 emit q->itemClicked(item);
1641 emit q->cellClicked(index.row(), index.column());
1642}
1643
1645{
1646 Q_Q(QTableWidget);
1648 emit q->itemDoubleClicked(item);
1649 emit q->cellDoubleClicked(index.row(), index.column());
1650}
1651
1653{
1654 Q_Q(QTableWidget);
1656 emit q->itemActivated(item);
1657 emit q->cellActivated(index.row(), index.column());
1658}
1659
1661{
1662 Q_Q(QTableWidget);
1664 emit q->itemEntered(item);
1665 emit q->cellEntered(index.row(), index.column());
1666}
1667
1669{
1670 Q_Q(QTableWidget);
1672 emit q->itemChanged(item);
1673 emit q->cellChanged(index.row(), index.column());
1674}
1675
1677 const QModelIndex &previous)
1678{
1679 Q_Q(QTableWidget);
1680 QTableWidgetItem *currentItem = tableModel()->item(current);
1681 QTableWidgetItem *previousItem = tableModel()->item(previous);
1682 if (currentItem || previousItem)
1683 emit q->currentItemChanged(currentItem, previousItem);
1684 emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());
1685}
1686
1695
1697 const QModelIndex &bottomRight)
1698{
1699 if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) {
1701 if (column >= topLeft.column() && column <= bottomRight.column()) {
1703 tableModel()->ensureSorted(column, order, topLeft.row(), bottomRight.row());
1704 }
1705 }
1706}
1707
1872 : QTableView(*new QTableWidgetPrivate, parent)
1873{
1874 Q_D(QTableWidget);
1875 QTableView::setModel(new QTableModel(0, 0, this));
1876 d->setup();
1877}
1878
1882QTableWidget::QTableWidget(int rows, int columns, QWidget *parent)
1883 : QTableView(*new QTableWidgetPrivate, parent)
1884{
1885 Q_D(QTableWidget);
1886 QTableView::setModel(new QTableModel(rows, columns, this));
1887 d->setup();
1888}
1889
1894{
1895 Q_D(QTableWidget);
1896 d->clearConnections();
1897}
1898
1907{
1908 Q_D(QTableWidget);
1909 d->tableModel()->setRowCount(rows);
1910}
1911
1917{
1918 Q_D(const QTableWidget);
1919 return d->model->rowCount();
1920}
1921
1930{
1931 Q_D(QTableWidget);
1932 d->tableModel()->setColumnCount(columns);
1933}
1934
1940{
1941 Q_D(const QTableWidget);
1942 return d->model->columnCount();
1943}
1944
1949{
1950 Q_D(const QTableWidget);
1951 return d->tableModel()->index(item).row();
1952}
1953
1958{
1959 Q_D(const QTableWidget);
1960 return d->tableModel()->index(item).column();
1961}
1962
1963
1971{
1972 Q_D(const QTableWidget);
1973 return d->tableModel()->item(row, column);
1974}
1975
1995{
1996 Q_D(QTableWidget);
1997 if (item) {
1998 if (Q_UNLIKELY(item->view)) {
1999 qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget");
2000 } else {
2001 item->view = this;
2002 d->tableModel()->setItem(row, column, item);
2003 }
2004 } else {
2005 delete takeItem(row, column);
2006 }
2007}
2008
2013{
2014 Q_D(QTableWidget);
2015 QTableWidgetItem *item = d->tableModel()->takeItem(row, column);
2016 if (item)
2017 item->view = nullptr;
2018 return item;
2019}
2020
2025{
2026 Q_D(const QTableWidget);
2027 return d->tableModel()->verticalHeaderItem(row);
2028}
2029
2034{
2035 Q_D(QTableWidget);
2036 if (item) {
2037 item->view = this;
2038 d->tableModel()->setVerticalHeaderItem(row, item);
2039 } else {
2041 }
2042}
2043
2049{
2050 Q_D(QTableWidget);
2051 QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row);
2052 if (itm)
2053 itm->view = nullptr;
2054 return itm;
2055}
2056
2062{
2063 Q_D(const QTableWidget);
2064 return d->tableModel()->horizontalHeaderItem(column);
2065}
2066
2073{
2074 Q_D(QTableWidget);
2075 if (item) {
2076 item->view = this;
2077 d->tableModel()->setHorizontalHeaderItem(column, item);
2078 } else {
2080 }
2081}
2082
2088{
2089 Q_D(QTableWidget);
2090 QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column);
2091 if (itm)
2092 itm->view = nullptr;
2093 return itm;
2094}
2095
2100{
2101 Q_D(QTableWidget);
2102 QTableModel *model = d->tableModel();
2103 QTableWidgetItem *item = nullptr;
2104 for (int i = 0; i < model->rowCount() && i < labels.size(); ++i) {
2105 item = model->verticalHeaderItem(i);
2106 if (!item) {
2107 item = model->createItem();
2109 }
2110 item->setText(labels.at(i));
2111 }
2112}
2113
2118{
2119 Q_D(QTableWidget);
2120 QTableModel *model = d->tableModel();
2121 QTableWidgetItem *item = nullptr;
2122 for (int i = 0; i < model->columnCount() && i < labels.size(); ++i) {
2123 item = model->horizontalHeaderItem(i);
2124 if (!item) {
2125 item = model->createItem();
2127 }
2128 item->setText(labels.at(i));
2129 }
2130}
2131
2138{
2139 return currentIndex().row();
2140}
2141
2148{
2149 return currentIndex().column();
2150}
2151
2158{
2159 Q_D(const QTableWidget);
2160 return d->tableModel()->item(currentIndex());
2161}
2162
2172{
2173 Q_D(QTableWidget);
2174 setCurrentIndex(d->tableModel()->index(item));
2175}
2176
2184void QTableWidget::setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command)
2185{
2186 Q_D(QTableWidget);
2187 d->selectionModel->setCurrentIndex(d->tableModel()->index(item), command);
2188}
2189
2205
2214void QTableWidget::setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command)
2215{
2216 Q_D(QTableWidget);
2217 d->selectionModel->setCurrentIndex(model()->index(row, column, QModelIndex()), command);
2218}
2219
2229
2237
2242{
2244}
2245
2251{
2252 Q_D(QTableWidget);
2253 if (!item)
2254 return;
2255 edit(d->tableModel()->index(item));
2256}
2257
2264{
2265 Q_D(QTableWidget);
2266 if (!item)
2267 return;
2268 QModelIndex index = d->tableModel()->index(item);
2270}
2271
2278{
2279 Q_D(QTableWidget);
2280 if (!item)
2281 return;
2282 QModelIndex index = d->tableModel()->index(item);
2284}
2285
2294{
2295 Q_D(const QTableWidget);
2296 const QModelIndex index = d->tableModel()->index(item);
2298}
2299
2314
2334
2339{
2340 if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
2341 !model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
2342 return;
2343
2344 QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
2345 QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
2346
2347 selectionModel()->select(QItemSelection(topLeft, bottomRight),
2349}
2350
2357QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
2358{
2359 const QList<QItemSelectionRange> ranges = selectionModel()->selection();
2360 QList<QTableWidgetSelectionRange> result;
2361 const int rangesCount = ranges.size();
2362 result.reserve(rangesCount);
2363 for (int i = 0; i < rangesCount; ++i)
2364 result.append({ranges.at(i).top(),
2365 ranges.at(i).left(),
2366 ranges.at(i).bottom(),
2367 ranges.at(i).right()});
2368 return result;
2369}
2370
2381QList<QTableWidgetItem*> QTableWidget::selectedItems() const
2382{
2383 Q_D(const QTableWidget);
2384 const QModelIndexList indexes = selectionModel()->selectedIndexes();
2385 QList<QTableWidgetItem*> items;
2386 for (const auto &index : indexes) {
2387 if (isIndexHidden(index))
2388 continue;
2389 QTableWidgetItem *item = d->tableModel()->item(index);
2390 if (item)
2391 items.append(item);
2392 }
2393 return items;
2394}
2395
2400QList<QTableWidgetItem*> QTableWidget::findItems(const QString &text, Qt::MatchFlags flags) const
2401{
2402 Q_D(const QTableWidget);
2403 QModelIndexList indexes;
2404 for (int column = 0; column < columnCount(); ++column)
2405 indexes += d->model->match(model()->index(0, column, QModelIndex()),
2406 Qt::DisplayRole, text, -1, flags);
2407 QList<QTableWidgetItem*> items;
2408 const int indexCount = indexes.size();
2409 items.reserve(indexCount);
2410 for (int i = 0; i < indexCount; ++i)
2411 items.append(d->tableModel()->item(indexes.at(i)));
2412 return items;
2413}
2414
2419int QTableWidget::visualRow(int logicalRow) const
2420{
2421 return verticalHeader()->visualIndex(logicalRow);
2422}
2423
2428int QTableWidget::visualColumn(int logicalColumn) const
2429{
2430 return horizontalHeader()->visualIndex(logicalColumn);
2431}
2432
2443{
2444 Q_D(const QTableWidget);
2445 return d->tableModel()->item(indexAt(p));
2446}
2447
2452{
2453 Q_D(const QTableWidget);
2454 if (!item)
2455 return QRect();
2456 QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2457 Q_ASSERT(index.isValid());
2458 return visualRect(index);
2459}
2460
2468{
2469 Q_D(QTableWidget);
2470 if (!item)
2471 return;
2472 QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2473 Q_ASSERT(index.isValid());
2475}
2476
2483{
2484 Q_D(const QTableWidget);
2485 return d->tableModel()->itemPrototype();
2486}
2487
2502{
2503 Q_D(QTableWidget);
2504 d->tableModel()->setItemPrototype(item);
2505}
2506
2511{
2512 Q_D(QTableWidget);
2513 d->tableModel()->insertRows(row);
2514}
2515
2520{
2521 Q_D(QTableWidget);
2522 d->tableModel()->insertColumns(column);
2523}
2524
2529{
2530 Q_D(QTableWidget);
2531 d->tableModel()->removeRows(row);
2532}
2533
2538{
2539 Q_D(QTableWidget);
2540 d->tableModel()->removeColumns(column);
2541}
2542
2552{
2553 Q_D(QTableWidget);
2554 selectionModel()->clear();
2555 d->tableModel()->clear();
2556}
2557
2566{
2567 Q_D(QTableWidget);
2568 selectionModel()->clear();
2569 d->tableModel()->clearContents();
2570}
2571
2579{
2580 return d_func()->tableModel()->QAbstractTableModel::mimeTypes();
2581}
2582
2591QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem *> &items) const
2592{
2593 Q_D(const QTableWidget);
2594
2595 QModelIndexList &cachedIndexes = d->tableModel()->cachedIndexes;
2596
2597 // if non empty, it's called from the model's own mimeData
2598 if (cachedIndexes.isEmpty()) {
2599 cachedIndexes.reserve(items.size());
2600 for (QTableWidgetItem *item : items)
2601 cachedIndexes << indexFromItem(item);
2602
2603 QMimeData *result = d->tableModel()->internalMimeData();
2604
2605 cachedIndexes.clear();
2606 return result;
2607 }
2608
2609 return d->tableModel()->internalMimeData();
2610}
2611
2621{
2622 QModelIndex idx;
2623#if QT_CONFIG(draganddrop)
2624 if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
2625 // QAbstractTableModel::dropMimeData will overwrite on the index if row == -1 and column == -1
2626 idx = model()->index(row, column);
2627 row = -1;
2628 column = -1;
2629 }
2630#endif
2631 return d_func()->tableModel()->QAbstractTableModel::dropMimeData(data, action , row, column, idx);
2632}
2633
2640{
2641 return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
2642}
2643
2650QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const
2651{
2652 const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data);
2653 if (twd)
2654 return twd->items;
2655 return QList<QTableWidgetItem*>();
2656}
2657
2665{
2666 Q_D(const QTableWidget);
2667 return d->tableModel()->index(item);
2668}
2669
2675{
2676 Q_D(const QTableWidget);
2677 return d->tableModel()->item(index);
2678}
2679
2684{
2685 Q_ASSERT(!"QTableWidget::setModel() - Changing the model of the QTableWidget is not allowed.");
2686}
2687
2690{
2691 return QTableView::event(e);
2692}
2693
2694#if QT_CONFIG(draganddrop)
2696void QTableWidget::dropEvent(QDropEvent *event) {
2697 Q_D(QTableWidget);
2698 if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
2699 dragDropMode() == QAbstractItemView::InternalMove)) {
2700 QModelIndex topIndex;
2701 int col = -1;
2702 int row = -1;
2703 // check whether a subclass has already accepted the event, ie. moved the data
2704 if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) {
2705 const QModelIndexList indexes = selectedIndexes();
2706 int top = INT_MAX;
2707 int left = INT_MAX;
2708 for (const auto &index : indexes) {
2709 top = qMin(index.row(), top);
2710 left = qMin(index.column(), left);
2711 }
2712
2713 QList<QTableWidgetItem *> taken;
2714 const int indexesCount = indexes.size();
2715 taken.reserve(indexesCount);
2716 for (const auto &index : indexes)
2717 taken.append(takeItem(index.row(), index.column()));
2718
2719 for (const auto &index : indexes) {
2720 int r = index.row() - top + topIndex.row();
2721 int c = index.column() - left + topIndex.column();
2722 setItem(r, c, taken.takeFirst());
2723 }
2724
2725 event->accept();
2726 }
2727 // either we or a subclass accepted the move event, so assume that the data was
2728 // moved and that QAbstractItemView shouldn't remove the source when QDrag::exec returns
2729 if (event->isAccepted())
2730 d->dropEventMoved = true;
2731 }
2732
2733 QTableView::dropEvent(event);
2734}
2735#endif
2736
2738
2739#include "moc_qtablewidget.cpp"
2740#include "moc_qtablewidget_p.cpp"
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
void endResetModel()
Completes a model reset operation.
void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after columns have been removed from the model.
void endRemoveRows()
Ends a row removal operation.
QModelIndexList persistentIndexList() const
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
This signal is emitted whenever the data in an existing item changes.
virtual Q_INVOKABLE int rowCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of rows under the given parent.
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
void layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options=CheckIndexOption::NoOption) const
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
void endInsertRows()
Ends a row insertion operation.
void beginResetModel()
Begins a model reset operation.
void endRemoveColumns()
Ends a column removal operation.
virtual Q_INVOKABLE int columnCount(const QModelIndex &parent=QModelIndex()) const =0
Returns the number of columns for the children of the given parent.
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
void endInsertColumns()
Ends a column insertion operation.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
virtual Q_INVOKABLE 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 beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
QWidget * indexWidget(const QModelIndex &index) const
void activated(const QModelIndex &index)
This signal is emitted when the item specified by index is activated by the user.
QAbstractItemModel * model() const
Returns the model that this view is presenting.
void setCurrentIndex(const QModelIndex &index)
Sets the current item to be the item at index.
void doubleClicked(const QModelIndex &index)
This signal is emitted when a mouse button is double-clicked.
bool event(QEvent *event) override
\reimp
void entered(const QModelIndex &index)
This signal is emitted when the mouse cursor enters the item specified by index.
QModelIndex currentIndex() const
Returns the model index of the current item.
QModelIndex rootIndex() const
Returns the model index of the model's root item.
bool isPersistentEditorOpen(const QModelIndex &index) const
void setIndexWidget(const QModelIndex &index, QWidget *widget)
void openPersistentEditor(const QModelIndex &index)
Opens a persistent editor on the item at the given index.
void pressed(const QModelIndex &index)
This signal is emitted when a mouse button is pressed.
ScrollHint
\value EnsureVisible Scroll to ensure that the item is visible.
void clicked(const QModelIndex &index)
This signal is emitted when a mouse button is left-clicked.
QItemSelectionModel * selectionModel() const
Returns the current selection model.
void closePersistentEditor(const QModelIndex &index)
Closes the persistent editor for the item at the given index.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
Definition qcoreevent.h:45
void setSortIndicator(int logicalIndex, Qt::SortOrder order)
Sets the sort indicator for the section specified by the given logicalIndex in the direction specifie...
Qt::SortOrder sortIndicatorOrder() const
Returns the order for the sort indicator.
int visualIndex(int logicalIndex) const
Returns the visual index position of the section specified by the given logicalIndex,...
int sortIndicatorSection() const
Returns the logical index of the section that has a sort indicator.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This signal is emitted whenever the selection changes.
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
This signal is emitted whenever the current item changes.
Q_INVOKABLE bool isSelected(const QModelIndex &index) const
Returns true if the given model item index is selected.
QModelIndexList selectedIndexes
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Selects the model item index using the specified command, and emits selectionChanged().
virtual void clear()
Clears the selection model.
\inmodule QtCore
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:488
iterator end()
Definition qlist.h:626
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
T value(qsizetype i) const
Definition qlist.h:664
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:794
iterator begin()
Definition qlist.h:625
void reserve(qsizetype size)
Definition qlist.h:753
void resize(qsizetype size)
Definition qlist.h:403
const_iterator cend() const noexcept
Definition qlist.h:631
void append(parameter_type t)
Definition qlist.h:458
const_iterator cbegin() const noexcept
Definition qlist.h:630
void clear()
Definition qlist.h:434
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
constexpr int row() const noexcept
Returns the row this model index refers to.
constexpr int column() const noexcept
Returns the column this model index refers to.
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
iterator begin()
Definition qset.h:136
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void removeItem(QTableWidgetItem *item)
static bool itemLessThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
void setRowCount(int rows)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
QTableWidgetItem * verticalHeaderItem(int section)
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
void setVerticalHeaderItem(int section, QTableWidgetItem *item)
bool clearItemData(const QModelIndex &index) override
void ensureSorted(int column, Qt::SortOrder order, int start, int end)
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override
Sets the data for the given role and section in the header with the specified orientation to the valu...
void setItemPrototype(const QTableWidgetItem *item)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
QMap< int, QVariant > itemData(const QModelIndex &index) const override
Returns a map with values for all predefined roles in the model for the item at the given index.
QTableWidgetItem * takeHorizontalHeaderItem(int section)
void setItem(int row, int column, QTableWidgetItem *item)
bool insertRows(int row, int count=1, const QModelIndex &parent=QModelIndex()) override
QStringList mimeTypes() const override
Returns the list of allowed MIME types.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Returns the data for the given role and section in the header with the specified orientation.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
\reimp
bool isValid(const QModelIndex &index) const
QTableWidgetItem * createItem() const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
static QList< QTableWidgetItem * >::iterator sortedInsertionIterator(const QList< QTableWidgetItem * >::iterator &begin, const QList< QTableWidgetItem * >::iterator &end, Qt::SortOrder order, QTableWidgetItem *item)
QTableWidgetItem * horizontalHeaderItem(int section)
void setHorizontalHeaderItem(int section, QTableWidgetItem *item)
void itemChanged(QTableWidgetItem *item, const QList< int > &roles=QList< int >())
QMimeData * internalMimeData() const
const QTableWidgetItem * itemPrototype() const
bool insertColumns(int column, int count=1, const QModelIndex &parent=QModelIndex()) override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Sets the role data for the item at index to value.
bool removeColumns(int column, int count=1, const QModelIndex &parent=QModelIndex()) override
QTableWidgetItem * takeItem(int row, int column)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
void setColumnCount(int columns)
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole...
void sort(int column, Qt::SortOrder order) override
QMimeData * mimeData(const QModelIndexList &indexes) const override
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
static bool itemGreaterThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
bool removeRows(int row, int count=1, const QModelIndex &parent=QModelIndex()) override
long tableIndex(int row, int column) const
QTableModel(int rows, int columns, QTableWidget *parent)
QList< QTableWidgetItem * > columnItems(int column) const
void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow)
void clearContents()
Qt::DropActions supportedDropActions() const override
QTableWidgetItem * takeVerticalHeaderItem(int section)
QTableWidgetItem * item(int row, int column) const
QHeaderView * horizontalHeader
The QTableView class provides a default model/view implementation of a table view.
Definition qtableview.h:18
void setModel(QAbstractItemModel *model) override
\reimp
void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible) override
\reimp
void setSortingEnabled(bool enable)
If enable is true, enables sorting for the table and immediately trigger a call to sortByColumn() wit...
QRect visualRect(const QModelIndex &index) const override
\reimp
QModelIndex indexAt(const QPoint &p) const override
Returns the index position of the model item corresponding to the table item at position pos in conte...
bool isSortingEnabled() const
QHeaderView * horizontalHeader() const
Returns the table view's horizontal header.
QModelIndexList selectedIndexes() const override
\reimp
QHeaderView * verticalHeader() const
Returns the table view's vertical header.
bool isIndexHidden(const QModelIndex &index) const override
\reimp
The QTableWidgetItem class provides an item for use with the QTableWidget class.
virtual void read(QDataStream &in)
Reads the item from stream in.
QDataStream & operator>>(QDataStream &in, QTableWidgetItem &item)
Reads a table widget item from stream in into item.
virtual void write(QDataStream &out) const
Writes the item to stream out.
void setSelected(bool select)
QString text() const
Returns the item's text.
bool isSelected() const
QIcon icon() const
Returns the item's icon.
QDataStream & operator<<(QDataStream &out, const QTableWidgetItem &item)
Writes the table widget item item to stream out.
QTableWidgetItem & operator=(const QTableWidgetItem &other)
Assigns other's data and flags to this item.
virtual QTableWidgetItem * clone() const
Creates a copy of the item.
virtual void setData(int role, const QVariant &value)
Sets the item's data for the given role to the specified value.
QTableWidgetItem(int type=Type)
Constructs a table item of the specified type that does not belong to any table.
virtual QVariant data(int role) const
Returns the item's data for the given role.
virtual bool operator<(const QTableWidgetItem &other) const
Returns true if the item is less than the other item; otherwise returns false.
void setFlags(Qt::ItemFlags flags)
Sets the flags for the item to the given flags.
virtual ~QTableWidgetItem()
Destroys the table item.
void emitItemPressed(const QModelIndex &index)
void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current)
void emitItemActivated(const QModelIndex &index)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void emitItemClicked(const QModelIndex &index)
std::array< QMetaObject::Connection, 10 > connections
QTableModel * tableModel() const
void emitItemDoubleClicked(const QModelIndex &index)
void emitItemChanged(const QModelIndex &index)
void emitItemEntered(const QModelIndex &index)
The QTableWidgetSelectionRange class provides a way to interact with selection in a model without usi...
The QTableWidget class provides an item-based table view with a default model.
bool event(QEvent *e) override
\reimp
int row(const QTableWidgetItem *item) const
Returns the row for the item.
void setHorizontalHeaderLabels(const QStringList &labels)
Sets the horizontal header labels using labels.
void setSortingEnabled(bool enable)
void itemSelectionChanged()
This signal is emitted whenever the selection changes.
QTableWidget(QWidget *parent=nullptr)
Creates a new table view with the given parent.
bool isPersistentEditorOpen(QTableWidgetItem *item) const
void setRowCount(int rows)
Sets the number of rows in this table's model to rows.
void setItemPrototype(const QTableWidgetItem *item)
Sets the item prototype for the table to the specified item.
QList< QTableWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags) const
Finds items that matches the text using the given flags.
QTableWidgetItem * horizontalHeaderItem(int column) const
Returns the horizontal header item for column, column, if one has been set; otherwise returns \nullpt...
void setCurrentCell(int row, int column)
void insertRow(int row)
Inserts an empty row into the table at row.
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 ...
~QTableWidget()
Destroys this QTableWidget.
void setRangeSelected(const QTableWidgetSelectionRange &range, bool select)
Selects or deselects the range depending on select.
void setColumnCount(int columns)
Sets the number of columns in this table's model to columns.
void sortItems(int column, Qt::SortOrder order=Qt::AscendingOrder)
Sorts all the rows in the table widget based on column and order.
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this view.
void clear()
Removes all items in the view.
int visualRow(int logicalRow) const
Returns the visual row of the given logicalRow.
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of tablewidget items.
QList< QTableWidgetSelectionRange > selectedRanges() const
Returns a list of all selected ranges.
QTableWidgetItem * item(int row, int column) const
Returns the item for the given row and column if one has been set; otherwise returns \nullptr.
QList< QTableWidgetItem * > items(const QMimeData *data) const
Returns a list of pointers to the items contained in the data object.
QTableWidgetItem * verticalHeaderItem(int row) const
Returns the vertical header item for row row.
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
QTableWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the given point, or returns \nullptr if point is not covered by an i...
void setCurrentItem(QTableWidgetItem *item)
Sets the current item to item.
QTableWidgetItem * currentItem() const
Returns the current item.
int rowCount
the number of rows in the table
QList< QTableWidgetItem * > selectedItems() const
Returns a list of all selected items.
void removeRow(int row)
Removes the row row and all its items from the table.
void setModel(QAbstractItemModel *model) override
QWidget * cellWidget(int row, int column) const
void setVerticalHeaderLabels(const QStringList &labels)
Sets the vertical header labels using labels.
void setVerticalHeaderItem(int row, QTableWidgetItem *item)
Sets the vertical header item for row row to item.
friend class QTableModel
QTableWidgetItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QTableWidgetItem associated with the given index.
bool isSortingEnabled() const
void insertColumn(int column)
Inserts an empty column into the table at column.
int column(const QTableWidgetItem *item) const
Returns the column for the item.
void closePersistentEditor(QTableWidgetItem *item)
Closes the persistent editor for item.
virtual QMimeData * mimeData(const QList< QTableWidgetItem * > &items) const
Returns an object that contains a serialized description of the specified items.
void removeColumn(int column)
Removes the column column and all its items from the table.
const QTableWidgetItem * itemPrototype() const
Returns the item prototype used by the table.
QTableWidgetItem * takeVerticalHeaderItem(int row)
void setHorizontalHeaderItem(int column, QTableWidgetItem *item)
Sets the horizontal header item for column column to item.
int columnCount
the number of columns in the table
void openPersistentEditor(QTableWidgetItem *item)
Opens an editor for the give item.
int currentColumn() const
Returns the column of the current item.
void setCellWidget(int row, int column, QWidget *widget)
void editItem(QTableWidgetItem *item)
Starts editing the item if it is editable.
QRect visualItemRect(const QTableWidgetItem *item) const
Returns the rectangle on the viewport occupied by the item at item.
int currentRow() const
Returns the row of the current item.
QTableWidgetItem * takeHorizontalHeaderItem(int column)
void scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint=EnsureVisible)
Scrolls the view if necessary to ensure that the item is visible.
QModelIndex indexFromItem(const QTableWidgetItem *item) const
Returns the QModelIndex associated with the given item.
QTableWidgetItem * takeItem(int row, int column)
Removes the item at row and column from the table without deleting it.
int visualColumn(int logicalColumn) const
Returns the visual column of the given logicalColumn.
\inmodule QtCore
Definition qvariant.h:65
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
#define this
Definition dialogs.cpp:9
QOpenGLWidget * widget
[1]
QString text
list append(new Employee("Blackpool", "Stephen"))
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Definition qcompare.h:63
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
@ DecorationRole
@ EditRole
@ DisplayRole
SortOrder
Definition qnamespace.h:121
@ AscendingOrder
Definition qnamespace.h:122
DropAction
@ IgnoreAction
@ MoveAction
@ ItemIsEditable
@ ItemIsDragEnabled
@ ItemIsUserCheckable
@ ItemIsSelectable
@ ItemIsEnabled
@ ItemIsDropEnabled
#define Q_UNLIKELY(x)
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection * connection
static QString header(const QString &name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:166
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLfloat GLfloat GLfloat v2
GLenum GLsizei GLsizei GLint * values
[15]
GLsizei const GLfloat * v
[13]
GLuint index
[2]
GLboolean r
[2]
GLuint GLuint end
GLenum GLuint id
[7]
GLdouble GLdouble GLdouble GLdouble top
GLenum GLenum GLsizei count
GLdouble GLdouble right
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLsizei range
GLint left
GLenum type
GLbitfield flags
GLint GLfloat GLfloat v1
GLboolean enable
GLuint start
GLfloat n
GLfloat GLfloat GLfloat GLfloat h
GLenum GLenum GLsizei void GLsizei void * column
struct _cl_event * event
const GLubyte * c
GLuint in
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLfixed GLfixed GLint GLint order
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define emit
static int compare(quint64 a, quint64 b)
ptrdiff_t qsizetype
Definition qtypes.h:165
QSqlQueryModel * model
[16]
QTextStream out(stdout)
[7]
QMimeData * mimeData
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
selection select(topLeft, bottomRight)
QList< QTreeWidgetItem * > items
QQuickView * view
[0]
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:962
Definition moc.h:23