1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qlistwidget.h"
#include <qitemdelegate.h>
#include <private/qlistview_p.h>
#include <private/qwidgetitemdata_p.h>
#include <private/qlistwidget_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
class QListWidgetMimeData : public QMimeData
{
Q_OBJECT
public:
QList<QListWidgetItem*> items;
};
QT_BEGIN_INCLUDE_NAMESPACE
#include "qlistwidget.moc"
QT_END_INCLUDE_NAMESPACE
QListModel::QListModel(QListWidget *parent)
: QAbstractListModel(parent)
{
}
QListModel::~QListModel()
{
clear();
}
void QListModel::clear()
{
beginResetModel();
for (int i = 0; i < items.count(); ++i) {
if (items.at(i)) {
items.at(i)->d->theid = -1;
items.at(i)->view = 0;
delete items.at(i);
}
}
items.clear();
endResetModel();
}
QListWidgetItem *QListModel::at(int row) const
{
return items.value(row);
}
void QListModel::remove(QListWidgetItem *item)
{
if (!item)
return;
int row = items.indexOf(item); // ### use index(item) - it's faster
Q_ASSERT(row != -1);
beginRemoveRows(QModelIndex(), row, row);
items.at(row)->d->theid = -1;
items.at(row)->view = 0;
items.removeAt(row);
endRemoveRows();
}
void QListModel::insert(int row, QListWidgetItem *item)
{
if (!item)
return;
item->view = qobject_cast<QListWidget*>(QObject::parent());
if (item->view && item->view->isSortingEnabled()) {
// sorted insertion
QList<QListWidgetItem*>::iterator it;
it = sortedInsertionIterator(items.begin(), items.end(),
item->view->sortOrder(), item);
row = qMax(it - items.begin(), 0);
} else {
if (row < 0)
row = 0;
else if (row > items.count())
row = items.count();
}
beginInsertRows(QModelIndex(), row, row);
items.insert(row, item);
item->d->theid = row;
endInsertRows();
}
void QListModel::insert(int row, const QStringList &labels)
{
const int count = labels.count();
if (count <= 0)
return;
QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
if (view && view->isSortingEnabled()) {
// sorted insertion
for (int i = 0; i < count; ++i) {
QListWidgetItem *item = new QListWidgetItem(labels.at(i));
insert(row, item);
}
} else {
if (row < 0)
row = 0;
else if (row > items.count())
row = items.count();
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
QListWidgetItem *item = new QListWidgetItem(labels.at(i));
item->d->theid = row;
item->view = qobject_cast<QListWidget*>(QObject::parent());
items.insert(row++, item);
}
endInsertRows();
}
}
QListWidgetItem *QListModel::take(int row)
{
if (row < 0 || row >= items.count())
return 0;
beginRemoveRows(QModelIndex(), row, row);
items.at(row)->d->theid = -1;
items.at(row)->view = 0;
QListWidgetItem *item = items.takeAt(row);
endRemoveRows();
return item;
}
void QListModel::move(int srcRow, int dstRow)
{
if (srcRow == dstRow
|| srcRow < 0 || srcRow >= items.count()
|| dstRow < 0 || dstRow > items.count())
return;
if (!beginMoveRows(QModelIndex(), srcRow, srcRow, QModelIndex(), dstRow))
return;
if (srcRow < dstRow)
--dstRow;
items.move(srcRow, dstRow);
endMoveRows();
}
int QListModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : items.count();
}
QModelIndex QListModel::index(const QListWidgetItem *item_) const
{
QListWidgetItem *item = const_cast<QListWidgetItem *>(item_);
if (!item || !item->view || static_cast<const QListModel *>(item->view->model()) != this
|| items.isEmpty())
return QModelIndex();
int row;
const int theid = item->d->theid;
if (theid >= 0 && theid < items.count() && items.at(theid) == item) {
row = theid;
} else { // we need to search for the item
row = items.lastIndexOf(item); // lastIndexOf is an optimization in favor of indexOf
if (row == -1) // not found
return QModelIndex();
item->d->theid = row;
}
return createIndex(row, 0, item);
}
QModelIndex QListModel::index(int row, int column, const QModelIndex &parent) const
{
if (hasIndex(row, column, parent))
return createIndex(row, column, items.at(row));
return QModelIndex();
}
QVariant QListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= items.count())
return QVariant();
return items.at(index.row())->data(role);
}
bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || index.row() >= items.count())
return false;
items.at(index.row())->setData(role, value);
return true;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool QListModel::clearItemData(const QModelIndex &index)
{
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
return false;
QListWidgetItem *item = items.at(index.row());
const auto beginIter = item->d->values.cbegin();
const auto endIter = item->d->values.cend();
if (std::all_of(beginIter, endIter, [](const QWidgetItemData& data) -> bool { return !data.value.isValid(); }))
return true; //it's already cleared
item->d->values.clear();
emit dataChanged(index, index, QVector<int>{});
return true;
}
#endif
QMap<int, QVariant> QListModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles;
if (!index.isValid() || index.row() >= items.count())
return roles;
QListWidgetItem *itm = items.at(index.row());
for (int i = 0; i < itm->d->values.count(); ++i) {
roles.insert(itm->d->values.at(i).role,
itm->d->values.at(i).value);
}
return roles;
}
bool QListModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
QListWidgetItem *itm = 0;
for (int r = row; r < row + count; ++r) {
itm = new QListWidgetItem;
itm->view = view;
itm->d->theid = r;
items.insert(r, itm);
}
endInsertRows();
return true;
}
bool QListModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
QListWidgetItem *itm = 0;
for (int r = row; r < row + count; ++r) {
itm = items.takeAt(row);
itm->view = 0;
itm->d->theid = -1;
delete itm;
}
endRemoveRows();
return true;
}
/*!
\since 5.13
\reimp
*/
bool QListModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
{
if (sourceRow < 0
|| sourceRow + count - 1 >= rowCount(sourceParent)
|| destinationChild <= 0
|| destinationChild > rowCount(destinationParent)
|| sourceRow == destinationChild - 1
|| count <= 0) {
return false;
}
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
return false;
destinationChild--;
const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow;
while (count--)
items.move(fromRow, destinationChild);
endMoveRows();
return true;
}
Qt::ItemFlags QListModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || index.row() >= items.count() || index.model() != this)
return Qt::ItemIsDropEnabled; // we allow drops outside the items
return items.at(index.row())->flags();
}
void QListModel::sort(int column, Qt::SortOrder order)
{
if (column != 0)
return;
emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
QVector < QPair<QListWidgetItem*,int> > sorting(items.count());
for (int i = 0; i < items.count(); ++i) {
QListWidgetItem *item = items.at(i);
sorting[i].first = item;
sorting[i].second = i;
}
const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
std::sort(sorting.begin(), sorting.end(), compare);
QModelIndexList fromIndexes;
QModelIndexList toIndexes;
const int sortingCount = sorting.count();
fromIndexes.reserve(sortingCount);
toIndexes.reserve(sortingCount);
for (int r = 0; r < sortingCount; ++r) {
QListWidgetItem *item = sorting.at(r).first;
toIndexes.append(createIndex(r, 0, item));
fromIndexes.append(createIndex(sorting.at(r).second, 0, sorting.at(r).first));
items[r] = sorting.at(r).first;
}
changePersistentIndexList(fromIndexes, toIndexes);
emit layoutChanged({}, QAbstractItemModel::VerticalSortHint);
}
/**
* This function assumes that all items in the model except the items that are between
* (inclusive) start and end are sorted.
* With these assumptions, this function can ensure that the model is sorted in a
* much more efficient way than doing a naive 'sort everything'.
* (provided that the range is relatively small compared to the total number of items)
*/
void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int end)
{
if (column != 0)
return;
int count = end - start + 1;
QVector < QPair<QListWidgetItem*,int> > sorting(count);
for (int i = 0; i < count; ++i) {
sorting[i].first = items.at(start + i);
sorting[i].second = start + i;
}
const auto compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
std::sort(sorting.begin(), sorting.end(), compare);
QModelIndexList oldPersistentIndexes = persistentIndexList();
QModelIndexList newPersistentIndexes = oldPersistentIndexes;
QList<QListWidgetItem*> tmp = items;
QList<QListWidgetItem*>::iterator lit = tmp.begin();
bool changed = false;
for (int i = 0; i < count; ++i) {
int oldRow = sorting.at(i).second;
int tmpitepos = lit - tmp.begin();
QListWidgetItem *item = tmp.takeAt(oldRow);
if (tmpitepos > tmp.size())
--tmpitepos;
lit = tmp.begin() + tmpitepos;
lit = sortedInsertionIterator(lit, tmp.end(), order, item);
int newRow = qMax(lit - tmp.begin(), 0);
lit = tmp.insert(lit, item);
if (newRow != oldRow) {
changed = true;
for (int j = i + 1; j < count; ++j) {
int otherRow = sorting.at(j).second;
if (oldRow < otherRow && newRow >= otherRow)
--sorting[j].second;
else if (oldRow > otherRow && newRow <= otherRow)
++sorting[j].second;
}
for (int k = 0; k < newPersistentIndexes.count(); ++k) {
QModelIndex pi = newPersistentIndexes.at(k);
int oldPersistentRow = pi.row();
int newPersistentRow = oldPersistentRow;
if (oldPersistentRow == oldRow)
newPersistentRow = newRow;
else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
newPersistentRow = oldPersistentRow - 1;
else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
newPersistentRow = oldPersistentRow + 1;
if (newPersistentRow != oldPersistentRow)
newPersistentIndexes[k] = createIndex(newPersistentRow,
pi.column(), pi.internalPointer());
}
}
}
if (changed) {
emit layoutAboutToBeChanged();
items = tmp;
changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
emit layoutChanged();
}
}
bool QListModel::itemLessThan(const QPair<QListWidgetItem*,int> &left,
const QPair<QListWidgetItem*,int> &right)
{
return (*left.first) < (*right.first);
}
bool QListModel::itemGreaterThan(const QPair<QListWidgetItem*,int> &left,
const QPair<QListWidgetItem*,int> &right)
{
return (*right.first) < (*left.first);
}
QList<QListWidgetItem*>::iterator QListModel::sortedInsertionIterator(
const QList<QListWidgetItem*>::iterator &begin,
const QList<QListWidgetItem*>::iterator &end,
Qt::SortOrder order, QListWidgetItem *item)
{
if (order == Qt::AscendingOrder)
return std::lower_bound(begin, end, item, QListModelLessThan());
return std::lower_bound(begin, end, item, QListModelGreaterThan());
}
void QListModel::itemChanged(QListWidgetItem *item, const QVector<int> &roles)
{
const QModelIndex idx = index(item);
emit dataChanged(idx, idx, roles);
}
QStringList QListModel::mimeTypes() const
{
const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
return view->mimeTypes();
}
QMimeData *QListModel::internalMimeData() const
{
return QAbstractItemModel::mimeData(cachedIndexes);
}
QMimeData *QListModel::mimeData(const QModelIndexList &indexes) const
{
QList<QListWidgetItem*> itemlist;
const int indexesCount = indexes.count();
itemlist.reserve(indexesCount);
for (int i = 0; i < indexesCount; ++i)
itemlist << at(indexes.at(i).row());
const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
cachedIndexes = indexes;
QMimeData *mimeData = view->mimeData(itemlist);
cachedIndexes.clear();
return mimeData;
}
#if QT_CONFIG(draganddrop)
bool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &index)
{
Q_UNUSED(column);
QListWidget *view = qobject_cast<QListWidget*>(QObject::parent());
if (index.isValid())
row = index.row();
else if (row == -1)
row = items.count();
return view->dropMimeData(row, data, action);
}
Qt::DropActions QListModel::supportedDropActions() const
{
const QListWidget *view = qobject_cast<const QListWidget*>(QObject::parent());
return view->supportedDropActions();
}
#endif // QT_CONFIG(draganddrop)
/*!
\class QListWidgetItem
\brief The QListWidgetItem class provides an item for use with the
QListWidget item view class.
\ingroup model-view
\inmodule QtWidgets
A QListWidgetItem represents a single item in a QListWidget. Each item can
hold several pieces of information, and will display them appropriately.
The item view convenience classes use a classic item-based interface rather
than a pure model/view approach. For a more flexible list view widget,
consider using the QListView class with a standard model.
List items can be inserted automatically into a list, when they are
constructed, by specifying the list widget:
\snippet qlistwidget-using/mainwindow.cpp 2
Alternatively, list items can also be created without a parent widget, and
later inserted into a list using QListWidget::insertItem().
List items are typically used to display text() and an icon(). These are
set with the setText() and setIcon() functions. The appearance of the text
can be customized with setFont(), setForeground(), and setBackground().
Text in list items can be aligned using the setTextAlignment() function.
Tooltips, status tips and "What's This?" help can be added to list items
with setToolTip(), setStatusTip(), and setWhatsThis().
By default, items are enabled, selectable, checkable, and can be the source
of drag and drop operations.
Each item's flags can be changed by calling setFlags() with the appropriate
value (see Qt::ItemFlags). Checkable items can be checked, unchecked and
partially checked with the setCheckState() function. The corresponding
checkState() function indicates the item's current check state.
The isHidden() function can be used to determine whether the item is
hidden. To hide an item, use setHidden().
\section1 Subclassing
When subclassing QListWidgetItem to provide custom items, it is possible to
define new types for them enabling them to be distinguished from standard
items. For subclasses that require this feature, ensure that you call the
base class constructor with a new type value equal to or greater than
\l UserType, within \e your constructor.
\sa QListWidget, {Model/View Programming}, QTreeWidgetItem, QTableWidgetItem
*/
/*!
\enum QListWidgetItem::ItemType
This enum describes the types that are used to describe list widget items.
\value Type The default type for list widget items.
\value UserType The minimum value for custom types. Values below UserType are
reserved by Qt.
You can define new user types in QListWidgetItem subclasses to ensure that
custom items are treated specially.
\sa type()
*/
/*!
\fn int QListWidgetItem::type() const
Returns the type passed to the QListWidgetItem constructor.
*/
/*!
\fn QListWidget *QListWidgetItem::listWidget() const
Returns the list widget containing the item.
*/
/*!
\fn void QListWidgetItem::setHidden(bool hide)
\since 4.2
Hides the item if \a hide is true; otherwise shows the item.
\sa isHidden()
*/
/*!
\fn bool QListWidgetItem::isHidden() const
\since 4.2
Returns \c true if the item is hidden; otherwise returns \c false.
\sa setHidden()
*/
/*!
\fn QListWidgetItem::QListWidgetItem(QListWidget *parent, int type)
Constructs an empty list widget item of the specified \a type with the
given \a parent. If \a parent is not specified, the item will need to be
inserted into a list widget with QListWidget::insertItem().
This constructor inserts the item into the model of the parent that is
passed to the constructor. If the model is sorted then the behavior of the
insert is undetermined since the model will call the \c '<' operator method
on the item which, at this point, is not yet constructed. To avoid the
undetermined behavior, we recommend not to specify the parent and use
QListWidget::insertItem() instead.
\sa type()
*/
QListWidgetItem::QListWidgetItem(QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
if (QListModel *model = listModel())
model->insert(model->rowCount(), this);
}
/*!
\fn QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *parent, int type)
Constructs an empty list widget item of the specified \a type with the
given \a text and \a parent. If the parent is not specified, the item will
need to be inserted into a list widget with QListWidget::insertItem().
This constructor inserts the item into the model of the parent that is
passed to the constructor. If the model is sorted then the behavior of the
insert is undetermined since the model will call the \c '<' operator method
on the item which, at this point, is not yet constructed. To avoid the
undetermined behavior, we recommend not to specify the parent and use
QListWidget::insertItem() instead.
\sa type()
*/
QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
QListModel *model = listModel();
{
QSignalBlocker b(view);
QSignalBlocker bm(model);
setData(Qt::DisplayRole, text);
}
if (model)
model->insert(model->rowCount(), this);
}
/*!
\fn QListWidgetItem::QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent, int type)
Constructs an empty list widget item of the specified \a type with the
given \a icon, \a text and \a parent. If the parent is not specified, the
item will need to be inserted into a list widget with
QListWidget::insertItem().
This constructor inserts the item into the model of the parent that is
passed to the constructor. If the model is sorted then the behavior of the
insert is undetermined since the model will call the \c '<' operator method
on the item which, at this point, is not yet constructed. To avoid the
undetermined behavior, we recommend not to specify the parent and use
QListWidget::insertItem() instead.
\sa type()
*/
QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text,
QListWidget *listview, int type)
: rtti(type), view(listview), d(new QListWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled)
{
QListModel *model = listModel();
{
QSignalBlocker b(view);
QSignalBlocker bm(model);
setData(Qt::DisplayRole, text);
setData(Qt::DecorationRole, icon);
}
if (model)
model->insert(model->rowCount(), this);
}
/*!
Destroys the list item.
*/
QListWidgetItem::~QListWidgetItem()
{
if (QListModel *model = listModel())
model->remove(this);
delete d;
}
/*!
Creates an exact copy of the item.
*/
QListWidgetItem *QListWidgetItem::clone() const
{
return new QListWidgetItem(*this);
}
/*!
Sets the data for a given \a role to the given \a value. Reimplement this
function if you need extra roles or special behavior for certain roles.
\note The default implementation treats Qt::EditRole and Qt::DisplayRole as
referring to the same data.
\sa Qt::ItemDataRole, data()
*/
void QListWidgetItem::setData(int role, const QVariant &value)
{
bool found = false;
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
for (int i = 0; i < d->values.count(); ++i) {
if (d->values.at(i).role == role) {
if (d->values.at(i).value == value)
return;
d->values[i].value = value;
found = true;
break;
}
}
if (!found)
d->values.append(QWidgetItemData(role, value));
if (QListModel *model = listModel()) {
const QVector<int> roles((role == Qt::DisplayRole) ?
QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
QVector<int>({role}));
model->itemChanged(this, roles);
}
}
/*!
Returns the item's data for a given \a role. Reimplement this function if
you need extra roles or special behavior for certain roles.
\sa Qt::ItemDataRole, setData()
*/
QVariant QListWidgetItem::data(int role) const
{
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
for (int i = 0; i < d->values.count(); ++i)
if (d->values.at(i).role == role)
return d->values.at(i).value;
return QVariant();
}
/*!
Returns \c true if this item's text is less then \a other item's text;
otherwise returns \c false.
*/
bool QListWidgetItem::operator<(const QListWidgetItem &other) const
{
const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole);
return QAbstractItemModelPrivate::variantLessThan(v1, v2);
}
#ifndef QT_NO_DATASTREAM
/*!
Reads the item from stream \a in.
\sa write()
*/
void QListWidgetItem::read(QDataStream &in)
{
in >> d->values;
}
/*!
Writes the item to stream \a out.
\sa read()
*/
void QListWidgetItem::write(QDataStream &out) const
{
out << d->values;
}
#endif // QT_NO_DATASTREAM
/*!
\since 4.1
Constructs a copy of \a other. Note that type() and listWidget() are not
copied.
This function is useful when reimplementing clone().
\sa data(), flags()
*/
QListWidgetItem::QListWidgetItem(const QListWidgetItem &other)
: rtti(Type), view(nullptr),
d(new QListWidgetItemPrivate(this)),
itemFlags(other.itemFlags)
{
d->values = other.d->values;
}
/*!
Assigns \a other's data and flags to this item. Note that type() and
listWidget() are not copied.
This function is useful when reimplementing clone().
\sa data(), flags()
*/
QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other)
{
d->values = other.d->values;
itemFlags = other.itemFlags;
return *this;
}
/*!
\internal
returns the QListModel if a view is set
*/
QListModel *QListWidgetItem::listModel() const
{
return (view ? qobject_cast<QListModel*>(view->model()) : nullptr);
}
#ifndef QT_NO_DATASTREAM
/*!
\relates QListWidgetItem
Writes the list widget item \a item to stream \a out.
This operator uses QListWidgetItem::write().
\sa {Serializing Qt Data Types}
*/
QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item)
{
item.write(out);
return out;
}
/*!
\relates QListWidgetItem
Reads a list widget item from stream \a in into \a item.
This operator uses QListWidgetItem::read().
\sa {Serializing Qt Data Types}
*/
QDataStream &operator>>(QDataStream &in, QListWidgetItem &item)
{
item.read(in);
return in;
}
#endif // QT_NO_DATASTREAM
/*!
\fn Qt::ItemFlags QListWidgetItem::flags() const
Returns the item flags for this item (see \l{Qt::ItemFlags}).
*/
/*!
\fn QString QListWidgetItem::text() const
Returns the list item's text.
\sa setText()
*/
/*!
\fn QIcon QListWidgetItem::icon() const
Returns the list item's icon.
\sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
*/
/*!
\fn QString QListWidgetItem::statusTip() const
Returns the list item's status tip.
\sa setStatusTip()
*/
/*!
\fn QString QListWidgetItem::toolTip() const
Returns the list item's tooltip.
\sa setToolTip(), statusTip(), whatsThis()
*/
/*!
\fn QString QListWidgetItem::whatsThis() const
Returns the list item's "What's This?" help text.
\sa setWhatsThis(), statusTip(), toolTip()
*/
/*!
\fn QFont QListWidgetItem::font() const
Returns the font used to display this list item's text.
*/
/*!
\fn int QListWidgetItem::textAlignment() const
Returns the text alignment for the list item.
\sa Qt::AlignmentFlag
*/
#if QT_DEPRECATED_SINCE(5, 13)
/*!
\fn QColor QListWidgetItem::backgroundColor() const
\obsolete
This function is deprecated. Use background() instead.
*/
#endif
/*!
\fn QBrush QListWidgetItem::background() const
\since 4.2
Returns the brush used to display the list item's background.
\sa setBackground(), foreground()
*/
#if QT_DEPRECATED_SINCE(5, 13)
/*!
\fn QColor QListWidgetItem::textColor() const
\obsolete
Returns the color used to display the list item's text.
This function is deprecated. Use foreground() instead.
*/
#endif
/*!
\fn QBrush QListWidgetItem::foreground() const
\since 4.2
Returns the brush used to display the list item's foreground (e.g. text).
\sa setForeground(), background()
*/
/*!
\fn Qt::CheckState QListWidgetItem::checkState() const
Returns the checked state of the list item (see \l{Qt::CheckState}).
\sa flags()
*/
/*!
\fn QSize QListWidgetItem::sizeHint() const
\since 4.1
Returns the size hint set for the list item.
*/
/*!
\fn void QListWidgetItem::setSizeHint(const QSize &size)
\since 4.1
Sets the size hint for the list item to be \a size.
If no size hint is set or \a size is invalid, the item
delegate will compute the size hint based on the item data.
*/
/*!
\fn void QListWidgetItem::setSelected(bool select)
\since 4.2
Sets the selected state of the item to \a select.
\sa isSelected()
*/
void QListWidgetItem::setSelected(bool select)
{
const QListModel *model = listModel();
if (!model || !view->selectionModel())
return;
const QAbstractItemView::SelectionMode selectionMode = view->selectionMode();
if (selectionMode == QAbstractItemView::NoSelection)
return;
const QModelIndex index = model->index(this);
if (selectionMode == QAbstractItemView::SingleSelection)
view->selectionModel()->select(index, select
? QItemSelectionModel::ClearAndSelect
: QItemSelectionModel::Deselect);
else
view->selectionModel()->select(index, select
? QItemSelectionModel::Select
: QItemSelectionModel::Deselect);
}
/*!
\fn bool QListWidgetItem::isSelected() const
\since 4.2
Returns \c true if the item is selected; otherwise returns \c false.
\sa setSelected()
*/
bool QListWidgetItem::isSelected() const
{
const QListModel *model = listModel();
if (!model || !view->selectionModel())
return false;
const QModelIndex index = model->index(this);
return view->selectionModel()->isSelected(index);
}
/*!
\fn void QListWidgetItem::setFlags(Qt::ItemFlags flags)
Sets the item flags for the list item to \a flags.
\sa Qt::ItemFlags
*/
void QListWidgetItem::setFlags(Qt::ItemFlags aflags)
{
itemFlags = aflags;
if (QListModel *model = listModel())
model->itemChanged(this);
}
/*!
\fn void QListWidgetItem::setText(const QString &text)
Sets the text for the list widget item's to the given \a text.
\sa text()
*/
/*!
\fn void QListWidgetItem::setIcon(const QIcon &icon)
Sets the icon for the list item to the given \a icon.
\sa icon(), text(), {QAbstractItemView::iconSize}{iconSize}
*/
/*!
\fn void QListWidgetItem::setStatusTip(const QString &statusTip)
Sets the status tip for the list item to the text specified by
\a statusTip. QListWidget mouseTracking needs to be enabled for this
feature to work.
\sa statusTip(), setToolTip(), setWhatsThis(), QWidget::setMouseTracking()
*/
/*!
\fn void QListWidgetItem::setToolTip(const QString &toolTip)
Sets the tooltip for the list item to the text specified by \a toolTip.
\sa toolTip(), setStatusTip(), setWhatsThis()
*/
/*!
\fn void QListWidgetItem::setWhatsThis(const QString &whatsThis)
Sets the "What's This?" help for the list item to the text specified by
\a whatsThis.
\sa whatsThis(), setStatusTip(), setToolTip()
*/
/*!
\fn void QListWidgetItem::setFont(const QFont &font)
Sets the font used when painting the item to the given \a font.
*/
/*!
\fn void QListWidgetItem::setTextAlignment(int alignment)
Sets the list item's text alignment to \a alignment.
\sa Qt::AlignmentFlag
*/
/*!
\fn void QListWidgetItem::setBackgroundColor(const QColor &color)
\obsolete
This function is deprecated. Use setBackground() instead.
*/
/*!
\fn void QListWidgetItem::setBackground(const QBrush &brush)
\since 4.2
Sets the background brush of the list item to the given \a brush.
Setting a default-constructed brush will let the view use the
default color from the style.
\sa background(), setForeground()
*/
#if QT_DEPRECATED_SINCE(5, 13)
/*!
\fn void QListWidgetItem::setTextColor(const QColor &color)
\obsolete
This function is deprecated. Use setForeground() instead.
*/
#endif
/*!
\fn void QListWidgetItem::setForeground(const QBrush &brush)
\since 4.2
Sets the foreground brush of the list item to the given \a brush.
Setting a default-constructed brush will let the view use the
default color from the style.
\sa foreground(), setBackground()
*/
/*!
\fn void QListWidgetItem::setCheckState(Qt::CheckState state)
Sets the check state of the list item to \a state.
\sa checkState()
*/
void QListWidgetPrivate::setup()
{
Q_Q(QListWidget);
q->QListView::setModel(new QListModel(q));
// view signals
QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex)));
QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex)));
QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
q, SLOT(_q_emitItemDoubleClicked(QModelIndex)));
QObject::connect(q, SIGNAL(activated(QModelIndex)),
q, SLOT(_q_emitItemActivated(QModelIndex)));
QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitItemChanged(QModelIndex)));
QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
}
void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemPressed(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemClicked(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemDoubleClicked(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemActivated(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemEntered(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
{
Q_Q(QListWidget);
emit q->itemChanged(listModel()->at(index.row()));
}
void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t,
const QModelIndex &previous)
{
Q_Q(QListWidget);
QPersistentModelIndex persistentCurrent = current;
QListWidgetItem *currentItem = listModel()->at(persistentCurrent.row());
emit q->currentItemChanged(currentItem, listModel()->at(previous.row()));
//persistentCurrent is invalid if something changed the model in response
//to the currentItemChanged signal emission and the item was removed
if (!persistentCurrent.isValid()) {
currentItem = 0;
}
emit q->currentTextChanged(currentItem ? currentItem->text() : QString());
emit q->currentRowChanged(persistentCurrent.row());
}
void QListWidgetPrivate::_q_sort()
{
if (sortingEnabled)
model->sort(0, sortOrder);
}
void QListWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight)
{
if (sortingEnabled && topLeft.isValid() && bottomRight.isValid())
listModel()->ensureSorted(topLeft.column(), sortOrder,
topLeft.row(), bottomRight.row());
}
/*!
\class QListWidget
\brief The QListWidget class provides an item-based list widget.
\ingroup model-view
\inmodule QtWidgets
\image windows-listview.png
QListWidget is a convenience class that provides a list view similar to the
one supplied by QListView, but with a classic item-based interface for
adding and removing items. QListWidget uses an internal model to manage
each QListWidgetItem in the list.
For a more flexible list view widget, use the QListView class with a
standard model.
List widgets are constructed in the same way as other widgets:
\snippet qlistwidget-using/mainwindow.cpp 0
The selectionMode() of a list widget determines how many of the items in
the list can be selected at the same time, and whether complex selections
of items can be created. This can be set with the setSelectionMode()
function.
There are two ways to add items to the list: they can be constructed with
the list widget as their parent widget, or they can be constructed with no
parent widget and added to the list later. If a list widget already exists
when the items are constructed, the first method is easier to use:
\snippet qlistwidget-using/mainwindow.cpp 1
If you need to insert a new item into the list at a particular position,
then it should be constructed without a parent widget. The insertItem()
function should then be used to place it within the list. The list widget
will take ownership of the item.
\snippet qlistwidget-using/mainwindow.cpp 6
\snippet qlistwidget-using/mainwindow.cpp 7
For multiple items, insertItems() can be used instead. The number of items
in the list is found with the count() function. To remove items from the
list, use takeItem().
The current item in the list can be found with currentItem(), and changed
with setCurrentItem(). The user can also change the current item by
navigating with the keyboard or clicking on a different item. When the
current item changes, the currentItemChanged() signal is emitted with the
new current item and the item that was previously current.
\sa QListWidgetItem, QListView, QTreeView, {Model/View Programming},
{Tab Dialog Example}
*/
/*!
\fn void QListWidget::addItem(QListWidgetItem *item)
Inserts the \a item at the end of the list widget.
\warning A QListWidgetItem can only be added to a QListWidget once. Adding
the same QListWidgetItem multiple times to a QListWidget will result in
undefined behavior.
\sa insertItem()
*/
/*!
\fn void QListWidget::addItem(const QString &label)
Inserts an item with the text \a label at the end of the list widget.
*/
/*!
\fn void QListWidget::addItems(const QStringList &labels)
Inserts items with the text \a labels at the end of the list widget.
\sa insertItems()
*/
/*!
\fn void QListWidget::itemPressed(QListWidgetItem *item)
This signal is emitted with the specified \a item when a mouse button is
pressed on an item in the widget.
\sa itemClicked(), itemDoubleClicked()
*/
/*!
\fn void QListWidget::itemClicked(QListWidgetItem *item)
This signal is emitted with the specified \a item when a mouse button is
clicked on an item in the widget.
\sa itemPressed(), itemDoubleClicked()
*/
/*!
\fn void QListWidget::itemDoubleClicked(QListWidgetItem *item)
This signal is emitted with the specified \a item when a mouse button is
double clicked on an item in the widget.
\sa itemClicked(), itemPressed()
*/
/*!
\fn void QListWidget::itemActivated(QListWidgetItem *item)
This signal is emitted when the \a item is activated. The \a item is
activated when the user clicks or double clicks on it, depending on the
system configuration. It is also activated when the user presses the
activation key (on Windows and X11 this is the \uicontrol Return key, on Mac OS
X it is \uicontrol{Command+O}).
*/
/*!
\fn void QListWidget::itemEntered(QListWidgetItem *item)
This signal is emitted when the mouse cursor enters an item. The \a item is
the item entered. This signal is only emitted when mouseTracking is turned
on, or when a mouse button is pressed while moving into an item.
\sa QWidget::setMouseTracking()
*/
/*!
\fn void QListWidget::itemChanged(QListWidgetItem *item)
This signal is emitted whenever the data of \a item has changed.
*/
/*!
\fn void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
This signal is emitted whenever the current item changes.
\a previous is the item that previously had the focus; \a current is the
new current item.
*/
/*!
\fn void QListWidget::currentTextChanged(const QString ¤tText)
This signal is emitted whenever the current item changes.
\a currentText is the text data in the current item. If there is no current
item, the \a currentText is invalid.
*/
/*!
\fn void QListWidget::currentRowChanged(int currentRow)
This signal is emitted whenever the current item changes.
\a currentRow is the row of the current item. If there is no current item,
the \a currentRow is -1.
*/
/*!
\fn void QListWidget::itemSelectionChanged()
This signal is emitted whenever the selection changes.
\sa selectedItems(), QListWidgetItem::isSelected(), currentItemChanged()
*/
/*!
\since 4.3
\fn void QListWidget::removeItemWidget(QListWidgetItem *item)
Removes the widget set on the given \a item.
To remove an item (row) from the list entirely, either delete the item or
use takeItem().
\sa itemWidget(), setItemWidget()
*/
/*!
Constructs an empty QListWidget with the given \a parent.
*/
QListWidget::QListWidget(QWidget *parent)
: QListView(*new QListWidgetPrivate(), parent)
{
Q_D(QListWidget);
d->setup();
}
/*!
Destroys the list widget and all its items.
*/
QListWidget::~QListWidget()
{
}
/*!
\reimp
*/
void QListWidget::setSelectionModel(QItemSelectionModel *selectionModel)
{
Q_D(QListWidget);
if (d->selectionModel) {
QObject::disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(itemSelectionChanged()));
}
QListView::setSelectionModel(selectionModel);
if (d->selectionModel) {
QObject::connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(itemSelectionChanged()));
}
}
/*!
Returns the item that occupies the given \a row in the list if one has been
set; otherwise returns \nullptr.
\sa row()
*/
QListWidgetItem *QListWidget::item(int row) const
{
Q_D(const QListWidget);
if (row < 0 || row >= d->model->rowCount())
return 0;
return d->listModel()->at(row);
}
/*!
Returns the row containing the given \a item.
\sa item()
*/
int QListWidget::row(const QListWidgetItem *item) const
{
Q_D(const QListWidget);
return d->listModel()->index(const_cast<QListWidgetItem*>(item)).row();
}
/*!
Inserts the \a item at the position in the list given by \a row.
\sa addItem()
*/
void QListWidget::insertItem(int row, QListWidgetItem *item)
{
Q_D(QListWidget);
if (item && !item->view)
d->listModel()->insert(row, item);
}
/*!
Inserts an item with the text \a label in the list widget at the position
given by \a row.
\sa addItem()
*/
void QListWidget::insertItem(int row, const QString &label)
{
Q_D(QListWidget);
d->listModel()->insert(row, new QListWidgetItem(label));
}
/*!
Inserts items from the list of \a labels into the list, starting at the
given \a row.
\sa insertItem(), addItem()
*/
void QListWidget::insertItems(int row, const QStringList &labels)
{
Q_D(QListWidget);
d->listModel()->insert(row, labels);
}
/*!
Removes and returns the item from the given \a row in the list widget;
otherwise returns \nullptr.
Items removed from a list widget will not be managed by Qt, and will need
to be deleted manually.
\sa insertItem(), addItem()
*/
QListWidgetItem *QListWidget::takeItem(int row)
{
Q_D(QListWidget);
if (row < 0 || row >= d->model->rowCount())
return 0;
return d->listModel()->take(row);
}
/*!
\property QListWidget::count
\brief the number of items in the list including any hidden items.
*/
int QListWidget::count() const
{
Q_D(const QListWidget);
return d->model->rowCount();
}
/*!
Returns the current item.
*/
QListWidgetItem *QListWidget::currentItem() const
{
Q_D(const QListWidget);
return d->listModel()->at(currentIndex().row());
}
/*!
Sets the current item to \a item.
Unless the selection mode is \l{QAbstractItemView::}{NoSelection},
the item is also selected.
*/
void QListWidget::setCurrentItem(QListWidgetItem *item)
{
setCurrentRow(row(item));
}
/*!
\since 4.4
Set the current item to \a item, using the given \a command.
*/
void QListWidget::setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)
{
setCurrentRow(row(item), command);
}
/*!
\property QListWidget::currentRow
\brief the row of the current item.
Depending on the current selection mode, the row may also be selected.
*/
int QListWidget::currentRow() const
{
return currentIndex().row();
}
void QListWidget::setCurrentRow(int row)
{
Q_D(QListWidget);
QModelIndex index = d->listModel()->index(row);
if (d->selectionMode == SingleSelection)
selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
else if (d->selectionMode == NoSelection)
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
else
selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
}
/*!
\since 4.4
Sets the current row to be the given \a row, using the given \a command,
*/
void QListWidget::setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
{
Q_D(QListWidget);
d->selectionModel->setCurrentIndex(d->listModel()->index(row), command);
}
/*!
Returns a pointer to the item at the coordinates \a p. The coordinates
are relative to the list widget's \l{QAbstractScrollArea::}{viewport()}.
*/
QListWidgetItem *QListWidget::itemAt(const QPoint &p) const
{
Q_D(const QListWidget);
return d->listModel()->at(indexAt(p).row());
}
/*!
\fn QListWidgetItem *QListWidget::itemAt(int x, int y) const
\overload
Returns a pointer to the item at the coordinates (\a x, \a y).
The coordinates are relative to the list widget's
\l{QAbstractScrollArea::}{viewport()}.
*/
/*!
Returns the rectangle on the viewport occupied by the item at \a item.
*/
QRect QListWidget::visualItemRect(const QListWidgetItem *item) const
{
Q_D(const QListWidget);
QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
return visualRect(index);
}
/*!
Sorts all the items in the list widget according to the specified \a order.
*/
void QListWidget::sortItems(Qt::SortOrder order)
{
Q_D(QListWidget);
d->sortOrder = order;
d->listModel()->sort(0, order);
}
/*!
\since 4.2
\property QListWidget::sortingEnabled
\brief whether sorting is enabled
If this property is \c true, sorting is enabled for the list; if the property
is false, sorting is not enabled.
The default value is false.
*/
void QListWidget::setSortingEnabled(bool enable)
{
Q_D(QListWidget);
d->sortingEnabled = enable;
}
bool QListWidget::isSortingEnabled() const
{
Q_D(const QListWidget);
return d->sortingEnabled;
}
/*!
\internal
*/
Qt::SortOrder QListWidget::sortOrder() const
{
Q_D(const QListWidget);
return d->sortOrder;
}
/*!
Starts editing the \a item if it is editable.
*/
void QListWidget::editItem(QListWidgetItem *item)
{
Q_D(QListWidget);
edit(d->listModel()->index(item));
}
/*!
Opens an editor for the given \a item. The editor remains open after
editing.
\sa closePersistentEditor(), isPersistentEditorOpen()
*/
void QListWidget::openPersistentEditor(QListWidgetItem *item)
{
Q_D(QListWidget);
QModelIndex index = d->listModel()->index(item);
QAbstractItemView::openPersistentEditor(index);
}
/*!
Closes the persistent editor for the given \a item.
\sa openPersistentEditor(), isPersistentEditorOpen()
*/
void QListWidget::closePersistentEditor(QListWidgetItem *item)
{
Q_D(QListWidget);
QModelIndex index = d->listModel()->index(item);
QAbstractItemView::closePersistentEditor(index);
}
/*!
\since 5.10
Returns whether a persistent editor is open for item \a item.
\sa openPersistentEditor(), closePersistentEditor()
*/
bool QListWidget::isPersistentEditorOpen(QListWidgetItem *item) const
{
Q_D(const QListWidget);
const QModelIndex index = d->listModel()->index(item);
return QAbstractItemView::isPersistentEditorOpen(index);
}
/*!
\since 4.1
Returns the widget displayed in the given \a item.
\sa setItemWidget(), removeItemWidget()
*/
QWidget *QListWidget::itemWidget(QListWidgetItem *item) const
{
Q_D(const QListWidget);
QModelIndex index = d->listModel()->index(item);
return QAbstractItemView::indexWidget(index);
}
/*!
\since 4.1
Sets the \a widget to be displayed in the given \a item.
This function should only be used to display static content in the place of
a list widget item. If you want to display custom dynamic content or
implement a custom editor widget, use QListView and subclass QStyledItemDelegate
instead.
\sa itemWidget(), removeItemWidget(), {Delegate Classes}
*/
void QListWidget::setItemWidget(QListWidgetItem *item, QWidget *widget)
{
Q_D(QListWidget);
QModelIndex index = d->listModel()->index(item);
QAbstractItemView::setIndexWidget(index, widget);
}
#if QT_DEPRECATED_SINCE(5, 13)
/*!
Returns \c true if \a item is selected; otherwise returns \c false.
\obsolete
This function is deprecated. Use QListWidgetItem::isSelected() instead.
*/
bool QListWidget::isItemSelected(const QListWidgetItem *item) const
{
return ((item && item->listWidget() == this) ? item->isSelected() : false);
}
/*!
Selects or deselects the given \a item depending on whether \a select is
true of false.
\obsolete
This function is deprecated. Use QListWidgetItem::setSelected() instead.
*/
void QListWidget::setItemSelected(const QListWidgetItem *item, bool select)
{
if (item && item->listWidget() == this)
const_cast<QListWidgetItem*>(item)->setSelected(select);
}
#endif
/*!
Returns a list of all selected items in the list widget.
*/
QList<QListWidgetItem*> QListWidget::selectedItems() const
{
Q_D(const QListWidget);
QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QListWidgetItem*> items;
const int numIndexes = indexes.count();
items.reserve(numIndexes);
for (int i = 0; i < numIndexes; ++i)
items.append(d->listModel()->at(indexes.at(i).row()));
return items;
}
/*!
Finds items with the text that matches the string \a text using the given
\a flags.
*/
QList<QListWidgetItem*> QListWidget::findItems(const QString &text, Qt::MatchFlags flags) const
{
Q_D(const QListWidget);
QModelIndexList indexes = d->listModel()->match(model()->index(0, 0, QModelIndex()),
Qt::DisplayRole, text, -1, flags);
QList<QListWidgetItem*> items;
const int indexesSize = indexes.size();
items.reserve(indexesSize);
for (int i = 0; i < indexesSize; ++i)
items.append(d->listModel()->at(indexes.at(i).row()));
return items;
}
#if QT_DEPRECATED_SINCE(5, 13)
/*!
Returns \c true if the \a item is explicitly hidden; otherwise returns \c false.
\obsolete
This function is deprecated. Use QListWidgetItem::isHidden() instead.
*/
bool QListWidget::isItemHidden(const QListWidgetItem *item) const
{
return isRowHidden(row(item));
}
/*!
If \a hide is true, the \a item will be hidden; otherwise it will be shown.
\obsolete
This function is deprecated. Use QListWidgetItem::setHidden() instead.
*/
void QListWidget::setItemHidden(const QListWidgetItem *item, bool hide)
{
setRowHidden(row(item), hide);
}
#endif
/*!
Scrolls the view if necessary to ensure that the \a item is visible.
\a hint specifies where the \a item should be located after the operation.
*/
void QListWidget::scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint)
{
Q_D(QListWidget);
QModelIndex index = d->listModel()->index(const_cast<QListWidgetItem*>(item));
QListView::scrollTo(index, hint);
}
/*!
Removes all items and selections in the view.
\warning All items will be permanently deleted.
*/
void QListWidget::clear()
{
Q_D(QListWidget);
selectionModel()->clear();
d->listModel()->clear();
}
/*!
Returns a list of MIME types that can be used to describe a list of
listwidget items.
\sa mimeData()
*/
QStringList QListWidget::mimeTypes() const
{
return d_func()->listModel()->QAbstractListModel::mimeTypes();
}
/*!
Returns an object that contains a serialized description of the specified
\a items. The format used to describe the items is obtained from the
mimeTypes() function.
If the list of items is empty, \nullptr is returned instead of a
serialized empty list.
*/
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QMimeData *QListWidget::mimeData(const QList<QListWidgetItem *> &items) const
#else
QMimeData *QListWidget::mimeData(const QList<QListWidgetItem*> items) const
#endif
{
Q_D(const QListWidget);
QModelIndexList &cachedIndexes = d->listModel()->cachedIndexes;
// if non empty, it's called from the model's own mimeData
if (cachedIndexes.isEmpty()) {
cachedIndexes.reserve(items.count());
for (QListWidgetItem *item : items)
cachedIndexes << indexFromItem(item);
QMimeData *result = d->listModel()->internalMimeData();
cachedIndexes.clear();
return result;
}
return d->listModel()->internalMimeData();
}
#if QT_CONFIG(draganddrop)
/*!
Handles \a data supplied by an external drag and drop operation that ended
with the given \a action in the given \a index. Returns \c true if \a data and
\a action can be handled by the model; otherwise returns \c false.
\sa supportedDropActions()
*/
bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
{
QModelIndex idx;
int row = index;
int column = 0;
if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
// QAbstractListModel::dropMimeData will overwrite on the index if row == -1 and column == -1
idx = model()->index(row, column);
row = -1;
column = -1;
}
return d_func()->listModel()->QAbstractListModel::dropMimeData(data, action , row, column, idx);
}
/*! \reimp */
void QListWidget::dropEvent(QDropEvent *event) {
Q_D(QListWidget);
if (event->source() == this && d->movement != Static) {
QListView::dropEvent(event);
return;
}
if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
dragDropMode() == QAbstractItemView::InternalMove)) {
QModelIndex topIndex;
int col = -1;
int row = -1;
if (d->dropOn(event, &row, &col, &topIndex)) {
QList<QModelIndex> selIndexes = selectedIndexes();
QList<QPersistentModelIndex> persIndexes;
const int selIndexesCount = selIndexes.count();
persIndexes.reserve(selIndexesCount);
for (int i = 0; i < selIndexesCount; i++)
persIndexes.append(selIndexes.at(i));
if (persIndexes.contains(topIndex))
return;
std::sort(persIndexes.begin(), persIndexes.end()); // The dropped items will remain in the same visual order.
QPersistentModelIndex dropRow = model()->index(row, col, topIndex);
int r = row == -1 ? count() : (dropRow.row() >= 0 ? dropRow.row() : row);
for (int i = 0; i < persIndexes.count(); ++i) {
const QPersistentModelIndex &pIndex = persIndexes.at(i);
d->listModel()->move(pIndex.row(), r);
r = pIndex.row() + 1; // Dropped items are inserted contiguously and in the right order.
}
event->accept();
// Don't want QAbstractItemView to delete it because it was "moved" we already did it
event->setDropAction(Qt::CopyAction);
}
}
QListView::dropEvent(event);
}
/*!
Returns the drop actions supported by this view.
\sa Qt::DropActions
*/
Qt::DropActions QListWidget::supportedDropActions() const
{
Q_D(const QListWidget);
return d->listModel()->QAbstractListModel::supportedDropActions() | Qt::MoveAction;
}
#endif // QT_CONFIG(draganddrop)
/*!
Returns a list of pointers to the items contained in the \a data object. If
the object was not created by a QListWidget in the same process, the list
is empty.
*/
QList<QListWidgetItem*> QListWidget::items(const QMimeData *data) const
{
const QListWidgetMimeData *lwd = qobject_cast<const QListWidgetMimeData*>(data);
if (lwd)
return lwd->items;
return QList<QListWidgetItem*>();
}
/*!
Returns the QModelIndex associated with the given \a item.
\note In Qt versions prior to 5.10, this function took a non-\c{const} \a item.
*/
QModelIndex QListWidget::indexFromItem(const QListWidgetItem *item) const
{
Q_D(const QListWidget);
return d->listModel()->index(item);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
/*!
\internal
\obsolete
\overload
*/
QModelIndex QListWidget::indexFromItem(QListWidgetItem *item) const
{
return indexFromItem(const_cast<const QListWidgetItem *>(item));
}
#endif
/*!
Returns a pointer to the QListWidgetItem associated with the given \a index.
*/
QListWidgetItem *QListWidget::itemFromIndex(const QModelIndex &index) const
{
Q_D(const QListWidget);
if (d->isIndexValid(index))
return d->listModel()->at(index.row());
return 0;
}
/*!
\internal
*/
void QListWidget::setModel(QAbstractItemModel * /*model*/)
{
Q_ASSERT(!"QListWidget::setModel() - Changing the model of the QListWidget is not allowed.");
}
/*!
\reimp
*/
bool QListWidget::event(QEvent *e)
{
return QListView::event(e);
}
QT_END_NAMESPACE
#include "moc_qlistwidget.cpp"
#include "moc_qlistwidget_p.cpp"
|