Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qlistwidget.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 "qlistwidget.h"
5
6#include <qitemdelegate.h>
7#include <private/qlistview_p.h>
8#include <private/qwidgetitemdata_p.h>
9#include <private/qlistwidget_p.h>
10
11#include <algorithm>
12
14
16{
18public:
20};
21
23#include "qlistwidget.moc"
25
28{
29}
30
32{
33 clear();
34}
35
37{
39 for (int i = 0; i < items.size(); ++i) {
40 if (items.at(i)) {
41 items.at(i)->d->theid = -1;
42 items.at(i)->view = nullptr;
43 delete items.at(i);
44 }
45 }
46 items.clear();
48}
49
51{
52 return items.value(row);
53}
54
56{
57 if (!item)
58 return;
59 int row = items.indexOf(item); // ### use index(item) - it's faster
60 Q_ASSERT(row != -1);
62 items.at(row)->d->theid = -1;
63 items.at(row)->view = nullptr;
64 items.removeAt(row);
66}
67
69{
70 if (!item)
71 return;
72
73 item->view = qobject_cast<QListWidget*>(QObject::parent());
74 if (item->view && item->view->isSortingEnabled()) {
75 // sorted insertion
77 it = sortedInsertionIterator(items.begin(), items.end(),
78 item->view->sortOrder(), item);
79 row = qMax<qsizetype>(it - items.begin(), 0);
80 } else {
81 if (row < 0)
82 row = 0;
83 else if (row > items.size())
84 row = items.size();
85 }
87 items.insert(row, item);
88 item->d->theid = row;
90}
91
92void QListModel::insert(int row, const QStringList &labels)
93{
94 const int count = labels.size();
95 if (count <= 0)
96 return;
97 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
98 if (view && view->isSortingEnabled()) {
99 // sorted insertion
100 for (int i = 0; i < count; ++i) {
101 QListWidgetItem *item = new QListWidgetItem(labels.at(i));
102 insert(row, item);
103 }
104 } else {
105 if (row < 0)
106 row = 0;
107 else if (row > items.size())
108 row = items.size();
110 for (int i = 0; i < count; ++i) {
111 QListWidgetItem *item = new QListWidgetItem(labels.at(i));
112 item->d->theid = row;
113 item->view = qobject_cast<QListWidget*>(QObject::parent());
114 items.insert(row++, item);
115 }
117 }
118}
119
121{
122 if (row < 0 || row >= items.size())
123 return nullptr;
124
126 items.at(row)->d->theid = -1;
127 items.at(row)->view = nullptr;
128 QListWidgetItem *item = items.takeAt(row);
130 return item;
131}
132
133void QListModel::move(int srcRow, int dstRow)
134{
135 if (srcRow == dstRow
136 || srcRow < 0 || srcRow >= items.size()
137 || dstRow < 0 || dstRow > items.size())
138 return;
139
140 if (!beginMoveRows(QModelIndex(), srcRow, srcRow, QModelIndex(), dstRow))
141 return;
142 if (srcRow < dstRow)
143 --dstRow;
144 items.move(srcRow, dstRow);
145 endMoveRows();
146}
147
149{
150 return parent.isValid() ? 0 : items.size();
151}
152
154{
155 QListWidgetItem *item = const_cast<QListWidgetItem *>(item_);
156 if (!item || !item->view || static_cast<const QListModel *>(item->view->model()) != this
157 || items.isEmpty())
158 return QModelIndex();
159 int row;
160 const int theid = item->d->theid;
161 if (theid >= 0 && theid < items.size() && items.at(theid) == item) {
162 row = theid;
163 } else { // we need to search for the item
164 row = items.lastIndexOf(item); // lastIndexOf is an optimization in favor of indexOf
165 if (row == -1) // not found
166 return QModelIndex();
167 item->d->theid = row;
168 }
169 return createIndex(row, 0, item);
170}
171
173{
174 if (hasIndex(row, column, parent))
175 return createIndex(row, column, items.at(row));
176 return QModelIndex();
177}
178
180{
181 if (!index.isValid() || index.row() >= items.size())
182 return QVariant();
183 return items.at(index.row())->data(role);
184}
185
186bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role)
187{
188 if (!index.isValid() || index.row() >= items.size())
189 return false;
190 items.at(index.row())->setData(role, value);
191 return true;
192}
193
195{
197 return false;
198 QListWidgetItem *item = items.at(index.row());
199 const auto beginIter = item->d->values.cbegin();
200 const auto endIter = item->d->values.cend();
201 if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
202 return true; //it's already cleared
203 item->d->values.clear();
205 return true;
206}
207
209{
211 if (!index.isValid() || index.row() >= items.size())
212 return roles;
213 QListWidgetItem *itm = items.at(index.row());
214 for (int i = 0; i < itm->d->values.size(); ++i) {
215 roles.insert(itm->d->values.at(i).role,
216 itm->d->values.at(i).value);
217 }
218 return roles;
219}
220
222{
223 if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
224 return false;
225
227 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
228 QListWidgetItem *itm = nullptr;
229
230 for (int r = row; r < row + count; ++r) {
231 itm = new QListWidgetItem;
232 itm->view = view;
233 itm->d->theid = r;
234 items.insert(r, itm);
235 }
236
238 return true;
239}
240
242{
243 if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
244 return false;
245
247 QListWidgetItem *itm = nullptr;
248 for (int r = row; r < row + count; ++r) {
249 itm = items.takeAt(row);
250 itm->view = nullptr;
251 itm->d->theid = -1;
252 delete itm;
253 }
255 return true;
256}
257
262bool QListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
263{
264 if (sourceRow < 0
265 || sourceRow + count - 1 >= rowCount(sourceParent)
266 || destinationChild < 0
270 || count <= 0
271 || sourceParent.isValid()
273 return false;
274 }
276 return false;
277
278 int fromRow = sourceRow;
280 fromRow += count - 1;
281 else
283 while (count--)
284 items.move(fromRow, destinationChild);
285 endMoveRows();
286 return true;
287}
288
289Qt::ItemFlags QListModel::flags(const QModelIndex &index) const
290{
291 if (!index.isValid() || index.row() >= items.size() || index.model() != this)
292 return Qt::ItemIsDropEnabled; // we allow drops outside the items
293 return items.at(index.row())->flags();
294}
295
297{
298 if (column != 0)
299 return;
300
302
304 for (int i = 0; i < items.size(); ++i) {
305 QListWidgetItem *item = items.at(i);
306 sorting[i].first = item;
307 sorting[i].second = i;
308 }
309
311 std::sort(sorting.begin(), sorting.end(), compare);
312 QModelIndexList fromIndexes;
313 QModelIndexList toIndexes;
314 const int sortingCount = sorting.size();
315 fromIndexes.reserve(sortingCount);
316 toIndexes.reserve(sortingCount);
317 for (int r = 0; r < sortingCount; ++r) {
318 QListWidgetItem *item = sorting.at(r).first;
319 toIndexes.append(createIndex(r, 0, item));
320 fromIndexes.append(createIndex(sorting.at(r).second, 0, sorting.at(r).first));
321 items[r] = sorting.at(r).first;
322 }
323 changePersistentIndexList(fromIndexes, toIndexes);
324
326}
327
336{
337 if (column != 0)
338 return;
339
340 const int count = end - start + 1;
342 for (int i = 0; i < count; ++i) {
343 sorting[i].first = items.at(start + i);
344 sorting[i].second = start + i;
345 }
346
348 std::sort(sorting.begin(), sorting.end(), compare);
349
350 QModelIndexList oldPersistentIndexes = persistentIndexList();
351 QModelIndexList newPersistentIndexes = oldPersistentIndexes;
352 QList<QListWidgetItem*> tmp = items;
354 bool changed = false;
355 for (int i = 0; i < count; ++i) {
356 int oldRow = sorting.at(i).second;
357 int tmpitepos = lit - tmp.begin();
358 QListWidgetItem *item = tmp.takeAt(oldRow);
359 if (tmpitepos > tmp.size())
360 --tmpitepos;
361 lit = tmp.begin() + tmpitepos;
362 lit = sortedInsertionIterator(lit, tmp.end(), order, item);
363 int newRow = qMax<qsizetype>(lit - tmp.begin(), 0);
364 lit = tmp.insert(lit, item);
365 if (newRow != oldRow) {
366 if (!changed) {
368 oldPersistentIndexes = persistentIndexList();
369 newPersistentIndexes = oldPersistentIndexes;
370 changed = true;
371 }
372 for (int j = i + 1; j < count; ++j) {
373 int otherRow = sorting.at(j).second;
374 if (oldRow < otherRow && newRow >= otherRow)
375 --sorting[j].second;
376 else if (oldRow > otherRow && newRow <= otherRow)
377 ++sorting[j].second;
378 }
379 for (int k = 0; k < newPersistentIndexes.size(); ++k) {
380 QModelIndex pi = newPersistentIndexes.at(k);
381 int oldPersistentRow = pi.row();
382 int newPersistentRow = oldPersistentRow;
383 if (oldPersistentRow == oldRow)
384 newPersistentRow = newRow;
385 else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
386 newPersistentRow = oldPersistentRow - 1;
387 else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
388 newPersistentRow = oldPersistentRow + 1;
389 if (newPersistentRow != oldPersistentRow)
390 newPersistentIndexes[k] = createIndex(newPersistentRow,
391 pi.column(), pi.internalPointer());
392 }
393 }
394 }
395
396 if (changed) {
397 items = tmp;
398 changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
400 }
401}
402
405{
406 return (*left.first) < (*right.first);
407}
408
411{
412 return (*right.first) < (*left.first);
413}
414
419{
421 return std::lower_bound(begin, end, item, QListModelLessThan());
422 return std::lower_bound(begin, end, item, QListModelGreaterThan());
423}
424
426{
427 const QModelIndex idx = index(item);
428 emit dataChanged(idx, idx, roles);
429}
430
432{
433 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
434 if (view)
435 return view->mimeTypes();
436 return {};
437}
438
440{
441 return QAbstractItemModel::mimeData(cachedIndexes);
442}
443
445{
447 const int indexesCount = indexes.size();
448 itemlist.reserve(indexesCount);
449 for (int i = 0; i < indexesCount; ++i)
450 itemlist << at(indexes.at(i).row());
451 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
452
453 cachedIndexes = indexes;
454 QMimeData *mimeData = view->mimeData(itemlist);
455 cachedIndexes.clear();
456 return mimeData;
457}
458
459#if QT_CONFIG(draganddrop)
461 int row, int column, const QModelIndex &index)
462{
464 QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
465 if (index.isValid())
466 row = index.row();
467 else if (row == -1)
468 row = items.size();
469
470 return view->dropMimeData(row, data, action);
471}
472
473Qt::DropActions QListModel::supportedDropActions() const
474{
475 const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
476 return view->supportedDropActions();
477}
478#endif // QT_CONFIG(draganddrop)
479
595 : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
596 itemFlags(Qt::ItemIsSelectable
597 |Qt::ItemIsUserCheckable
598 |Qt::ItemIsEnabled
599 |Qt::ItemIsDragEnabled)
600{
601 if (QListModel *model = listModel())
602 model->insert(model->rowCount(), this);
603}
604
622 : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
623 itemFlags(Qt::ItemIsSelectable
624 |Qt::ItemIsUserCheckable
625 |Qt::ItemIsEnabled
626 |Qt::ItemIsDragEnabled)
627{
628 QListModel *model = listModel();
629 {
633 }
634 if (model)
635 model->insert(model->rowCount(), this);
636}
637
656 QListWidget *listview, int type)
657 : rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
658 itemFlags(Qt::ItemIsSelectable
659 |Qt::ItemIsUserCheckable
660 |Qt::ItemIsEnabled
661 |Qt::ItemIsDragEnabled)
662{
663 QListModel *model = listModel();
664 {
669 }
670 if (model)
671 model->insert(model->rowCount(), this);
672}
673
678{
679 if (QListModel *model = listModel())
680 model->remove(this);
681 delete d;
682}
683
688{
689 return new QListWidgetItem(*this);
690}
691
702{
703 bool found = false;
704 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
705 for (int i = 0; i < d->values.size(); ++i) {
706 if (d->values.at(i).role == role) {
707 if (d->values.at(i).value == value)
708 return;
709 d->values[i].value = value;
710 found = true;
711 break;
712 }
713 }
714 if (!found)
716 if (QListModel *model = listModel()) {
717 const QList<int> roles((role == Qt::DisplayRole)
719 : QList<int>({ role }));
720 model->itemChanged(this, roles);
721 }
722}
723
731{
732 role = (role == Qt::EditRole ? Qt::DisplayRole : role);
733 for (int i = 0; i < d->values.size(); ++i)
734 if (d->values.at(i).role == role)
735 return d->values.at(i).value;
736 return QVariant();
737}
738
744{
747}
748
749#ifndef QT_NO_DATASTREAM
750
757{
758 in >> d->values;
759}
760
767{
768 out << d->values;
769}
770#endif // QT_NO_DATASTREAM
771
783 : rtti(Type), view(nullptr),
785 itemFlags(other.itemFlags)
786{
787 d->values = other.d->values;
788}
789
799{
800 d->values = other.d->values;
801 itemFlags = other.itemFlags;
802 return *this;
803}
804
809QListModel *QListWidgetItem::listModel() const
810{
811 return (view ? qobject_cast<QListModel*>(view->model()) : nullptr);
812}
813
814#ifndef QT_NO_DATASTREAM
815
826{
827 item.write(out);
828 return out;
829}
830
841{
842 item.read(in);
843 return in;
844}
845
846#endif // QT_NO_DATASTREAM
847
972{
973 const QListModel *model = listModel();
974 if (!model || !view->selectionModel())
975 return;
976 const QAbstractItemView::SelectionMode selectionMode = view->selectionMode();
977 if (selectionMode == QAbstractItemView::NoSelection)
978 return;
979 const QModelIndex index = model->index(this);
980 if (selectionMode == QAbstractItemView::SingleSelection)
981 view->selectionModel()->select(index, select
984 else
985 view->selectionModel()->select(index, select
988}
989
999{
1000 const QListModel *model = listModel();
1001 if (!model || !view->selectionModel())
1002 return false;
1003 const QModelIndex index = model->index(this);
1004 return view->selectionModel()->isSelected(index);
1005}
1006
1014void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
1015{
1016 itemFlags = aflags;
1017 if (QListModel *model = listModel())
1018 model->itemChanged(this);
1019}
1020
1021
1125{
1126 Q_Q(QListWidget);
1127 q->QListView::setModel(new QListModel(q));
1128 // view signals
1131 QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
1133 QObject::connect(q, SIGNAL(activated(QModelIndex)),
1140 QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
1141}
1142
1144{
1145 Q_Q(QListWidget);
1146 emit q->itemPressed(listModel()->at(index.row()));
1147}
1148
1150{
1151 Q_Q(QListWidget);
1152 emit q->itemClicked(listModel()->at(index.row()));
1153}
1154
1156{
1157 Q_Q(QListWidget);
1158 emit q->itemDoubleClicked(listModel()->at(index.row()));
1159}
1160
1162{
1163 Q_Q(QListWidget);
1164 emit q->itemActivated(listModel()->at(index.row()));
1165}
1166
1168{
1169 Q_Q(QListWidget);
1170 emit q->itemEntered(listModel()->at(index.row()));
1171}
1172
1174{
1175 Q_Q(QListWidget);
1176 emit q->itemChanged(listModel()->at(index.row()));
1177}
1178
1180 const QModelIndex &previous)
1181{
1182 Q_Q(QListWidget);
1183 QPersistentModelIndex persistentCurrent = current;
1184 QListWidgetItem *currentItem = listModel()->at(persistentCurrent.row());
1185 emit q->currentItemChanged(currentItem, listModel()->at(previous.row()));
1186
1187 //persistentCurrent is invalid if something changed the model in response
1188 //to the currentItemChanged signal emission and the item was removed
1189 if (!persistentCurrent.isValid()) {
1190 currentItem = nullptr;
1191 }
1192
1193 emit q->currentTextChanged(currentItem ? currentItem->text() : QString());
1194 emit q->currentRowChanged(persistentCurrent.row());
1195}
1196
1198{
1199 if (sortingEnabled)
1200 model->sort(0, sortOrder);
1201}
1202
1204 const QModelIndex &bottomRight)
1205{
1206 if (sortingEnabled && topLeft.isValid() && bottomRight.isValid())
1207 listModel()->ensureSorted(topLeft.column(), sortOrder,
1208 topLeft.row(), bottomRight.row());
1209}
1210
1399{
1400 Q_D(QListWidget);
1401 d->setup();
1402}
1403
1409{
1410}
1411
1417{
1418 Q_D(QListWidget);
1419
1420 if (d->selectionModel) {
1422 this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
1424 this, SIGNAL(itemSelectionChanged()));
1425 }
1426
1428
1429 if (d->selectionModel) {
1431 this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
1433 this, SIGNAL(itemSelectionChanged()));
1434 }
1435}
1436
1445{
1446 Q_D(const QListWidget);
1447 if (row < 0 || row >= d->model->rowCount())
1448 return nullptr;
1449 return d->listModel()->at(row);
1450}
1451
1459{
1460 Q_D(const QListWidget);
1461 return d->listModel()->index(const_cast<QListWidgetItem*>(item)).row();
1462}
1463
1464
1472{
1473 Q_D(QListWidget);
1474 if (item && !item->view)
1475 d->listModel()->insert(row, item);
1476}
1477
1486{
1487 Q_D(QListWidget);
1488 d->listModel()->insert(row, new QListWidgetItem(label));
1489}
1490
1499{
1500 Q_D(QListWidget);
1501 d->listModel()->insert(row, labels);
1502}
1503
1515{
1516 Q_D(QListWidget);
1517 if (row < 0 || row >= d->model->rowCount())
1518 return nullptr;
1519 return d->listModel()->take(row);
1520}
1521
1528{
1529 Q_D(const QListWidget);
1530 return d->model->rowCount();
1531}
1532
1537{
1538 Q_D(const QListWidget);
1539 return d->listModel()->at(currentIndex().row());
1540}
1541
1542
1550{
1552}
1553
1558void QListWidget::setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)
1559{
1560 setCurrentRow(row(item), command);
1561}
1562
1571{
1572 return currentIndex().row();
1573}
1574
1576{
1577 Q_D(QListWidget);
1578 QModelIndex index = d->listModel()->index(row);
1579 if (d->selectionMode == SingleSelection)
1581 else if (d->selectionMode == NoSelection)
1583 else
1585}
1586
1592void QListWidget::setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
1593{
1594 Q_D(QListWidget);
1595 d->selectionModel->setCurrentIndex(d->listModel()->index(row), command);
1596}
1597
1604{
1605 Q_D(const QListWidget);
1606 return d->listModel()->at(indexAt(p).row());
1607
1608}
1609
1625{
1626 Q_D(const QListWidget);
1627 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1628 return visualRect(index);
1629}
1630
1635{
1636 Q_D(QListWidget);
1637 d->sortOrder = order;
1638 d->listModel()->sort(0, order);
1639}
1640
1652{
1653 Q_D(QListWidget);
1654 d->sortingEnabled = enable;
1655}
1656
1658{
1659 Q_D(const QListWidget);
1660 return d->sortingEnabled;
1661}
1662
1666Qt::SortOrder QListWidget::sortOrder() const
1667{
1668 Q_D(const QListWidget);
1669 return d->sortOrder;
1670}
1671
1677{
1678 Q_D(QListWidget);
1679 edit(d->listModel()->index(item));
1680}
1681
1689{
1690 Q_D(QListWidget);
1691 QModelIndex index = d->listModel()->index(item);
1693}
1694
1701{
1702 Q_D(QListWidget);
1703 QModelIndex index = d->listModel()->index(item);
1705}
1706
1715{
1716 Q_D(const QListWidget);
1717 const QModelIndex index = d->listModel()->index(item);
1719}
1720
1729{
1730 Q_D(const QListWidget);
1731 QModelIndex index = d->listModel()->index(item);
1733}
1734
1748{
1749 Q_D(QListWidget);
1750 QModelIndex index = d->listModel()->index(item);
1752}
1753
1759{
1760 Q_D(const QListWidget);
1763 const int numIndexes = indexes.size();
1764 items.reserve(numIndexes);
1765 for (int i = 0; i < numIndexes; ++i)
1766 items.append(d->listModel()->at(indexes.at(i).row()));
1767 return items;
1768}
1769
1776{
1777 Q_D(const QListWidget);
1778 QModelIndexList indexes = d->listModel()->match(model()->index(0, 0, QModelIndex()),
1779 Qt::DisplayRole, text, -1, flags);
1781 const int indexesSize = indexes.size();
1782 items.reserve(indexesSize);
1783 for (int i = 0; i < indexesSize; ++i)
1784 items.append(d->listModel()->at(indexes.at(i).row()));
1785 return items;
1786}
1787
1795{
1796 Q_D(QListWidget);
1797 QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
1799}
1800
1807{
1808 Q_D(QListWidget);
1809 selectionModel()->clear();
1810 d->listModel()->clear();
1811}
1812
1820{
1821 return d_func()->listModel()->QAbstractListModel::mimeTypes();
1822}
1823
1833{
1834 Q_D(const QListWidget);
1835
1836 QModelIndexList &cachedIndexes = d->listModel()->cachedIndexes;
1837
1838 // if non empty, it's called from the model's own mimeData
1839 if (cachedIndexes.isEmpty()) {
1840 cachedIndexes.reserve(items.size());
1841 for (QListWidgetItem *item : items)
1842 cachedIndexes << indexFromItem(item);
1843
1844 QMimeData *result = d->listModel()->internalMimeData();
1845
1846 cachedIndexes.clear();
1847 return result;
1848 }
1849
1850 return d->listModel()->internalMimeData();
1851}
1852
1853#if QT_CONFIG(draganddrop)
1861bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
1862{
1863 QModelIndex idx;
1864 int row = index;
1865 int column = 0;
1866 if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
1867 // QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1
1868 idx = model()->index(row, column);
1869 row = -1;
1870 column = -1;
1871 }
1872 return d_func()->listModel()->QAbstractListModel::dropMimeData(data, action , row, column, idx);
1873}
1874
1876void QListWidget::dropEvent(QDropEvent *event)
1877{
1878 QListView::dropEvent(event);
1879}
1880
1886Qt::DropActions QListWidget::supportedDropActions() const
1887{
1888 Q_D(const QListWidget);
1889 return d->listModel()->QAbstractListModel::supportedDropActions() | Qt::MoveAction;
1890}
1891#endif // QT_CONFIG(draganddrop)
1892
1899{
1900 const QListWidgetMimeData *lwd = qobject_cast<const QListWidgetMimeData*>(data);
1901 if (lwd)
1902 return lwd->items;
1903 return QList<QListWidgetItem*>();
1904}
1905
1913{
1914 Q_D(const QListWidget);
1915 return d->listModel()->index(item);
1916}
1917
1923{
1924 Q_D(const QListWidget);
1925 if (d->isIndexValid(index))
1926 return d->listModel()->at(index.row());
1927 return nullptr;
1928}
1929
1934{
1935 Q_ASSERT(!"QListWidget::setModel() - Changing the model of the QListWidget is not allowed.");
1936}
1937
1942{
1943 return QListView::event(e);
1944}
1945
1947
1948#include "moc_qlistwidget.cpp"
1949#include "moc_qlistwidget_p.cpp"
static bool variantLessThan(const QVariant &v1, const QVariant &v2)
virtual Qt::DropActions supportedDropActions() const
void endResetModel()
Completes a model reset operation.
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns {true} if the model returns a valid QModelIndex for row and column with parent,...
void endRemoveRows()
Ends a row removal operation.
QModelIndexList persistentIndexList() const
void endMoveRows()
Ends a row move operation.
Q_INVOKABLE int int const QModelIndex & destinationParent
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
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 QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
Q_INVOKABLE int int const QModelIndex int destinationChild
void layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options=CheckIndexOption::NoOption) const
Q_INVOKABLE int sourceRow
void endInsertRows()
Ends a row insertion operation.
void beginResetModel()
Begins a model reset operation.
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 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.
SelectionMode
This enum indicates how the view responds to user selections:
QWidget * indexWidget(const QModelIndex &index) const
QAbstractItemModel * model() const
Returns the model that this view is presenting.
virtual void setSelectionModel(QItemSelectionModel *selectionModel)
Sets the current selection model to the given selectionModel.
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This slot is called when the selection is changed.
QModelIndex currentIndex() const
Returns the model index of the current item.
bool isPersistentEditorOpen(const QModelIndex &index) const
void setIndexWidget(const QModelIndex &index, QWidget *widget)
virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous)
This slot is called when a new item becomes the current item.
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.
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
Definition qcoreevent.h:45
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
QModelIndexList selectedIndexes
virtual void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
Sets the model item index to be the current item, and emits currentChanged().
virtual void clear()
Clears the selection model.
\inmodule QtCore
QMimeData * internalMimeData() const
void ensureSorted(int column, Qt::SortOrder order, int start, int end)
QModelIndex index(const QListWidgetItem *item) const
bool insertRows(int row, int count=1, const QModelIndex &parent=QModelIndex()) override
void insert(int row, QListWidgetItem *item)
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.
QListModel(QListWidget *parent)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
void sort(int column, Qt::SortOrder order) override
QStringList mimeTypes() const override
Returns the list of allowed MIME types.
static bool itemLessThan(const QPair< QListWidgetItem *, int > &left, const QPair< QListWidgetItem *, int > &right)
QListWidgetItem * take(int row)
void remove(QListWidgetItem *item)
void move(int srcRow, int dstRow)
void itemChanged(QListWidgetItem *item, const QList< int > &roles=QList< int >())
static bool itemGreaterThan(const QPair< QListWidgetItem *, int > &left, const QPair< QListWidgetItem *, int > &right)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Sets the role data for the item at index to value.
QListWidgetItem * at(int row) const
bool removeRows(int row, int count=1, const QModelIndex &parent=QModelIndex()) override
Qt::ItemFlags flags(const QModelIndex &index) const override
\reimp
QMimeData * mimeData(const QModelIndexList &indexes) const override
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
static QList< QListWidgetItem * >::iterator sortedInsertionIterator(const QList< QListWidgetItem * >::iterator &begin, const QList< QListWidgetItem * >::iterator &end, Qt::SortOrder order, QListWidgetItem *item)
bool clearItemData(const QModelIndex &index) override
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.
The QListView class provides a list or icon view onto a model.
Definition qlistview.h:17
void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible) override
\reimp
bool event(QEvent *e) override
\reimp
QRect visualRect(const QModelIndex &index) const override
\reimp
QModelIndex indexAt(const QPoint &p) const override
\reimp
QList< QWidgetItemData > values
The QListWidgetItem class provides an item for use with the QListWidget item view class.
Definition qlistwidget.h:23
QListWidgetItem(QListWidget *listview=nullptr, int type=Type)
Constructs an empty list widget item of the specified type with the given parent.
bool isSelected() const
QString text() const
Returns the list item's text.
Definition qlistwidget.h:48
QDataStream & operator<<(QDataStream &out, const QListWidgetItem &item)
Writes the list widget item item to stream out.
QIcon icon() const
Returns the list item's icon.
Definition qlistwidget.h:52
QDataStream & operator>>(QDataStream &in, QListWidgetItem &item)
Reads a list widget item from stream in into item.
QListWidgetItem & operator=(const QListWidgetItem &other)
Assigns other's data and flags to this item.
virtual void read(QDataStream &in)
Reads the item from stream in.
virtual QListWidgetItem * clone() const
Creates an exact copy of the item.
virtual void write(QDataStream &out) const
Writes the item to stream out.
void setFlags(Qt::ItemFlags flags)
Sets the item flags for the list item to flags.
void setSelected(bool select)
virtual QVariant data(int role) const
Returns the item's data for a given role.
virtual void setData(int role, const QVariant &value)
Sets the data for a given role to the given value.
virtual bool operator<(const QListWidgetItem &other) const
Returns true if this item's text is less then other item's text; otherwise returns false.
virtual ~QListWidgetItem()
Destroys the list item.
Qt::ItemFlags flags() const
Returns the item flags for this item (see \l{Qt::ItemFlags}).
Definition qlistwidget.h:45
QList< QListWidgetItem * > items
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QListModel * listModel() const
void _q_emitItemPressed(const QModelIndex &index)
void _q_emitItemEntered(const QModelIndex &index)
void _q_emitItemChanged(const QModelIndex &index)
void _q_emitItemActivated(const QModelIndex &index)
void _q_emitItemClicked(const QModelIndex &index)
void _q_emitItemDoubleClicked(const QModelIndex &index)
void _q_emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous)
Qt::SortOrder sortOrder
The QListWidget class provides an item-based list widget.
QList< QListWidgetItem * > items(const QMimeData *data) const
Returns a list of pointers to the items contained in the data object.
QModelIndex indexFromItem(const QListWidgetItem *item) const
Returns the QModelIndex associated with the given item.
QListWidgetItem * item(int row) const
Returns the item that occupies the given row in the list if one has been set; otherwise returns \null...
QListWidgetItem * takeItem(int row)
Removes and returns the item from the given row in the list widget; otherwise returns \nullptr.
void openPersistentEditor(QListWidgetItem *item)
Opens an editor for the given item.
QRect visualItemRect(const QListWidgetItem *item) const
Returns the rectangle on the viewport occupied by the item at item.
void setModel(QAbstractItemModel *model) override
void setCurrentRow(int row)
QList< QListWidgetItem * > selectedItems() const
Returns a list of all selected items in the list widget.
QListWidgetItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QListWidgetItem associated with the given index.
QList< QListWidgetItem * > findItems(const QString &text, Qt::MatchFlags flags) const
Finds items with the text that matches the string text using the given flags.
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of listwidget items.
void insertItem(int row, QListWidgetItem *item)
Inserts the item at the position in the list given by row.
void editItem(QListWidgetItem *item)
Starts editing the item if it is editable.
bool event(QEvent *e) override
\reimp
bool isPersistentEditorOpen(QListWidgetItem *item) const
void sortItems(Qt::SortOrder order=Qt::AscendingOrder)
Sorts all the items in the list widget according to the specified order.
int currentRow
the row of the current item.
friend class QListWidgetItem
void setSortingEnabled(bool enable)
void closePersistentEditor(QListWidgetItem *item)
Closes the persistent editor for the given item.
QListWidgetItem * currentItem() const
Returns the current item.
QListWidgetItem * itemAt(const QPoint &p) const
Returns a pointer to the item at the coordinates p.
void setItemWidget(QListWidgetItem *item, QWidget *widget)
void clear()
Removes all items and selections in the view.
bool isSortingEnabled() const
void setSelectionModel(QItemSelectionModel *selectionModel) override
\reimp
~QListWidget()
Destroys the list widget and all its items.
void setCurrentItem(QListWidgetItem *item)
Sets the current item to item.
void insertItems(int row, const QStringList &labels)
Inserts items from the list of labels into the list, starting at the given row.
int row(const QListWidgetItem *item) const
Returns the row containing the given item.
virtual QMimeData * mimeData(const QList< QListWidgetItem * > &items) const
Returns an object that contains a serialized description of the specified items.
QListWidget(QWidget *parent=nullptr)
Constructs an empty QListWidget with the given parent.
void itemSelectionChanged()
This signal is emitted whenever the selection changes.
int count
the number of items in the list including any hidden items.
QWidget * itemWidget(QListWidgetItem *item) const
void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint=EnsureVisible)
Scrolls the view if necessary to ensure that the item is visible.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
T & first()
Definition qlist.h:628
void removeAt(qsizetype i)
Definition qlist.h:573
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:471
iterator end()
Definition qlist.h:609
T takeAt(qsizetype i)
Definition qlist.h:592
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
T value(qsizetype i) const
Definition qlist.h:661
void move(qsizetype from, qsizetype to)
Definition qlist.h:593
iterator begin()
Definition qlist.h:608
void reserve(qsizetype size)
Definition qlist.h:746
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
Definition qmap.h:186
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
\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
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3099
bool isValid() const
Returns {true} if this persistent model index is valid; otherwise returns {false}.
int row() const
Returns the row this persistent model index refers to.
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
Exception-safe wrapper around QObject::blockSignals().
Definition qobject.h:443
int rowCount(const QModelIndex &parent=QModelIndex()) const override
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
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
double pi
[0]
double e
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ DecorationRole
@ EditRole
@ DisplayRole
SortOrder
Definition qnamespace.h:120
@ AscendingOrder
Definition qnamespace.h:121
DropAction
@ MoveAction
@ ItemIsDropEnabled
std::pair< T1, T2 > QPair
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLint GLfloat GLfloat GLfloat v2
GLboolean GLboolean GLboolean b
GLuint index
[2]
GLboolean r
[2]
GLuint GLuint end
GLenum GLenum GLsizei count
GLdouble GLdouble right
GLint left
GLenum type
GLuint GLsizei const GLchar * label
[43]
GLbitfield flags
GLint GLfloat GLfloat v1
GLboolean enable
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
GLenum GLenum GLsizei void GLsizei void * column
struct _cl_event * event
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 QT_BEGIN_INCLUDE_NAMESPACE
#define QT_END_INCLUDE_NAMESPACE
#define Q_OBJECT
#define emit
#define Q_UNUSED(x)
static int compare(quint64 a, quint64 b)
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
QAction * at
QQuickView * view
[0]
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:955
qsizetype lastIndexOf(const AT &t, qsizetype from=-1) const noexcept
Definition qlist.h:962
Definition moc.h:24
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent