Qt 6.x
The Qt SDK
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 <qitemdelegate.h>
7#include <qpainter.h>
8#include <private/qtablewidget_p.h>
9
10#include <algorithm>
11
13
16 prototype(nullptr),
17 tableItems(rows * columns, 0),
18 verticalHeaderItems(rows, 0),
19 horizontalHeaderItems(columns, 0)
20{}
21
23{
24 clear();
25 delete prototype;
26}
27
29{
30 if (count < 1 || row < 0 || row > verticalHeaderItems.size())
31 return false;
32
34 int rc = verticalHeaderItems.size();
35 int cc = horizontalHeaderItems.size();
36 verticalHeaderItems.insert(row, count, 0);
37 if (rc == 0)
38 tableItems.resize(cc * count);
39 else
40 tableItems.insert(tableIndex(row, 0), cc * count, 0);
42 return true;
43}
44
46{
47 if (count < 1 || column < 0 || column > horizontalHeaderItems.size())
48 return false;
49
51 int rc = verticalHeaderItems.size();
52 int cc = horizontalHeaderItems.size();
53 horizontalHeaderItems.insert(column, count, 0);
54 if (cc == 0)
55 tableItems.resize(rc * count);
56 else
57 for (int row = 0; row < rc; ++row)
58 tableItems.insert(tableIndex(row, column), count, 0);
60 return true;
61}
62
64{
65 if (count < 1 || row < 0 || row + count > verticalHeaderItems.size())
66 return false;
67
69 int i = tableIndex(row, 0);
70 int n = count * columnCount();
71 QTableWidgetItem *oldItem = nullptr;
72 for (int j = i; j < n + i; ++j) {
73 oldItem = tableItems.at(j);
74 if (oldItem)
75 oldItem->view = nullptr;
76 delete oldItem;
77 }
78 tableItems.remove(qMax(i, 0), n);
79 for (int v = row; v < row + count; ++v) {
80 oldItem = verticalHeaderItems.at(v);
81 if (oldItem)
82 oldItem->view = nullptr;
83 delete oldItem;
84 }
85 verticalHeaderItems.remove(row, count);
87 return true;
88}
89
91{
92 if (count < 1 || column < 0 || column + count > horizontalHeaderItems.size())
93 return false;
94
96 QTableWidgetItem *oldItem = nullptr;
97 for (int row = rowCount() - 1; row >= 0; --row) {
98 int i = tableIndex(row, column);
99 for (int j = i; j < i + count; ++j) {
100 oldItem = tableItems.at(j);
101 if (oldItem)
102 oldItem->view = nullptr;
103 delete oldItem;
104 }
105 tableItems.remove(i, count);
106 }
107 for (int h=column; h<column+count; ++h) {
108 oldItem = horizontalHeaderItems.at(h);
109 if (oldItem)
110 oldItem->view = nullptr;
111 delete oldItem;
112 }
113 horizontalHeaderItems.remove(column, count);
115 return true;
116}
117
119{
120 int i = tableIndex(row, column);
121 if (i < 0 || i >= tableItems.size())
122 return;
123 QTableWidgetItem *oldItem = tableItems.at(i);
124 if (item == oldItem)
125 return;
126
127 // remove old
128 if (oldItem)
129 oldItem->view = nullptr;
130 delete tableItems.at(i);
131
132 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
133
134 // set new
135 if (item)
136 item->d->id = i;
137 tableItems[i] = item;
138
139 if (view && view->isSortingEnabled()
140 && view->horizontalHeader()->sortIndicatorSection() == column) {
141 // sorted insertion
142 Qt::SortOrder order = view->horizontalHeader()->sortIndicatorOrder();
144 if (row < colItems.size())
145 colItems.remove(row);
146 int sortedRow;
147 if (item == nullptr) {
148 // move to after all non-0 (sortable) items
149 sortedRow = colItems.size();
150 } else {
152 it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
153 sortedRow = qMax((int)(it - colItems.begin()), 0);
154 }
155 if (sortedRow != row) {
157 // move the items @ row to sortedRow
158 int cc = columnCount();
159 QList<QTableWidgetItem *> rowItems(cc);
160 for (int j = 0; j < cc; ++j)
161 rowItems[j] = tableItems.at(tableIndex(row, j));
162 tableItems.remove(tableIndex(row, 0), cc);
163 tableItems.insert(tableIndex(sortedRow, 0), cc, 0);
164 for (int j = 0; j < cc; ++j)
165 tableItems[tableIndex(sortedRow, j)] = rowItems.at(j);
166 QTableWidgetItem *header = verticalHeaderItems.at(row);
167 verticalHeaderItems.remove(row);
168 verticalHeaderItems.insert(sortedRow, header);
169 // update persistent indexes
170 QModelIndexList oldPersistentIndexes = persistentIndexList();
171 QModelIndexList newPersistentIndexes = oldPersistentIndexes;
172 updateRowIndexes(newPersistentIndexes, row, sortedRow);
173 changePersistentIndexList(oldPersistentIndexes,
174 newPersistentIndexes);
175
177 return;
178 }
179 }
181 emit dataChanged(idx, idx);
182}
183
185{
186 long i = tableIndex(row, column);
187 QTableWidgetItem *itm = tableItems.value(i);
188 if (itm) {
189 itm->view = nullptr;
190 itm->d->id = -1;
191 tableItems[i] = 0;
192 const QModelIndex ind = index(row, column);
193 emit dataChanged(ind, ind);
194 }
195 return itm;
196}
197
199{
200 return item(index(row, column));
201}
202
204{
205 if (!isValid(index))
206 return nullptr;
207 return tableItems.at(tableIndex(index.row(), index.column()));
208}
209
211{
212 int i = tableItems.indexOf(item);
213 if (i != -1) {
214 QModelIndex idx = index(item);
215 tableItems[i] = nullptr;
216 emit dataChanged(idx, idx);
217 return;
218 }
219
220 i = verticalHeaderItems.indexOf(item);
221
222 if (i != -1) {
223 verticalHeaderItems[i] = 0;
225 return;
226 }
227 i = horizontalHeaderItems.indexOf(item);
228 if (i != -1) {
229 horizontalHeaderItems[i] = 0;
231 return;
232 }
233}
234
236{
237 if (section < 0 || section >= horizontalHeaderItems.size())
238 return;
239 QTableWidgetItem *oldItem = horizontalHeaderItems.at(section);
240 if (item == oldItem)
241 return;
242
243 if (oldItem)
244 oldItem->view = nullptr;
245 delete oldItem;
246
247 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
248
249 if (item) {
250 item->view = view;
251 item->d->headerItem = true;
252 }
253 horizontalHeaderItems[section] = item;
254 emit headerDataChanged(Qt::Horizontal, section, section);
255}
256
258{
259 if (section < 0 || section >= verticalHeaderItems.size())
260 return;
261 QTableWidgetItem *oldItem = verticalHeaderItems.at(section);
262 if (item == oldItem)
263 return;
264
265 if (oldItem)
266 oldItem->view = nullptr;
267 delete oldItem;
268
269 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
270
271 if (item) {
272 item->view = view;
273 item->d->headerItem = true;
274 }
275 verticalHeaderItems[section] = item;
276 emit headerDataChanged(Qt::Vertical, section, section);
277}
278
280{
281 if (section < 0 || section >= horizontalHeaderItems.size())
282 return nullptr;
283 QTableWidgetItem *itm = horizontalHeaderItems.at(section);
284 if (itm) {
285 itm->view = nullptr;
286 itm->d->headerItem = false;
287 horizontalHeaderItems[section] = 0;
288 }
289 return itm;
290}
291
293{
294 if (section < 0 || section >= verticalHeaderItems.size())
295 return nullptr;
296 QTableWidgetItem *itm = verticalHeaderItems.at(section);
297 if (itm) {
298 itm->view = nullptr;
299 itm->d->headerItem = false;
300 verticalHeaderItems[section] = 0;
301 }
302 return itm;
303}
304
306{
307 return horizontalHeaderItems.value(section);
308}
309
311{
312 return verticalHeaderItems.value(section);
313}
314
316{
317 if (!item)
318 return QModelIndex();
319 int i = -1;
320 const int id = item->d->id;
321 if (id >= 0 && id < tableItems.size() && tableItems.at(id) == item) {
322 i = id;
323 } else { // we need to search for the item
324 i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item));
325 if (i == -1) // not found
326 return QModelIndex();
327 }
328 int row = i / columnCount();
329 int col = i % columnCount();
330 return QAbstractTableModel::index(row, col);
331}
332
334{
335 int rc = verticalHeaderItems.size();
336 if (rows < 0 || rc == rows)
337 return;
338 if (rc < rows)
339 insertRows(qMax(rc, 0), rows - rc);
340 else
341 removeRows(qMax(rows, 0), rc - rows);
342}
343
345{
346 int cc = horizontalHeaderItems.size();
347 if (columns < 0 || cc == columns)
348 return;
349 if (cc < columns)
350 insertColumns(qMax(cc, 0), columns - cc);
351 else
352 removeColumns(qMax(columns, 0), cc - columns);
353}
354
356{
357 return parent.isValid() ? 0 : verticalHeaderItems.size();
358}
359
361{
362 return parent.isValid() ? 0 : horizontalHeaderItems.size();
363}
364
366{
368 if (itm)
369 return itm->data(role);
370 return QVariant();
371}
372
374{
375 if (!index.isValid())
376 return false;
377
379 if (itm) {
380 itm->setData(role, value);
381 return true;
382 }
383
384 // don't create dummy table items for empty values
385 if (!value.isValid())
386 return false;
387
388 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
389 if (!view)
390 return false;
391
392 itm = createItem();
393 itm->setData(role, value);
394 view->setItem(index.row(), index.column(), itm);
395 return true;
396}
397
399{
402 if (itm) {
403 for (int i = 0; i < itm->values.size(); ++i) {
404 roles.insert(itm->values.at(i).role,
405 itm->values.at(i).value);
406 }
407 }
408 return roles;
409}
410
411// reimplemented to ensure that only one dataChanged() signal is emitted
413{
414 if (!index.isValid())
415 return false;
416
417 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
419 if (itm) {
420 itm->view = nullptr; // prohibits item from calling itemChanged()
421 QList<int> rolesVec;
422 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
423 const int role = (it.key() == Qt::EditRole ? Qt::DisplayRole : it.key());
424 if (itm->data(role) != it.value()) {
425 itm->setData(role, it.value());
426 rolesVec += role;
427 if (role == Qt::DisplayRole)
428 rolesVec += Qt::EditRole;
429 }
430 }
431 itm->view = view;
432 if (!rolesVec.isEmpty())
433 itemChanged(itm, rolesVec);
434 return true;
435 }
436
437 if (!view)
438 return false;
439
440 itm = createItem();
441 for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
442 itm->setData(it.key(), it.value());
443 view->setItem(index.row(), index.column(), itm);
444 return true;
445}
446
448{
450 return false;
452 if (!itm)
453 return false;
454 const auto beginIter = itm->values.cbegin();
455 const auto endIter = itm->values.cend();
456 if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
457 return true; //it's already cleared
458 itm->values.clear();
460 return true;
461}
462
463Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
464{
465 if (!index.isValid())
467 if (QTableWidgetItem *itm = item(index))
468 return itm->flags();
469 return (Qt::ItemIsEditable
475}
476
478{
480 QList<int> unsortable;
481
482 sortable.reserve(rowCount());
483 unsortable.reserve(rowCount());
484
485 for (int row = 0; row < rowCount(); ++row) {
486 if (QTableWidgetItem *itm = item(row, column))
488 else
489 unsortable.append(row);
490 }
491
493 std::stable_sort(sortable.begin(), sortable.end(), compare);
494
495 QList<QTableWidgetItem *> sorted_table(tableItems.size());
496 QModelIndexList from;
498 const int numRows = rowCount();
499 const int numColumns = columnCount();
500 from.reserve(numRows * numColumns);
501 to.reserve(numRows * numColumns);
502 for (int i = 0; i < numRows; ++i) {
503 int r = (i < sortable.size()
504 ? sortable.at(i).second
505 : unsortable.at(i - sortable.size()));
506 for (int c = 0; c < numColumns; ++c) {
507 sorted_table[tableIndex(i, c)] = item(r, c);
508 from.append(createIndex(r, c));
509 to.append(createIndex(i, c));
510 }
511 }
512
514
515 tableItems = sorted_table;
516 changePersistentIndexList(from, to); // ### slow
517
519}
520
521/*
522 \internal
523
524 Ensures that rows in the interval [start, end] are
525 sorted according to the contents of column \a column
526 and the given sort \a order.
527*/
529 int start, int end)
530{
531 int count = end - start + 1;
533 sorting.reserve(count);
534 for (int row = start; row <= end; ++row) {
536 if (itm == nullptr) {
537 // no more sortable items (all 0-items are
538 // at the end of the table when it is sorted)
539 break;
540 }
542 }
543
545 std::stable_sort(sorting.begin(), sorting.end(), compare);
546 QModelIndexList oldPersistentIndexes, newPersistentIndexes;
547 QList<QTableWidgetItem *> newTable = tableItems;
548 QList<QTableWidgetItem *> newVertical = verticalHeaderItems;
551 qsizetype distanceFromBegin = 0;
552 bool changed = false;
553 for (int i = 0; i < sorting.size(); ++i) {
554 distanceFromBegin = std::distance(colItems.begin(), vit);
555 int oldRow = sorting.at(i).second;
556 QTableWidgetItem *item = colItems.at(oldRow);
557 colItems.remove(oldRow);
558 vit = sortedInsertionIterator(colItems.begin() + distanceFromBegin, colItems.end(), order,
559 item);
560 int newRow = qMax((int)(vit - colItems.begin()), 0);
561 if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item))
562 newRow = oldRow;
563 vit = colItems.insert(vit, item);
564 if (newRow != oldRow) {
565 if (!changed) {
567 oldPersistentIndexes = persistentIndexList();
568 newPersistentIndexes = oldPersistentIndexes;
569 changed = true;
570 }
571 // move the items @ oldRow to newRow
572 int cc = columnCount();
573 QList<QTableWidgetItem *> rowItems(cc);
574 for (int j = 0; j < cc; ++j)
575 rowItems[j] = newTable.at(tableIndex(oldRow, j));
576 newTable.remove(tableIndex(oldRow, 0), cc);
577 newTable.insert(tableIndex(newRow, 0), cc, 0);
578 for (int j = 0; j < cc; ++j)
579 newTable[tableIndex(newRow, j)] = rowItems.at(j);
580 QTableWidgetItem *header = newVertical.at(oldRow);
581 newVertical.remove(oldRow);
582 newVertical.insert(newRow, header);
583 // update persistent indexes
584 updateRowIndexes(newPersistentIndexes, oldRow, newRow);
585 // the index of the remaining rows may have changed
586 for (int j = i + 1; j < sorting.size(); ++j) {
587 int otherRow = sorting.at(j).second;
588 if (oldRow < otherRow && newRow >= otherRow)
589 --sorting[j].second;
590 else if (oldRow > otherRow && newRow <= otherRow)
591 ++sorting[j].second;
592 }
593 }
594 }
595
596 if (changed) {
597 tableItems = newTable;
598 verticalHeaderItems = newVertical;
599 changePersistentIndexList(oldPersistentIndexes,
600 newPersistentIndexes);
602 }
603}
604
605/*
606 \internal
607
608 Returns the non-0 items in column \a column.
609*/
611{
613 int rc = rowCount();
614 items.reserve(rc);
615 for (int row = 0; row < rc; ++row) {
617 if (itm == nullptr) {
618 // no more sortable items (all 0-items are
619 // at the end of the table when it is sorted)
620 break;
621 }
622 items.append(itm);
623 }
624 return items;
625}
626
627/*
628 \internal
629
630 Adjusts the row of each index in \a indexes if necessary, given
631 that a row of items has been moved from row \a movedFrom to row
632 \a movedTo.
633*/
635 int movedFromRow, int movedToRow)
636{
637 QModelIndexList::iterator it;
638 for (it = indexes.begin(); it != indexes.end(); ++it) {
639 int oldRow = (*it).row();
640 int newRow = oldRow;
641 if (oldRow == movedFromRow)
642 newRow = movedToRow;
643 else if (movedFromRow < oldRow && movedToRow >= oldRow)
644 newRow = oldRow - 1;
645 else if (movedFromRow > oldRow && movedToRow <= oldRow)
646 newRow = oldRow + 1;
647 if (newRow != oldRow)
648 *it = index(newRow, (*it).column(), (*it).parent());
649 }
650}
651
652/*
653 \internal
654
655 Returns an iterator to the item where \a item should be
656 inserted in the interval (\a begin, \a end) according to
657 the given sort \a order.
658*/
663{
665 return std::lower_bound(begin, end, item, QTableModelLessThan());
666 return std::lower_bound(begin, end, item, QTableModelGreaterThan());
667}
668
671{
672 return *(left.first) < *(right.first);
673}
674
677{
678 return (*(right.first) < *(left .first));
679}
680
681QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
682{
683 if (section < 0)
684 return QVariant();
685
686 QTableWidgetItem *itm = nullptr;
687 if (orientation == Qt::Horizontal && section < horizontalHeaderItems.size())
688 itm = horizontalHeaderItems.at(section);
689 else if (orientation == Qt::Vertical && section < verticalHeaderItems.size())
690 itm = verticalHeaderItems.at(section);
691 else
692 return QVariant(); // section is out of bounds
693
694 if (itm)
695 return itm->data(role);
696 if (role == Qt::DisplayRole)
697 return section + 1;
698 return QVariant();
699}
700
701bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
702 const QVariant &value, int role)
703{
704 if (section < 0 ||
705 (orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) ||
706 (orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
707 return false;
708
709 QTableWidgetItem *itm = nullptr;
710 if (orientation == Qt::Horizontal)
711 itm = horizontalHeaderItems.at(section);
712 else
713 itm = verticalHeaderItems.at(section);
714 if (itm) {
715 itm->setData(role, value);
716 return true;
717 }
718 return false;
719}
720
722{
723 return (index.isValid()
724 && index.row() < verticalHeaderItems.size()
725 && index.column() < horizontalHeaderItems.size());
726}
727
729{
730 for (int j = 0; j < verticalHeaderItems.size(); ++j) {
731 if (verticalHeaderItems.at(j)) {
732 verticalHeaderItems.at(j)->view = nullptr;
733 delete verticalHeaderItems.at(j);
734 verticalHeaderItems[j] = 0;
735 }
736 }
737 for (int k = 0; k < horizontalHeaderItems.size(); ++k) {
738 if (horizontalHeaderItems.at(k)) {
739 horizontalHeaderItems.at(k)->view = nullptr;
740 delete horizontalHeaderItems.at(k);
741 horizontalHeaderItems[k] = 0;
742 }
743 }
745}
746
748{
750 for (int i = 0; i < tableItems.size(); ++i) {
751 if (tableItems.at(i)) {
752 tableItems.at(i)->view = nullptr;
753 delete tableItems.at(i);
754 tableItems[i] = 0;
755 }
756 }
758}
759
761{
762 if (!item)
763 return;
764 if (item->d->headerItem) {
765 int row = verticalHeaderItems.indexOf(item);
766 if (row >= 0) {
768 } else {
769 int column = horizontalHeaderItems.indexOf(item);
770 if (column >= 0)
772 }
773 } else {
774 QModelIndex idx = index(item);
775 if (idx.isValid())
776 emit dataChanged(idx, idx, roles);
777 }
778}
779
781{
782 return prototype ? prototype->clone() : new QTableWidgetItem;
783}
784
786{
787 return prototype;
788}
789
791{
792 if (prototype != item) {
793 delete prototype;
794 prototype = item;
795 }
796}
797
799{
800 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
801 return (view ? view->mimeTypes() : QStringList());
802}
803
805{
806 return QAbstractTableModel::mimeData(cachedIndexes);
807}
808
810{
812 const int indexesCount = indexes.size();
813 items.reserve(indexesCount);
814 for (int i = 0; i < indexesCount; ++i)
815 items << item(indexes.at(i));
816 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
817
818 // cachedIndexes is a little hack to avoid copying from QModelIndexList to
819 // QList<QTreeWidgetItem*> and back again in the view
820 cachedIndexes = indexes;
821 QMimeData *mimeData = (view ? view->mimeData(items) : nullptr);
822 cachedIndexes.clear();
823 return mimeData;
824}
825
827 int row , int column, const QModelIndex &index)
828{
829 if (index.isValid()) {
830 row = index.row();
831 column = index.column();
832 }else if (row == -1 || column == -1) { // The user dropped outside the table.
833 row = rowCount();
834 column = 0;
835 }
836
837 QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
838 return (view ? view->dropMimeData(row, column, data, action) : false);
839}
840
842{
843 const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
844 return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
845}
846
1063{
1064 if (!view || !view->selectionModel())
1065 return false;
1066 const QTableModel *model = qobject_cast<const QTableModel*>(view->model());
1067 if (!model)
1068 return false;
1069 const QModelIndex index = model->index(this);
1070 return view->selectionModel()->isSelected(index);
1071}
1072
1082{
1083 if (!view || !view->selectionModel())
1084 return;
1085 const QTableModel *model = qobject_cast<const QTableModel*>(view->model());
1086 if (!model)
1087 return;
1088 const QModelIndex index = model->index(this);
1090}
1091
1109void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
1110{
1111 itemFlags = aflags;
1112 if (QTableModel *model = tableModel())
1113 model->itemChanged(this);
1114}
1115
1116
1308 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1309 itemFlags(Qt::ItemIsEditable
1310 |Qt::ItemIsSelectable
1311 |Qt::ItemIsUserCheckable
1312 |Qt::ItemIsEnabled
1313 |Qt::ItemIsDragEnabled
1314 |Qt::ItemIsDropEnabled)
1315{
1316}
1317
1324 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1325 itemFlags(Qt::ItemIsEditable
1326 |Qt::ItemIsSelectable
1327 |Qt::ItemIsUserCheckable
1328 |Qt::ItemIsEnabled
1329 |Qt::ItemIsDragEnabled
1330 |Qt::ItemIsDropEnabled)
1331{
1333}
1334
1341 : rtti(type), view(nullptr), d(new QTableWidgetItemPrivate(this)),
1342 itemFlags(Qt::ItemIsEditable
1343 |Qt::ItemIsSelectable
1344 |Qt::ItemIsUserCheckable
1345 |Qt::ItemIsEnabled
1346 |Qt::ItemIsDragEnabled
1347 |Qt::ItemIsDropEnabled)
1348{
1351}
1352
1357{
1358 if (QTableModel *model = tableModel())
1359 model->removeItem(this);
1360 delete d;
1361}
1362
1367{
1368 return new QTableWidgetItem(*this);
1369}
1370
1380{
1381 bool found = false;
1382 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1383 for (int i = 0; i < values.size(); ++i) {
1384 if (values.at(i).role == role) {
1385 if (values[i].value == value)
1386 return;
1387
1388 values[i].value = value;
1389 found = true;
1390 break;
1391 }
1392 }
1393 if (!found)
1394 values.append(QWidgetItemData(role, value));
1395 if (QTableModel *model = tableModel())
1396 {
1397 const QList<int> roles((role == Qt::DisplayRole)
1399 : QList<int>({ role }));
1400 model->itemChanged(this, roles);
1401 }
1402}
1403
1408{
1409 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
1410 for (const auto &value : values) {
1411 if (value.role == role)
1412 return value.value;
1413 }
1414 return QVariant();
1415}
1416
1422{
1425}
1426
1427#ifndef QT_NO_DATASTREAM
1428
1435{
1436 in >> values;
1437}
1438
1445{
1446 out << values;
1447}
1448
1453QTableModel *QTableWidgetItem::tableModel() const
1454{
1455 return (view ? qobject_cast<QTableModel*>(view->model()) : nullptr);
1456}
1457
1458
1469{
1470 item.read(in);
1471 return in;
1472}
1473
1484{
1485 item.write(out);
1486 return out;
1487}
1488
1489#endif // QT_NO_DATASTREAM
1490
1502 : rtti(Type), values(other.values), view(nullptr),
1504 itemFlags(other.itemFlags)
1505{
1506}
1507
1517{
1518 values = other.values;
1519 itemFlags = other.itemFlags;
1520 return *this;
1521}
1522
1593{
1594 Q_Q(QTableWidget);
1595 // view signals
1598 QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
1602 // model signals
1605 // selection signals
1606 QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
1608 QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
1609 q, SIGNAL(itemSelectionChanged()));
1610 // sorting
1613 QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
1614}
1615
1617{
1618 Q_Q(QTableWidget);
1620 emit q->itemPressed(item);
1621 emit q->cellPressed(index.row(), index.column());
1622}
1623
1625{
1626 Q_Q(QTableWidget);
1628 emit q->itemClicked(item);
1629 emit q->cellClicked(index.row(), index.column());
1630}
1631
1633{
1634 Q_Q(QTableWidget);
1636 emit q->itemDoubleClicked(item);
1637 emit q->cellDoubleClicked(index.row(), index.column());
1638}
1639
1641{
1642 Q_Q(QTableWidget);
1644 emit q->itemActivated(item);
1645 emit q->cellActivated(index.row(), index.column());
1646}
1647
1649{
1650 Q_Q(QTableWidget);
1652 emit q->itemEntered(item);
1653 emit q->cellEntered(index.row(), index.column());
1654}
1655
1657{
1658 Q_Q(QTableWidget);
1660 emit q->itemChanged(item);
1661 emit q->cellChanged(index.row(), index.column());
1662}
1663
1665 const QModelIndex &previous)
1666{
1667 Q_Q(QTableWidget);
1668 QTableWidgetItem *currentItem = tableModel()->item(current);
1669 QTableWidgetItem *previousItem = tableModel()->item(previous);
1670 if (currentItem || previousItem)
1671 emit q->currentItemChanged(currentItem, previousItem);
1672 emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());
1673}
1674
1676{
1677 if (sortingEnabled) {
1680 model->sort(column, order);
1681 }
1682}
1683
1685 const QModelIndex &bottomRight)
1686{
1687 if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) {
1689 if (column >= topLeft.column() && column <= bottomRight.column()) {
1691 tableModel()->ensureSorted(column, order, topLeft.row(), bottomRight.row());
1692 }
1693 }
1694}
1695
1861{
1862 Q_D(QTableWidget);
1863 QTableView::setModel(new QTableModel(0, 0, this));
1864 d->setup();
1865}
1866
1872{
1873 Q_D(QTableWidget);
1874 QTableView::setModel(new QTableModel(rows, columns, this));
1875 d->setup();
1876}
1877
1882{
1883}
1884
1893{
1894 Q_D(QTableWidget);
1895 d->tableModel()->setRowCount(rows);
1896}
1897
1903{
1904 Q_D(const QTableWidget);
1905 return d->model->rowCount();
1906}
1907
1916{
1917 Q_D(QTableWidget);
1918 d->tableModel()->setColumnCount(columns);
1919}
1920
1926{
1927 Q_D(const QTableWidget);
1928 return d->model->columnCount();
1929}
1930
1935{
1936 Q_D(const QTableWidget);
1937 return d->tableModel()->index(item).row();
1938}
1939
1944{
1945 Q_D(const QTableWidget);
1946 return d->tableModel()->index(item).column();
1947}
1948
1949
1957{
1958 Q_D(const QTableWidget);
1959 return d->tableModel()->item(row, column);
1960}
1961
1981{
1982 Q_D(QTableWidget);
1983 if (item) {
1984 if (Q_UNLIKELY(item->view)) {
1985 qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget");
1986 } else {
1987 item->view = this;
1988 d->tableModel()->setItem(row, column, item);
1989 }
1990 } else {
1991 delete takeItem(row, column);
1992 }
1993}
1994
1999{
2000 Q_D(QTableWidget);
2001 QTableWidgetItem *item = d->tableModel()->takeItem(row, column);
2002 if (item)
2003 item->view = nullptr;
2004 return item;
2005}
2006
2011{
2012 Q_D(const QTableWidget);
2013 return d->tableModel()->verticalHeaderItem(row);
2014}
2015
2020{
2021 Q_D(QTableWidget);
2022 if (item) {
2023 item->view = this;
2024 d->tableModel()->setVerticalHeaderItem(row, item);
2025 } else {
2027 }
2028}
2029
2035{
2036 Q_D(QTableWidget);
2037 QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row);
2038 if (itm)
2039 itm->view = nullptr;
2040 return itm;
2041}
2042
2048{
2049 Q_D(const QTableWidget);
2050 return d->tableModel()->horizontalHeaderItem(column);
2051}
2052
2059{
2060 Q_D(QTableWidget);
2061 if (item) {
2062 item->view = this;
2063 d->tableModel()->setHorizontalHeaderItem(column, item);
2064 } else {
2066 }
2067}
2068
2074{
2075 Q_D(QTableWidget);
2076 QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column);
2077 if (itm)
2078 itm->view = nullptr;
2079 return itm;
2080}
2081
2086{
2087 Q_D(QTableWidget);
2088 QTableModel *model = d->tableModel();
2089 QTableWidgetItem *item = nullptr;
2090 for (int i = 0; i < model->rowCount() && i < labels.size(); ++i) {
2091 item = model->verticalHeaderItem(i);
2092 if (!item) {
2093 item = model->createItem();
2095 }
2096 item->setText(labels.at(i));
2097 }
2098}
2099
2104{
2105 Q_D(QTableWidget);
2106 QTableModel *model = d->tableModel();
2107 QTableWidgetItem *item = nullptr;
2108 for (int i = 0; i < model->columnCount() && i < labels.size(); ++i) {
2109 item = model->horizontalHeaderItem(i);
2110 if (!item) {
2111 item = model->createItem();
2113 }
2114 item->setText(labels.at(i));
2115 }
2116}
2117
2124{
2125 return currentIndex().row();
2126}
2127
2134{
2135 return currentIndex().column();
2136}
2137
2144{
2145 Q_D(const QTableWidget);
2146 return d->tableModel()->item(currentIndex());
2147}
2148
2158{
2159 Q_D(QTableWidget);
2160 setCurrentIndex(d->tableModel()->index(item));
2161}
2162
2170void QTableWidget::setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command)
2171{
2172 Q_D(QTableWidget);
2173 d->selectionModel->setCurrentIndex(d->tableModel()->index(item), command);
2174}
2175
2188{
2190}
2191
2200void QTableWidget::setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command)
2201{
2202 Q_D(QTableWidget);
2203 d->selectionModel->setCurrentIndex(model()->index(row, column, QModelIndex()), command);
2204}
2205
2210{
2211 Q_D(QTableWidget);
2212 d->model->sort(column, order);
2214}
2215
2220{
2222}
2223
2228{
2230}
2231
2237{
2238 Q_D(QTableWidget);
2239 if (!item)
2240 return;
2241 edit(d->tableModel()->index(item));
2242}
2243
2250{
2251 Q_D(QTableWidget);
2252 if (!item)
2253 return;
2254 QModelIndex index = 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
2280{
2281 Q_D(const QTableWidget);
2282 const QModelIndex index = d->tableModel()->index(item);
2284}
2285
2296{
2299}
2300
2316{
2319}
2320
2325{
2326 if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
2327 !model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
2328 return;
2329
2330 QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
2331 QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
2332
2333 selectionModel()->select(QItemSelection(topLeft, bottomRight),
2335}
2336
2344{
2347 const int rangesCount = ranges.size();
2348 result.reserve(rangesCount);
2349 for (int i = 0; i < rangesCount; ++i)
2350 result.append({ranges.at(i).top(),
2351 ranges.at(i).left(),
2352 ranges.at(i).bottom(),
2353 ranges.at(i).right()});
2354 return result;
2355}
2356
2368{
2369 Q_D(const QTableWidget);
2370 const QModelIndexList indexes = selectionModel()->selectedIndexes();
2372 for (const auto &index : indexes) {
2373 if (isIndexHidden(index))
2374 continue;
2375 QTableWidgetItem *item = d->tableModel()->item(index);
2376 if (item)
2377 items.append(item);
2378 }
2379 return items;
2380}
2381
2387{
2388 Q_D(const QTableWidget);
2389 QModelIndexList indexes;
2390 for (int column = 0; column < columnCount(); ++column)
2391 indexes += d->model->match(model()->index(0, column, QModelIndex()),
2392 Qt::DisplayRole, text, -1, flags);
2394 const int indexCount = indexes.size();
2395 items.reserve(indexCount);
2396 for (int i = 0; i < indexCount; ++i)
2397 items.append(d->tableModel()->item(indexes.at(i)));
2398 return items;
2399}
2400
2405int QTableWidget::visualRow(int logicalRow) const
2406{
2407 return verticalHeader()->visualIndex(logicalRow);
2408}
2409
2414int QTableWidget::visualColumn(int logicalColumn) const
2415{
2416 return horizontalHeader()->visualIndex(logicalColumn);
2417}
2418
2429{
2430 Q_D(const QTableWidget);
2431 return d->tableModel()->item(indexAt(p));
2432}
2433
2438{
2439 Q_D(const QTableWidget);
2440 if (!item)
2441 return QRect();
2442 QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2443 Q_ASSERT(index.isValid());
2444 return visualRect(index);
2445}
2446
2454{
2455 Q_D(QTableWidget);
2456 if (!item)
2457 return;
2458 QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
2459 Q_ASSERT(index.isValid());
2461}
2462
2469{
2470 Q_D(const QTableWidget);
2471 return d->tableModel()->itemPrototype();
2472}
2473
2488{
2489 Q_D(QTableWidget);
2490 d->tableModel()->setItemPrototype(item);
2491}
2492
2497{
2498 Q_D(QTableWidget);
2499 d->tableModel()->insertRows(row);
2500}
2501
2506{
2507 Q_D(QTableWidget);
2508 d->tableModel()->insertColumns(column);
2509}
2510
2515{
2516 Q_D(QTableWidget);
2517 d->tableModel()->removeRows(row);
2518}
2519
2524{
2525 Q_D(QTableWidget);
2526 d->tableModel()->removeColumns(column);
2527}
2528
2538{
2539 Q_D(QTableWidget);
2540 selectionModel()->clear();
2541 d->tableModel()->clear();
2542}
2543
2552{
2553 Q_D(QTableWidget);
2554 selectionModel()->clear();
2555 d->tableModel()->clearContents();
2556}
2557
2565{
2566 return d_func()->tableModel()->QAbstractTableModel::mimeTypes();
2567}
2568
2578{
2579 Q_D(const QTableWidget);
2580
2581 QModelIndexList &cachedIndexes = d->tableModel()->cachedIndexes;
2582
2583 // if non empty, it's called from the model's own mimeData
2584 if (cachedIndexes.isEmpty()) {
2585 cachedIndexes.reserve(items.size());
2586 for (QTableWidgetItem *item : items)
2587 cachedIndexes << indexFromItem(item);
2588
2589 QMimeData *result = d->tableModel()->internalMimeData();
2590
2591 cachedIndexes.clear();
2592 return result;
2593 }
2594
2595 return d->tableModel()->internalMimeData();
2596}
2597
2607{
2608 QModelIndex idx;
2609#if QT_CONFIG(draganddrop)
2610 if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
2611 // QAbstractTableModel::dropMimeData will overwrite on the index if row == -1 and column == -1
2612 idx = model()->index(row, column);
2613 row = -1;
2614 column = -1;
2615 }
2616#endif
2617 return d_func()->tableModel()->QAbstractTableModel::dropMimeData(data, action , row, column, idx);
2618}
2619
2626{
2627 return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
2628}
2629
2637{
2638 const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data);
2639 if (twd)
2640 return twd->items;
2641 return QList<QTableWidgetItem*>();
2642}
2643
2651{
2652 Q_D(const QTableWidget);
2653 return d->tableModel()->index(item);
2654}
2655
2661{
2662 Q_D(const QTableWidget);
2663 return d->tableModel()->item(index);
2664}
2665
2670{
2671 Q_ASSERT(!"QTableWidget::setModel() - Changing the model of the QTableWidget is not allowed.");
2672}
2673
2676{
2677 return QTableView::event(e);
2678}
2679
2680#if QT_CONFIG(draganddrop)
2682void QTableWidget::dropEvent(QDropEvent *event) {
2683 Q_D(QTableWidget);
2684 if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
2685 dragDropMode() == QAbstractItemView::InternalMove)) {
2686 QModelIndex topIndex;
2687 int col = -1;
2688 int row = -1;
2689 // check whether a subclass has already accepted the event, ie. moved the data
2690 if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) {
2691 const QModelIndexList indexes = selectedIndexes();
2692 int top = INT_MAX;
2693 int left = INT_MAX;
2694 for (const auto &index : indexes) {
2695 top = qMin(index.row(), top);
2696 left = qMin(index.column(), left);
2697 }
2698
2700 const int indexesCount = indexes.size();
2701 taken.reserve(indexesCount);
2702 for (const auto &index : indexes)
2703 taken.append(takeItem(index.row(), index.column()));
2704
2705 for (const auto &index : indexes) {
2706 int r = index.row() - top + topIndex.row();
2707 int c = index.column() - left + topIndex.column();
2708 setItem(r, c, taken.takeFirst());
2709 }
2710
2711 event->accept();
2712 }
2713 // either we or a subclass accepted the move event, so assume that the data was
2714 // moved and that QAbstractItemView shouldn't remove the source when QDrag::exec returns
2715 if (event->isAccepted())
2716 d->dropEventMoved = true;
2717 }
2718
2719 QTableView::dropEvent(event);
2720}
2721#endif
2722
2724
2725#include "moc_qtablewidget.cpp"
2726#include "moc_qtablewidget_p.cpp"
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
void endResetModel()
Completes a model reset operation.
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
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.
bool event(QEvent *event) override
\reimp
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.
ScrollHint
\value EnsureVisible Scroll to ensure that the item is visible.
void edit(const QModelIndex &index)
Starts editing the item corresponding to the given index if it is editable.
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:311
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\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
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
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:471
iterator end()
Definition qlist.h:609
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
value_type takeFirst()
Definition qlist.h:549
T value(qsizetype i) const
Definition qlist.h:661
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:787
iterator begin()
Definition qlist.h:608
void reserve(qsizetype size)
Definition qlist.h:746
void resize(qsizetype size)
Definition qlist.h:392
const_iterator cend() const noexcept
Definition qlist.h:614
void append(parameter_type t)
Definition qlist.h:441
const_iterator cbegin() const noexcept
Definition qlist.h:613
void clear()
Definition qlist.h:417
Definition qmap.h:186
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
const_iterator constBegin() const
Definition qmap.h:599
const_iterator constEnd() const
Definition qmap.h:603
\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}.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
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.
QList< QTableWidgetItem * > items
void _q_emitItemClicked(const QModelIndex &index)
void _q_emitItemActivated(const QModelIndex &index)
void _q_emitItemChanged(const QModelIndex &index)
void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current)
void _q_emitItemPressed(const QModelIndex &index)
void _q_emitItemEntered(const QModelIndex &index)
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QTableModel * tableModel() const
void _q_emitItemDoubleClicked(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)
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.
QTestTable * parent() const
Definition qtestdata.cpp:82
\inmodule QtCore
Definition qvariant.h:64
T value() const &
Definition qvariant.h:511
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"))
double e
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:97
@ Horizontal
Definition qnamespace.h:98
@ Vertical
Definition qnamespace.h:99
@ DecorationRole
@ EditRole
@ DisplayRole
SortOrder
Definition qnamespace.h:120
@ AscendingOrder
Definition qnamespace.h:121
DropAction
@ IgnoreAction
@ MoveAction
@ ItemIsEditable
@ ItemIsDragEnabled
@ ItemIsUserCheckable
@ ItemIsSelectable
@ ItemIsEnabled
@ ItemIsDropEnabled
#define Q_UNLIKELY(x)
std::pair< T1, T2 > QPair
static QString header(const QString &name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:162
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
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
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
GLsizei range
GLint left
GLenum type
GLbitfield flags
GLint GLfloat GLfloat v1
GLboolean enable
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
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:70
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:955
Definition moc.h:24
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent