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
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtablewidget.h"
#ifndef QT_NO_TABLEWIDGET
#include <qitemdelegate.h>
#include <qpainter.h>
#include <private/qtablewidget_p.h>
QT_BEGIN_NAMESPACE
QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
: QAbstractTableModel(parent),
prototype(0),
tableItems(rows * columns, 0),
verticalHeaderItems(rows, 0),
horizontalHeaderItems(columns, 0)
{}
QTableModel::~QTableModel()
{
clear();
delete prototype;
}
bool QTableModel::insertRows(int row, int count, const QModelIndex &)
{
if (count < 1 || row < 0 || row > verticalHeaderItems.count())
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
int rc = verticalHeaderItems.count();
int cc = horizontalHeaderItems.count();
verticalHeaderItems.insert(row, count, 0);
if (rc == 0)
tableItems.resize(cc * count);
else
tableItems.insert(tableIndex(row, 0), cc * count, 0);
endInsertRows();
return true;
}
bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
{
if (count < 1 || column < 0 || column > horizontalHeaderItems.count())
return false;
beginInsertColumns(QModelIndex(), column, column + count - 1);
int rc = verticalHeaderItems.count();
int cc = horizontalHeaderItems.count();
horizontalHeaderItems.insert(column, count, 0);
if (cc == 0)
tableItems.resize(rc * count);
else
for (int row = 0; row < rc; ++row)
tableItems.insert(tableIndex(row, column), count, 0);
endInsertColumns();
return true;
}
bool QTableModel::removeRows(int row, int count, const QModelIndex &)
{
if (count < 1 || row < 0 || row + count > verticalHeaderItems.count())
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
int i = tableIndex(row, 0);
int n = count * columnCount();
QTableWidgetItem *oldItem = 0;
for (int j = i; j < n + i; ++j) {
oldItem = tableItems.at(j);
if (oldItem)
oldItem->view = 0;
delete oldItem;
}
tableItems.remove(qMax(i, 0), n);
for (int v = row; v < row + count; ++v) {
oldItem = verticalHeaderItems.at(v);
if (oldItem)
oldItem->view = 0;
delete oldItem;
}
verticalHeaderItems.remove(row, count);
endRemoveRows();
return true;
}
bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
{
if (count < 1 || column < 0 || column + count > horizontalHeaderItems.count())
return false;
beginRemoveColumns(QModelIndex(), column, column + count - 1);
QTableWidgetItem *oldItem = 0;
for (int row = rowCount() - 1; row >= 0; --row) {
int i = tableIndex(row, column);
for (int j = i; j < i + count; ++j) {
oldItem = tableItems.at(j);
if (oldItem)
oldItem->view = 0;
delete oldItem;
}
tableItems.remove(i, count);
}
for (int h=column; h<column+count; ++h) {
oldItem = horizontalHeaderItems.at(h);
if (oldItem)
oldItem->view = 0;
delete oldItem;
}
horizontalHeaderItems.remove(column, count);
endRemoveColumns();
return true;
}
void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
{
int i = tableIndex(row, column);
if (i < 0 || i >= tableItems.count())
return;
QTableWidgetItem *oldItem = tableItems.at(i);
if (item == oldItem)
return;
// remove old
if (oldItem)
oldItem->view = 0;
delete tableItems.at(i);
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
// set new
if (item)
item->d->id = i;
tableItems[i] = item;
if (view && view->isSortingEnabled()
&& view->horizontalHeader()->sortIndicatorSection() == column) {
// sorted insertion
Qt::SortOrder order = view->horizontalHeader()->sortIndicatorOrder();
QVector<QTableWidgetItem*> colItems = columnItems(column);
if (row < colItems.count())
colItems.remove(row);
int sortedRow;
if (item == 0) {
// move to after all non-0 (sortable) items
sortedRow = colItems.count();
} else {
QVector<QTableWidgetItem*>::iterator it;
it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
sortedRow = qMax((int)(it - colItems.begin()), 0);
}
if (sortedRow != row) {
emit layoutAboutToBeChanged();
// move the items @ row to sortedRow
int cc = columnCount();
QVector<QTableWidgetItem*> rowItems(cc);
for (int j = 0; j < cc; ++j)
rowItems[j] = tableItems.at(tableIndex(row, j));
tableItems.remove(tableIndex(row, 0), cc);
tableItems.insert(tableIndex(sortedRow, 0), cc, 0);
for (int j = 0; j < cc; ++j)
tableItems[tableIndex(sortedRow, j)] = rowItems.at(j);
QTableWidgetItem *header = verticalHeaderItems.at(row);
verticalHeaderItems.remove(row);
verticalHeaderItems.insert(sortedRow, header);
// update persistent indexes
QModelIndexList oldPersistentIndexes = persistentIndexList();
QModelIndexList newPersistentIndexes = oldPersistentIndexes;
updateRowIndexes(newPersistentIndexes, row, sortedRow);
changePersistentIndexList(oldPersistentIndexes,
newPersistentIndexes);
emit layoutChanged();
return;
}
}
QModelIndex idx = QAbstractTableModel::index(row, column);
emit dataChanged(idx, idx);
}
QTableWidgetItem *QTableModel::takeItem(int row, int column)
{
long i = tableIndex(row, column);
QTableWidgetItem *itm = tableItems.value(i);
if (itm) {
itm->view = 0;
itm->d->id = -1;
tableItems[i] = 0;
}
return itm;
}
QTableWidgetItem *QTableModel::item(int row, int column) const
{
return tableItems.value(tableIndex(row, column));
}
QTableWidgetItem *QTableModel::item(const QModelIndex &index) const
{
if (!isValid(index))
return 0;
return tableItems.at(tableIndex(index.row(), index.column()));
}
void QTableModel::removeItem(QTableWidgetItem *item)
{
int i = tableItems.indexOf(item);
if (i != -1) {
tableItems[i] = 0;
QModelIndex idx = index(item);
emit dataChanged(idx, idx);
return;
}
i = verticalHeaderItems.indexOf(item);
if (i != -1) {
verticalHeaderItems[i] = 0;
emit headerDataChanged(Qt::Vertical, i, i);
return;
}
i = horizontalHeaderItems.indexOf(item);
if (i != -1) {
horizontalHeaderItems[i] = 0;
emit headerDataChanged(Qt::Horizontal, i, i);
return;
}
}
void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item)
{
if (section < 0 || section >= horizontalHeaderItems.count())
return;
QTableWidgetItem *oldItem = horizontalHeaderItems.at(section);
if (item == oldItem)
return;
if (oldItem)
oldItem->view = 0;
delete oldItem;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
if (item) {
item->view = view;
item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
}
horizontalHeaderItems[section] = item;
emit headerDataChanged(Qt::Horizontal, section, section);
}
void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item)
{
if (section < 0 || section >= verticalHeaderItems.count())
return;
QTableWidgetItem *oldItem = verticalHeaderItems.at(section);
if (item == oldItem)
return;
if (oldItem)
oldItem->view = 0;
delete oldItem;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
if (item) {
item->view = view;
item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem);
}
verticalHeaderItems[section] = item;
emit headerDataChanged(Qt::Vertical, section, section);
}
QTableWidgetItem *QTableModel::takeHorizontalHeaderItem(int section)
{
if (section < 0 || section >= horizontalHeaderItems.count())
return 0;
QTableWidgetItem *itm = horizontalHeaderItems.at(section);
if (itm) {
itm->view = 0;
itm->itemFlags &= ~ItemIsHeaderItem;
horizontalHeaderItems[section] = 0;
}
return itm;
}
QTableWidgetItem *QTableModel::takeVerticalHeaderItem(int section)
{
if (section < 0 || section >= verticalHeaderItems.count())
return 0;
QTableWidgetItem *itm = verticalHeaderItems.at(section);
if (itm) {
itm->view = 0;
itm->itemFlags &= ~ItemIsHeaderItem;
verticalHeaderItems[section] = 0;
}
return itm;
}
QTableWidgetItem *QTableModel::horizontalHeaderItem(int section)
{
return horizontalHeaderItems.value(section);
}
QTableWidgetItem *QTableModel::verticalHeaderItem(int section)
{
return verticalHeaderItems.value(section);
}
QModelIndex QTableModel::index(const QTableWidgetItem *item) const
{
if (!item)
return QModelIndex();
int i = -1;
const int id = item->d->id;
if (id >= 0 && id < tableItems.count() && tableItems.at(id) == item) {
i = id;
} else { // we need to search for the item
i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item));
if (i == -1) // not found
return QModelIndex();
}
int row = i / columnCount();
int col = i % columnCount();
return QAbstractTableModel::index(row, col);
}
void QTableModel::setRowCount(int rows)
{
int rc = verticalHeaderItems.count();
if (rows < 0 || rc == rows)
return;
if (rc < rows)
insertRows(qMax(rc, 0), rows - rc);
else
removeRows(qMax(rows, 0), rc - rows);
}
void QTableModel::setColumnCount(int columns)
{
int cc = horizontalHeaderItems.count();
if (columns < 0 || cc == columns)
return;
if (cc < columns)
insertColumns(qMax(cc, 0), columns - cc);
else
removeColumns(qMax(columns, 0), cc - columns);
}
int QTableModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : verticalHeaderItems.count();
}
int QTableModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : horizontalHeaderItems.count();
}
QVariant QTableModel::data(const QModelIndex &index, int role) const
{
QTableWidgetItem *itm = item(index);
if (itm)
return itm->data(role);
return QVariant();
}
bool QTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
QTableWidgetItem *itm = item(index);
if (itm) {
itm->setData(role, value);
return true;
}
// don't create dummy table items for empty values
if (!value.isValid())
return false;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
if (!view)
return false;
itm = createItem();
itm->setData(role, value);
view->setItem(index.row(), index.column(), itm);
return true;
}
QMap<int, QVariant> QTableModel::itemData(const QModelIndex &index) const
{
QMap<int, QVariant> roles;
QTableWidgetItem *itm = item(index);
if (itm) {
for (int i = 0; i < itm->values.count(); ++i) {
roles.insert(itm->values.at(i).role,
itm->values.at(i).value);
}
}
return roles;
}
// reimplemented to ensure that only one dataChanged() signal is emitted
bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
{
if (!index.isValid())
return false;
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
QTableWidgetItem *itm = item(index);
if (itm) {
itm->view = 0; // prohibits item from calling itemChanged()
bool changed = false;
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
if (itm->data(it.key()) != it.value()) {
itm->setData(it.key(), it.value());
changed = true;
}
}
itm->view = view;
if (changed)
itemChanged(itm);
return true;
}
if (!view)
return false;
itm = createItem();
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it)
itm->setData(it.key(), it.value());
view->setItem(index.row(), index.column(), itm);
return true;
}
Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsDropEnabled;
if (QTableWidgetItem *itm = item(index))
return itm->flags();
return (Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled);
}
void QTableModel::sort(int column, Qt::SortOrder order)
{
QVector<QPair<QTableWidgetItem*, int> > sortable;
QVector<int> unsortable;
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
for (int row = 0; row < rowCount(); ++row) {
if (QTableWidgetItem *itm = item(row, column))
sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
else
unsortable.append(row);
}
LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
qStableSort(sortable.begin(), sortable.end(), compare);
QVector<QTableWidgetItem*> sorted_table(tableItems.count());
QModelIndexList from;
QModelIndexList to;
for (int i = 0; i < rowCount(); ++i) {
int r = (i < sortable.count()
? sortable.at(i).second
: unsortable.at(i - sortable.count()));
for (int c = 0; c < columnCount(); ++c) {
sorted_table[tableIndex(i, c)] = item(r, c);
from.append(createIndex(r, c, 0));
to.append(createIndex(i, c, 0));
}
}
emit layoutAboutToBeChanged();
tableItems = sorted_table;
changePersistentIndexList(from, to); // ### slow
emit layoutChanged();
}
/*
\internal
Ensures that rows in the interval [start, end] are
sorted according to the contents of column \a column
and the given sort \a order.
*/
void QTableModel::ensureSorted(int column, Qt::SortOrder order,
int start, int end)
{
int count = end - start + 1;
QVector < QPair<QTableWidgetItem*,int> > sorting;
sorting.reserve(count);
for (int row = start; row <= end; ++row) {
QTableWidgetItem *itm = item(row, column);
if (itm == 0) {
// no more sortable items (all 0-items are
// at the end of the table when it is sorted)
break;
}
sorting.append(QPair<QTableWidgetItem*,int>(itm, row));
}
LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
qStableSort(sorting.begin(), sorting.end(), compare);
QModelIndexList oldPersistentIndexes = persistentIndexList();
QModelIndexList newPersistentIndexes = oldPersistentIndexes;
QVector<QTableWidgetItem*> newTable = tableItems;
QVector<QTableWidgetItem*> newVertical = verticalHeaderItems;
QVector<QTableWidgetItem*> colItems = columnItems(column);
QVector<QTableWidgetItem*>::iterator vit = colItems.begin();
bool changed = false;
for (int i = 0; i < sorting.count(); ++i) {
int oldRow = sorting.at(i).second;
QTableWidgetItem *item = colItems.at(oldRow);
colItems.remove(oldRow);
vit = sortedInsertionIterator(vit, colItems.end(), order, item);
int newRow = qMax((int)(vit - colItems.begin()), 0);
vit = colItems.insert(vit, item);
if (newRow != oldRow) {
changed = true;
// move the items @ oldRow to newRow
int cc = columnCount();
QVector<QTableWidgetItem*> rowItems(cc);
for (int j = 0; j < cc; ++j)
rowItems[j] = newTable.at(tableIndex(oldRow, j));
newTable.remove(tableIndex(oldRow, 0), cc);
newTable.insert(tableIndex(newRow, 0), cc, 0);
for (int j = 0; j < cc; ++j)
newTable[tableIndex(newRow, j)] = rowItems.at(j);
QTableWidgetItem *header = newVertical.at(oldRow);
newVertical.remove(oldRow);
newVertical.insert(newRow, header);
// update persistent indexes
updateRowIndexes(newPersistentIndexes, oldRow, newRow);
// the index of the remaining rows may have changed
for (int j = i + 1; j < sorting.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;
}
}
}
if (changed) {
emit layoutAboutToBeChanged();
tableItems = newTable;
verticalHeaderItems = newVertical;
changePersistentIndexList(oldPersistentIndexes,
newPersistentIndexes);
emit layoutChanged();
}
}
/*
\internal
Returns the non-0 items in column \a column.
*/
QVector<QTableWidgetItem*> QTableModel::columnItems(int column) const
{
QVector<QTableWidgetItem*> items;
int rc = rowCount();
items.reserve(rc);
for (int row = 0; row < rc; ++row) {
QTableWidgetItem *itm = item(row, column);
if (itm == 0) {
// no more sortable items (all 0-items are
// at the end of the table when it is sorted)
break;
}
items.append(itm);
}
return items;
}
/*
\internal
Adjusts the row of each index in \a indexes if necessary, given
that a row of items has been moved from row \a movedFrom to row
\a movedTo.
*/
void QTableModel::updateRowIndexes(QModelIndexList &indexes,
int movedFromRow, int movedToRow)
{
QModelIndexList::iterator it;
for (it = indexes.begin(); it != indexes.end(); ++it) {
int oldRow = (*it).row();
int newRow = oldRow;
if (oldRow == movedFromRow)
newRow = movedToRow;
else if (movedFromRow < oldRow && movedToRow >= oldRow)
newRow = oldRow - 1;
else if (movedFromRow > oldRow && movedToRow <= oldRow)
newRow = oldRow + 1;
if (newRow != oldRow)
*it = index(newRow, (*it).column(), (*it).parent());
}
}
/*
\internal
Returns an iterator to the item where \a item should be
inserted in the interval (\a begin, \a end) according to
the given sort \a order.
*/
QVector<QTableWidgetItem*>::iterator QTableModel::sortedInsertionIterator(
const QVector<QTableWidgetItem*>::iterator &begin,
const QVector<QTableWidgetItem*>::iterator &end,
Qt::SortOrder order, QTableWidgetItem *item)
{
if (order == Qt::AscendingOrder)
return qLowerBound(begin, end, item, QTableModelLessThan());
return qLowerBound(begin, end, item, QTableModelGreaterThan());
}
bool QTableModel::itemLessThan(const QPair<QTableWidgetItem*,int> &left,
const QPair<QTableWidgetItem*,int> &right)
{
return *(left.first) < *(right.first);
}
bool QTableModel::itemGreaterThan(const QPair<QTableWidgetItem*,int> &left,
const QPair<QTableWidgetItem*,int> &right)
{
return (*(right.first) < *(left .first));
}
QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (section < 0)
return QVariant();
QTableWidgetItem *itm = 0;
if (orientation == Qt::Horizontal && section < horizontalHeaderItems.count())
itm = horizontalHeaderItems.at(section);
else if (orientation == Qt::Vertical && section < verticalHeaderItems.count())
itm = verticalHeaderItems.at(section);
else
return QVariant(); // section is out of bounds
if (itm)
return itm->data(role);
if (role == Qt::DisplayRole)
return section + 1;
return QVariant();
}
bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
const QVariant &value, int role)
{
if (section < 0 ||
(orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) ||
(orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
return false;
QTableWidgetItem *itm = 0;
if (orientation == Qt::Horizontal)
itm = horizontalHeaderItems.at(section);
else
itm = verticalHeaderItems.at(section);
if (itm) {
itm->setData(role, value);
return true;
}
return false;
}
bool QTableModel::isValid(const QModelIndex &index) const
{
return (index.isValid()
&& index.row() < verticalHeaderItems.count()
&& index.column() < horizontalHeaderItems.count());
}
void QTableModel::clear()
{
for (int j = 0; j < verticalHeaderItems.count(); ++j) {
if (verticalHeaderItems.at(j)) {
verticalHeaderItems.at(j)->view = 0;
delete verticalHeaderItems.at(j);
verticalHeaderItems[j] = 0;
}
}
for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
if (horizontalHeaderItems.at(k)) {
horizontalHeaderItems.at(k)->view = 0;
delete horizontalHeaderItems.at(k);
horizontalHeaderItems[k] = 0;
}
}
clearContents();
}
void QTableModel::clearContents()
{
for (int i = 0; i < tableItems.count(); ++i) {
if (tableItems.at(i)) {
tableItems.at(i)->view = 0;
delete tableItems.at(i);
tableItems[i] = 0;
}
}
reset();
}
void QTableModel::itemChanged(QTableWidgetItem *item)
{
if (!item)
return;
if (item->flags() & ItemIsHeaderItem) {
int row = verticalHeaderItems.indexOf(item);
if (row >= 0) {
emit headerDataChanged(Qt::Vertical, row, row);
} else {
int column = horizontalHeaderItems.indexOf(item);
if (column >= 0)
emit headerDataChanged(Qt::Horizontal, column, column);
}
} else {
QModelIndex idx = index(item);
if (idx.isValid())
emit dataChanged(idx, idx);
}
}
QTableWidgetItem* QTableModel::createItem() const
{
return prototype ? prototype->clone() : new QTableWidgetItem;
}
const QTableWidgetItem *QTableModel::itemPrototype() const
{
return prototype;
}
void QTableModel::setItemPrototype(const QTableWidgetItem *item)
{
if (prototype != item) {
delete prototype;
prototype = item;
}
}
QStringList QTableModel::mimeTypes() const
{
const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
return (view ? view->mimeTypes() : QStringList());
}
QMimeData *QTableModel::internalMimeData() const
{
return QAbstractTableModel::mimeData(cachedIndexes);
}
QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const
{
QList<QTableWidgetItem*> items;
for (int i = 0; i < indexes.count(); ++i)
items << item(indexes.at(i));
const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
// cachedIndexes is a little hack to avoid copying from QModelIndexList to
// QList<QTreeWidgetItem*> and back again in the view
cachedIndexes = indexes;
QMimeData *mimeData = (view ? view->mimeData(items) : 0);
cachedIndexes.clear();
return mimeData;
}
bool QTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row , int column, const QModelIndex &index)
{
if (index.isValid()) {
row = index.row();
column = index.column();
}else if (row == -1 || column == -1) { // The user dropped outside the table.
row = rowCount();
column = 0;
}
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
return (view ? view->dropMimeData(row, column, data, action) : false);
}
Qt::DropActions QTableModel::supportedDropActions() const
{
const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
}
/*!
\class QTableWidgetSelectionRange
\brief The QTableWidgetSelectionRange class provides a way to interact with
selection in a model without using model indexes and a selection model.
\ingroup model-view
The QTableWidgetSelectionRange class stores the top left and bottom
right rows and columns of a selection range in a table. The
selections in the table may consist of several selection ranges.
\note If the item within the selection range is marked as not selectable,
e.g., \c{itemFlags() & Qt::ItemIsSelectable == 0} then it will not appear
in the selection range.
\sa QTableWidget
*/
/*!
Constructs an table selection range, i.e. a range
whose rowCount() and columnCount() are 0.
*/
QTableWidgetSelectionRange::QTableWidgetSelectionRange()
: top(-1), left(-1), bottom(-2), right(-2)
{
}
/*!
Constructs the table selection range from the given \a top, \a
left, \a bottom and \a right table rows and columns.
\sa topRow(), leftColumn(), bottomRow(), rightColumn()
*/
QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right)
: top(top), left(left), bottom(bottom), right(right)
{
}
/*!
Constructs a the table selection range by copying the given \a
other table selection range.
*/
QTableWidgetSelectionRange::QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other)
: top(other.top), left(other.left), bottom(other.bottom), right(other.right)
{
}
/*!
Destroys the table selection range.
*/
QTableWidgetSelectionRange::~QTableWidgetSelectionRange()
{
}
/*!
\fn int QTableWidgetSelectionRange::topRow() const
Returns the top row of the range.
\sa bottomRow(), leftColumn(), rowCount()
*/
/*!
\fn int QTableWidgetSelectionRange::bottomRow() const
Returns the bottom row of the range.
\sa topRow(), rightColumn(), rowCount()
*/
/*!
\fn int QTableWidgetSelectionRange::leftColumn() const
Returns the left column of the range.
\sa rightColumn(), topRow(), columnCount()
*/
/*!
\fn int QTableWidgetSelectionRange::rightColumn() const
Returns the right column of the range.
\sa leftColumn(), bottomRow(), columnCount()
*/
/*!
\since 4.1
\fn int QTableWidgetSelectionRange::rowCount() const
Returns the number of rows in the range.
This is equivalent to bottomRow() - topRow() + 1.
\sa columnCount(), topRow(), bottomRow()
*/
/*!
\since 4.1
\fn int QTableWidgetSelectionRange::columnCount() const
Returns the number of columns in the range.
This is equivalent to rightColumn() - leftColumn() + 1.
\sa rowCount(), leftColumn(), rightColumn()
*/
/*!
\class QTableWidgetItem
\brief The QTableWidgetItem class provides an item for use with the
QTableWidget class.
\ingroup model-view
Table items are used to hold pieces of information for table widgets.
Items usually contain text, icons, or checkboxes
The QTableWidgetItem class is a convenience class that replaces the
\c QTableItem class in Qt 3. It provides an item for use with
the QTableWidget class.
Top-level items are constructed without a parent then inserted at the
position specified by a pair of row and column numbers:
\snippet doc/src/snippets/qtablewidget-using/mainwindow.cpp 3
Each item can have its own background brush which is set with
the setBackground() function. The current background brush can be
found with background().
The text label for each item can be rendered with its own font and brush.
These are specified with the setFont() and setForeground() functions,
and read with font() and foreground().
By default, items are enabled, editable, selectable, checkable, and can be
used both as the source of a drag and drop operation and as a drop target.
Each item's flags can be changed by calling setFlags() with the appropriate
value (see \l{Qt::ItemFlags}). Checkable items can be checked and unchecked
with the setCheckState() function. The corresponding checkState() function
indicates whether the item is currently checked.
\section1 Subclassing
When subclassing QTableWidgetItem to provide custom items, it is possible to
define new types for them so that they can be distinguished from standard
items. The constructors for subclasses that require this feature need to
call the base class constructor with a new type value equal to or greater
than \l UserType.
\sa QTableWidget, {Model/View Programming}, QListWidgetItem, QTreeWidgetItem
*/
/*!
\fn int QTableWidgetItem::row() const
\since 4.2
Returns the row of the item in the table.
If the item is not in a table, this function will return -1.
\sa column()
*/
/*!
\fn int QTableWidgetItem::column() const
\since 4.2
Returns the column of the item in the table.
If the item is not in a table, this function will return -1.
\sa row()
*/
/*!
\fn void QTableWidgetItem::setSelected(bool select)
\since 4.2
Sets the selected state of the item to \a select.
\sa isSelected()
*/
/*!
\fn bool QTableWidgetItem::isSelected() const
\since 4.2
Returns true if the item is selected, otherwise returns false.
\sa setSelected()
*/
/*!
\fn QSize QTableWidgetItem::sizeHint() const
\since 4.1
Returns the size hint set for the table item.
*/
/*!
\fn void QTableWidgetItem::setSizeHint(const QSize &size)
\since 4.1
Sets the size hint for the table item to be \a size.
If no size hint is set, the item delegate will compute the
size hint based on the item data.
*/
/*!
\fn Qt::CheckState QTableWidgetItem::checkState() const
Returns the checked state of the table item.
\sa flags()
*/
/*!
\fn void QTableWidgetItem::setCheckState(Qt::CheckState state)
Sets the check state of the table item to be \a state.
*/
/*!
\fn QTableWidget *QTableWidgetItem::tableWidget() const
Returns the table widget that contains the item.
*/
/*!
\fn Qt::ItemFlags QTableWidgetItem::flags() const
Returns the flags used to describe the item. These determine whether
the item can be checked, edited, and selected.
\sa setFlags()
*/
/*!
\fn void QTableWidgetItem::setFlags(Qt::ItemFlags flags)
Sets the flags for the item to the given \a flags. These determine whether
the item can be selected or modified.
\sa flags()
*/
void QTableWidgetItem::setFlags(Qt::ItemFlags aflags)
{
itemFlags = aflags;
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
model->itemChanged(this);
}
/*!
\fn QString QTableWidgetItem::text() const
Returns the item's text.
\sa setText()
*/
/*!
\fn void QTableWidgetItem::setText(const QString &text)
Sets the item's text to the \a text specified.
\sa text() setFont() setForeground()
*/
/*!
\fn QIcon QTableWidgetItem::icon() const
Returns the item's icon.
\sa setIcon(), {QAbstractItemView::iconSize}{iconSize}
*/
/*!
\fn void QTableWidgetItem::setIcon(const QIcon &icon)
Sets the item's icon to the \a icon specified.
\sa icon(), setText(), {QAbstractItemView::iconSize}{iconSize}
*/
/*!
\fn QString QTableWidgetItem::statusTip() const
Returns the item's status tip.
\sa setStatusTip()
*/
/*!
\fn void QTableWidgetItem::setStatusTip(const QString &statusTip)
Sets the status tip for the table item to the text specified by
\a statusTip. QTableWidget mouse tracking needs to be enabled for this
feature to work.
\sa statusTip() setToolTip() setWhatsThis()
*/
/*!
\fn QString QTableWidgetItem::toolTip() const
Returns the item's tooltip.
\sa setToolTip()
*/
/*!
\fn void QTableWidgetItem::setToolTip(const QString &toolTip)
Sets the item's tooltip to the string specified by \a toolTip.
\sa toolTip() setStatusTip() setWhatsThis()
*/
/*!
\fn QString QTableWidgetItem::whatsThis() const
Returns the item's "What's This?" help.
\sa setWhatsThis()
*/
/*!
\fn void QTableWidgetItem::setWhatsThis(const QString &whatsThis)
Sets the item's "What's This?" help to the string specified by \a whatsThis.
\sa whatsThis() setStatusTip() setToolTip()
*/
/*!
\fn QFont QTableWidgetItem::font() const
Returns the font used to render the item's text.
\sa setFont()
*/
/*!
\fn void QTableWidgetItem::setFont(const QFont &font)
Sets the font used to display the item's text to the given \a font.
\sa font() setText() setForeground()
*/
/*!
\fn QColor QTableWidgetItem::backgroundColor() const
\obsolete
This function is deprecated. Use background() instead.
*/
/*!
\fn void QTableWidgetItem::setBackgroundColor(const QColor &color)
\obsolete
This function is deprecated. Use setBackground() instead.
*/
/*!
\fn QBrush QTableWidgetItem::background() const
\since 4.2
Returns the brush used to render the item's background.
\sa foreground()
*/
/*!
\fn void QTableWidgetItem::setBackground(const QBrush &brush)
\since 4.2
Sets the item's background brush to the specified \a brush.
\sa setForeground()
*/
/*!
\fn QColor QTableWidgetItem::textColor() const
\obsolete
This function is deprecated. Use foreground() instead.
*/
/*!
\fn void QTableWidgetItem::setTextColor(const QColor &color)
\obsolete
This function is deprecated. Use setForeground() instead.
*/
/*!
\fn QBrush QTableWidgetItem::foreground() const
\since 4.2
Returns the brush used to render the item's foreground (e.g. text).
\sa background()
*/
/*!
\fn void QTableWidgetItem::setForeground(const QBrush &brush)
\since 4.2
Sets the item's foreground brush to the specified \a brush.
\sa setBackground()
*/
/*!
\fn int QTableWidgetItem::textAlignment() const
Returns the text alignment for the item's text.
\sa Qt::Alignment
*/
/*!
\fn void QTableWidgetItem::setTextAlignment(int alignment)
Sets the text alignment for the item's text to the \a alignment
specified.
\sa Qt::Alignment
*/
/*!
Constructs a table item of the specified \a type that does not belong
to any table.
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
{
}
/*!
Constructs a table item with the given \a text.
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(const QString &text, int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
{
setData(Qt::DisplayRole, text);
}
/*!
Constructs a table item with the given \a icon and \a text.
\sa type()
*/
QTableWidgetItem::QTableWidgetItem(const QIcon &icon, const QString &text, int type)
: rtti(type), view(0), d(new QTableWidgetItemPrivate(this)),
itemFlags(Qt::ItemIsEditable
|Qt::ItemIsSelectable
|Qt::ItemIsUserCheckable
|Qt::ItemIsEnabled
|Qt::ItemIsDragEnabled
|Qt::ItemIsDropEnabled)
{
setData(Qt::DecorationRole, icon);
setData(Qt::DisplayRole, text);
}
/*!
Destroys the table item.
*/
QTableWidgetItem::~QTableWidgetItem()
{
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
model->removeItem(this);
view = 0;
delete d;
}
/*!
Creates a copy of the item.
*/
QTableWidgetItem *QTableWidgetItem::clone() const
{
return new QTableWidgetItem(*this);
}
/*!
Sets the item's data for the given \a role to the specified \a value.
\sa Qt::ItemDataRole, data()
*/
void QTableWidgetItem::setData(int role, const QVariant &value)
{
bool found = false;
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
for (int i = 0; i < values.count(); ++i) {
if (values.at(i).role == role) {
if (values[i].value == value)
return;
values[i].value = value;
found = true;
break;
}
}
if (!found)
values.append(QWidgetItemData(role, value));
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
model->itemChanged(this);
}
/*!
Returns the item's data for the given \a role.
*/
QVariant QTableWidgetItem::data(int role) const
{
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
for (int i = 0; i < values.count(); ++i)
if (values.at(i).role == role)
return values.at(i).value;
return QVariant();
}
/*!
Returns true if the item is less than the \a other item; otherwise returns
false.
*/
bool QTableWidgetItem::operator<(const QTableWidgetItem &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 QTableWidgetItem::read(QDataStream &in)
{
in >> values;
}
/*!
Writes the item to stream \a out.
\sa read()
*/
void QTableWidgetItem::write(QDataStream &out) const
{
out << values;
}
/*!
\relates QTableWidgetItem
Reads a table widget item from stream \a in into \a item.
This operator uses QTableWidgetItem::read().
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item)
{
item.read(in);
return in;
}
/*!
\relates QTableWidgetItem
Writes the table widget item \a item to stream \a out.
This operator uses QTableWidgetItem::write().
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item)
{
item.write(out);
return out;
}
#endif // QT_NO_DATASTREAM
/*!
\since 4.1
Constructs a copy of \a other. Note that type() and tableWidget()
are not copied.
This function is useful when reimplementing clone().
\sa data(), flags()
*/
QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other)
: rtti(Type), values(other.values), view(0),
d(new QTableWidgetItemPrivate(this)),
itemFlags(other.itemFlags)
{
}
/*!
Assigns \a other's data and flags to this item. Note that type()
and tableWidget() are not copied.
This function is useful when reimplementing clone().
\sa data(), flags()
*/
QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other)
{
values = other.values;
itemFlags = other.itemFlags;
return *this;
}
/*!
\class QTableWidget
\brief The QTableWidget class provides an item-based table view with a default model.
\ingroup model-view
Table widgets provide standard table display facilities for applications.
The items in a QTableWidget are provided by QTableWidgetItem.
If you want a table that uses your own data model you should
use QTableView rather than this class.
Table widgets can be constructed with the required numbers of rows and
columns:
\snippet doc/src/snippets/qtablewidget-using/mainwindow.cpp 0
Alternatively, tables can be constructed without a given size and resized
later:
\snippet doc/src/snippets/qtablewidget-resizing/mainwindow.cpp 0
\snippet doc/src/snippets/qtablewidget-resizing/mainwindow.cpp 1
Items are created ouside the table (with no parent widget) and inserted
into the table with setItem():
\snippet doc/src/snippets/qtablewidget-resizing/mainwindow.cpp 2
If you want to enable sorting in your table widget, do so after you
have populated it with items, otherwise sorting may interfere with
the insertion order (see setItem() for details).
Tables can be given both horizontal and vertical headers. The simplest way
to create the headers is to supply a list of strings to the
setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These
will provide simple textual headers for the table's columns and rows.
More sophisticated headers can be created from existing table items
that are usually constructed outside the table. For example, we can
construct a table item with an icon and aligned text, and use it as the
header for a particular column:
\snippet doc/src/snippets/qtablewidget-using/mainwindow.cpp 2
The number of rows in the table can be found with rowCount(), and the
number of columns with columnCount(). The table can be cleared with the
clear() function.
\table 100%
\row \o \inlineimage windowsxp-tableview.png Screenshot of a Windows XP style table widget
\o \inlineimage macintosh-tableview.png Screenshot of a Macintosh style table widget
\o \inlineimage plastique-tableview.png Screenshot of a Plastique style table widget
\row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} table widget.
\o A \l{Macintosh Style Widget Gallery}{Macintosh style} table widget.
\o A \l{Plastique Style Widget Gallery}{Plastique style} table widget.
\endtable
\sa QTableWidgetItem, QTableView, {Model/View Programming}
*/
/*!
\property QTableWidget::rowCount
\brief the number of rows in the table
By default, for a table constructed without row and column counts,
this property contains a value of 0.
*/
/*!
\property QTableWidget::columnCount
\brief the number of columns in the table
By default, for a table constructed without row and column counts,
this property contains a value of 0.
*/
void QTableWidgetPrivate::setup()
{
Q_Q(QTableWidget);
// 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)));
// model signals
QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitItemChanged(QModelIndex)));
// selection signals
QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
q, SIGNAL(itemSelectionChanged()));
// sorting
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 QTableWidgetPrivate::_q_emitItemPressed(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemPressed(item);
emit q->cellPressed(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitItemClicked(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemClicked(item);
emit q->cellClicked(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemDoubleClicked(item);
emit q->cellDoubleClicked(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitItemActivated(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemActivated(item);
emit q->cellActivated(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitItemEntered(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemEntered(item);
emit q->cellEntered(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index)
{
Q_Q(QTableWidget);
if (QTableWidgetItem *item = tableModel()->item(index))
emit q->itemChanged(item);
emit q->cellChanged(index.row(), index.column());
}
void QTableWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t,
const QModelIndex &previous)
{
Q_Q(QTableWidget);
QTableWidgetItem *currentItem = tableModel()->item(current);
QTableWidgetItem *previousItem = tableModel()->item(previous);
if (currentItem || previousItem)
emit q->currentItemChanged(currentItem, previousItem);
emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());
}
void QTableWidgetPrivate::_q_sort()
{
if (sortingEnabled) {
int column = horizontalHeader->sortIndicatorSection();
Qt::SortOrder order = horizontalHeader->sortIndicatorOrder();
model->sort(column, order);
}
}
void QTableWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft,
const QModelIndex &bottomRight)
{
if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) {
int column = horizontalHeader->sortIndicatorSection();
if (column >= topLeft.column() && column <= bottomRight.column()) {
Qt::SortOrder order = horizontalHeader->sortIndicatorOrder();
tableModel()->ensureSorted(column, order, topLeft.row(), bottomRight.row());
}
}
}
/*!
\fn void QTableWidget::itemPressed(QTableWidgetItem *item)
This signal is emitted whenever an item in the table is pressed.
The \a item specified is the item that was pressed.
*/
/*!
\fn void QTableWidget::itemClicked(QTableWidgetItem *item)
This signal is emitted whenever an item in the table is clicked.
The \a item specified is the item that was clicked.
*/
/*!
\fn void QTableWidget::itemDoubleClicked(QTableWidgetItem *item)
This signal is emitted whenever an item in the table is double
clicked. The \a item specified is the item that was double clicked.
*/
/*!
\fn void QTableWidget::itemActivated(QTableWidgetItem *item)
This signal is emitted when the specified \a item has been activated
*/
/*!
\fn void QTableWidget::itemEntered(QTableWidgetItem *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.
*/
/*!
\fn void QTableWidget::itemChanged(QTableWidgetItem *item)
This signal is emitted whenever the data of \a item has changed.
*/
/*!
\fn void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
This signal is emitted whenever the current item changes. The \a
previous item is the item that previously had the focus, \a
current is the new current item.
*/
/*!
\fn void QTableWidget::itemSelectionChanged()
This signal is emitted whenever the selection changes.
\sa selectedItems() QTableWidgetItem::isSelected()
*/
/*!
\since 4.1
\fn void QTableWidget::cellPressed(int row, int column)
This signal is emitted whenever a cell in the table is pressed.
The \a row and \a column specified is the cell that was pressed.
*/
/*!
\since 4.1
\fn void QTableWidget::cellClicked(int row, int column)
This signal is emitted whenever a cell in the table is clicked.
The \a row and \a column specified is the cell that was clicked.
*/
/*!
\since 4.1
\fn void QTableWidget::cellDoubleClicked(int row, int column)
This signal is emitted whenever a cell in the table is double
clicked. The \a row and \a column specified is the cell that was
double clicked.
*/
/*!
\since 4.1
\fn void QTableWidget::cellActivated(int row, int column)
This signal is emitted when the cell specified by \a row and \a column
has been activated
*/
/*!
\since 4.1
\fn void QTableWidget::cellEntered(int row, int column)
This signal is emitted when the mouse cursor enters a cell. The
cell is specified by \a row and \a column.
This signal is only emitted when mouseTracking is turned on, or when a
mouse button is pressed while moving into an item.
*/
/*!
\since 4.1
\fn void QTableWidget::cellChanged(int row, int column)
This signal is emitted whenever the data of the item in the cell
specified by \a row and \a column has changed.
*/
/*!
\since 4.1
\fn void QTableWidget::currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
This signal is emitted whenever the current cell changes. The cell
specified by \a previousRow and \a previousColumn is the cell that
previously had the focus, the cell specified by \a currentRow and \a
currentColumn is the new current cell.
*/
/*!
\since 4.3
\fn void QTableWidget::removeCellWidget(int row, int column)
Removes the widget set on the cell indicated by \a row and \a column.
*/
/*!
\fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const
Returns the item at the position equivalent to QPoint(\a{ax}, \a{ay}) in
the table widget's coordinate system, or returns 0 if the specified point
is not covered by an item in the table widget.
\sa item()
*/
/*!
\enum QTableWidgetItem::ItemType
This enum describes the types that are used to describe table widget items.
\value Type The default type for table widget items.
\value UserType The minimum value for custom types. Values below UserType are
reserved by Qt.
You can define new user types in QTableWidgetItem subclasses to ensure that
custom items are treated specially.
\sa type()
*/
/*!
\fn int QTableWidgetItem::type() const
Returns the type passed to the QTableWidgetItem constructor.
*/
/*!
Creates a new table view with the given \a parent.
*/
QTableWidget::QTableWidget(QWidget *parent)
: QTableView(*new QTableWidgetPrivate, parent)
{
Q_D(QTableWidget);
QTableView::setModel(new QTableModel(0, 0, this));
d->setup();
}
/*!
Creates a new table view with the given \a rows and \a columns, and with the given \a parent.
*/
QTableWidget::QTableWidget(int rows, int columns, QWidget *parent)
: QTableView(*new QTableWidgetPrivate, parent)
{
Q_D(QTableWidget);
QTableView::setModel(new QTableModel(rows, columns, this));
d->setup();
}
/*!
Destroys this QTableWidget.
*/
QTableWidget::~QTableWidget()
{
}
/*!
Sets the number of rows in this table's model to \a rows. If
this is less than rowCount(), the data in the unwanted rows
is discarded.
\sa setColumnCount()
*/
void QTableWidget::setRowCount(int rows)
{
Q_D(QTableWidget);
d->tableModel()->setRowCount(rows);
}
/*!
Returns the number of rows.
*/
int QTableWidget::rowCount() const
{
Q_D(const QTableWidget);
return d->model->rowCount();
}
/*!
Sets the number of columns in this table's model to \a columns. If
this is less than columnCount(), the data in the unwanted columns
is discarded.
\sa setRowCount()
*/
void QTableWidget::setColumnCount(int columns)
{
Q_D(QTableWidget);
d->tableModel()->setColumnCount(columns);
}
/*!
Returns the number of columns.
*/
int QTableWidget::columnCount() const
{
Q_D(const QTableWidget);
return d->model->columnCount();
}
/*!
Returns the row for the \a item.
*/
int QTableWidget::row(const QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
return d->tableModel()->index(item).row();
}
/*!
Returns the column for the \a item.
*/
int QTableWidget::column(const QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
return d->tableModel()->index(item).column();
}
/*!
Returns the item for the given \a row and \a column if one has been set; otherwise
returns 0.
\sa setItem()
*/
QTableWidgetItem *QTableWidget::item(int row, int column) const
{
Q_D(const QTableWidget);
return d->tableModel()->item(row, column);
}
/*!
Sets the item for the given \a row and \a column to \a item.
The table takes ownership of the item.
Note that if sorting is enabled (see
\l{QTableView::sortingEnabled} {sortingEnabled}) and \a column is
the current sort column, the \a row will be moved to the sorted
position determined by \a item.
If you want to set several items of a particular row (say, by
calling setItem() in a loop), you may want to turn off sorting
before doing so, and turn it back on afterwards; this will allow
you to use the same \a row argument for all items in the same row
(i.e. setItem() will not move the row).
\sa item() takeItem()
*/
void QTableWidget::setItem(int row, int column, QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (item) {
if (item->view != 0) {
qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget");
} else {
item->view = this;
d->tableModel()->setItem(row, column, item);
}
} else {
delete takeItem(row, column);
}
}
/*!
Removes the item at \a row and \a column from the table without deleting it.
*/
QTableWidgetItem *QTableWidget::takeItem(int row, int column)
{
Q_D(QTableWidget);
QTableWidgetItem *item = d->tableModel()->takeItem(row, column);
if (item)
item->view = 0;
return item;
}
/*!
Returns the vertical header item for row \a row.
*/
QTableWidgetItem *QTableWidget::verticalHeaderItem(int row) const
{
Q_D(const QTableWidget);
return d->tableModel()->verticalHeaderItem(row);
}
/*!
Sets the vertical header item for row \a row to \a item.
*/
void QTableWidget::setVerticalHeaderItem(int row, QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (item) {
item->view = this;
d->tableModel()->setVerticalHeaderItem(row, item);
} else {
delete takeVerticalHeaderItem(row);
}
}
/*!
\since 4.1
Removes the vertical header item at \a row from the header without deleting it.
*/
QTableWidgetItem *QTableWidget::takeVerticalHeaderItem(int row)
{
Q_D(QTableWidget);
QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row);
if (itm)
itm->view = 0;
return itm;
}
/*!
Returns the horizontal header item for column, \a column, if one has been
set; otherwise returns 0.
*/
QTableWidgetItem *QTableWidget::horizontalHeaderItem(int column) const
{
Q_D(const QTableWidget);
return d->tableModel()->horizontalHeaderItem(column);
}
/*!
Sets the horizontal header item for column \a column to \a item.
*/
void QTableWidget::setHorizontalHeaderItem(int column, QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (item) {
item->view = this;
d->tableModel()->setHorizontalHeaderItem(column, item);
} else {
delete takeHorizontalHeaderItem(column);
}
}
/*!
\since 4.1
Removes the horizontal header item at \a column from the header without deleting it.
*/
QTableWidgetItem *QTableWidget::takeHorizontalHeaderItem(int column)
{
Q_D(QTableWidget);
QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column);
if (itm)
itm->view = 0;
return itm;
}
/*!
Sets the vertical header labels using \a labels.
*/
void QTableWidget::setVerticalHeaderLabels(const QStringList &labels)
{
Q_D(QTableWidget);
QTableModel *model = d->tableModel();
QTableWidgetItem *item = 0;
for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) {
item = model->verticalHeaderItem(i);
if (!item) {
item = model->createItem();
setVerticalHeaderItem(i, item);
}
item->setText(labels.at(i));
}
}
/*!
Sets the horizontal header labels using \a labels.
*/
void QTableWidget::setHorizontalHeaderLabels(const QStringList &labels)
{
Q_D(QTableWidget);
QTableModel *model = d->tableModel();
QTableWidgetItem *item = 0;
for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) {
item = model->horizontalHeaderItem(i);
if (!item) {
item = model->createItem();
setHorizontalHeaderItem(i, item);
}
item->setText(labels.at(i));
}
}
/*!
Returns the row of the current item.
\sa currentColumn(), setCurrentCell()
*/
int QTableWidget::currentRow() const
{
return currentIndex().row();
}
/*!
Returns the column of the current item.
\sa currentRow(), setCurrentCell()
*/
int QTableWidget::currentColumn() const
{
return currentIndex().column();
}
/*!
Returns the current item.
\sa setCurrentItem()
*/
QTableWidgetItem *QTableWidget::currentItem() const
{
Q_D(const QTableWidget);
return d->tableModel()->item(currentIndex());
}
/*!
Sets the current item to \a item.
Unless the selection mode is \l{QAbstractItemView::}{NoSelection},
the item is also be selected.
\sa currentItem(), setCurrentCell()
*/
void QTableWidget::setCurrentItem(QTableWidgetItem *item)
{
Q_D(QTableWidget);
setCurrentIndex(d->tableModel()->index(item));
}
/*!
\since 4.4
Sets the current item to be \a item, using the given \a command.
\sa currentItem(), setCurrentCell()
*/
void QTableWidget::setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command)
{
Q_D(QTableWidget);
d->selectionModel->setCurrentIndex(d->tableModel()->index(item), command);
}
/*!
\since 4.1
Sets the current cell to be the cell at position (\a row, \a
column).
Depending on the current \l{QAbstractItemView::SelectionMode}{selection mode},
the cell may also be selected.
\sa setCurrentItem(), currentRow(), currentColumn()
*/
void QTableWidget::setCurrentCell(int row, int column)
{
setCurrentIndex(model()->index(row, column, QModelIndex()));
}
/*!
\since 4.4
Sets the current cell to be the cell at position (\a row, \a
column), using the given \a command.
\sa setCurrentItem(), currentRow(), currentColumn()
*/
void QTableWidget::setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command)
{
Q_D(QTableWidget);
d->selectionModel->setCurrentIndex(model()->index(row, column, QModelIndex()), command);
}
/*!
Sorts all the rows in the table widget based on \a column and \a order.
*/
void QTableWidget::sortItems(int column, Qt::SortOrder order)
{
Q_D(QTableWidget);
d->model->sort(column, order);
horizontalHeader()->setSortIndicator(column, order);
}
/*!
\internal
*/
void QTableWidget::setSortingEnabled(bool enable)
{
QTableView::setSortingEnabled(enable);
}
/*!
\internal
*/
bool QTableWidget::isSortingEnabled() const
{
return QTableView::isSortingEnabled();
}
/*!
Starts editing the \a item if it is editable.
*/
void QTableWidget::editItem(QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (!item)
return;
edit(d->tableModel()->index(item));
}
/*!
Opens an editor for the give \a item. The editor remains open after editing.
\sa closePersistentEditor()
*/
void QTableWidget::openPersistentEditor(QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (!item)
return;
QModelIndex index = d->tableModel()->index(item);
QAbstractItemView::openPersistentEditor(index);
}
/*!
Closes the persistent editor for \a item.
\sa openPersistentEditor()
*/
void QTableWidget::closePersistentEditor(QTableWidgetItem *item)
{
Q_D(QTableWidget);
if (!item)
return;
QModelIndex index = d->tableModel()->index(item);
QAbstractItemView::closePersistentEditor(index);
}
/*!
\since 4.1
Returns the widget displayed in the cell in the given \a row and \a column.
\note The table takes ownership of the widget.
\sa setCellWidget()
*/
QWidget *QTableWidget::cellWidget(int row, int column) const
{
QModelIndex index = model()->index(row, column, QModelIndex());
return QAbstractItemView::indexWidget(index);
}
/*!
\since 4.1
Sets the given \a widget to be displayed in the cell in the given \a row
and \a column, passing the ownership of the widget to the table.
If cell widget A is replaced with cell widget B, cell widget A will be
deleted. For example, in the code snippet below, the QLineEdit object will
be deleted.
\snippet doc/src/snippets/code/src_gui_itemviews_qtablewidget.cpp 0
\sa cellWidget()
*/
void QTableWidget::setCellWidget(int row, int column, QWidget *widget)
{
QModelIndex index = model()->index(row, column, QModelIndex());
QAbstractItemView::setIndexWidget(index, widget);
}
/*!
Returns true if the \a item is selected, otherwise returns false.
\obsolete
This function is deprecated. Use \l{QTableWidgetItem::isSelected()} instead.
*/
bool QTableWidget::isItemSelected(const QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
QModelIndex index = d->tableModel()->index(item);
return selectionModel()->isSelected(index);
}
/*!
Selects or deselects \a item depending on \a select.
\obsolete
This function is deprecated. Use \l{QTableWidgetItem::setSelected()} instead.
*/
void QTableWidget::setItemSelected(const QTableWidgetItem *item, bool select)
{
Q_D(QTableWidget);
QModelIndex index = d->tableModel()->index(item);
selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
}
/*!
Selects or deselects the \a range depending on \a select.
*/
void QTableWidget::setRangeSelected(const QTableWidgetSelectionRange &range, bool select)
{
if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
!model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
return;
QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
selectionModel()->select(QItemSelection(topLeft, bottomRight),
select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
}
/*!
Returns a list of all selected ranges.
\sa QTableWidgetSelectionRange
*/
QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
{
const QList<QItemSelectionRange> ranges = selectionModel()->selection();
QList<QTableWidgetSelectionRange> result;
for (int i = 0; i < ranges.count(); ++i)
result.append(QTableWidgetSelectionRange(ranges.at(i).top(),
ranges.at(i).left(),
ranges.at(i).bottom(),
ranges.at(i).right()));
return result;
}
/*!
Returns a list of all selected items.
This function returns a list of pointers to the contents of the
selected cells. Use the selectedIndexes() function to retrieve the
complete selection \e including empty cells.
\sa selectedIndexes()
*/
QList<QTableWidgetItem*> QTableWidget::selectedItems()
{
Q_D(QTableWidget);
QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTableWidgetItem*> items;
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
if (isIndexHidden(index))
continue;
QTableWidgetItem *item = d->tableModel()->item(index);
if (item)
items.append(item);
}
return items;
}
/*!
Finds items that matches the \a text using the given \a flags.
*/
QList<QTableWidgetItem*> QTableWidget::findItems(const QString &text, Qt::MatchFlags flags) const
{
Q_D(const QTableWidget);
QModelIndexList indexes;
for (int column = 0; column < columnCount(); ++column)
indexes += d->model->match(model()->index(0, column, QModelIndex()),
Qt::DisplayRole, text, -1, flags);
QList<QTableWidgetItem*> items;
for (int i = 0; i < indexes.size(); ++i)
items.append(d->tableModel()->item(indexes.at(i)));
return items;
}
/*!
Returns the visual row of the given \a logicalRow.
*/
int QTableWidget::visualRow(int logicalRow) const
{
return verticalHeader()->visualIndex(logicalRow);
}
/*!
Returns the visual column of the given \a logicalColumn.
*/
int QTableWidget::visualColumn(int logicalColumn) const
{
return horizontalHeader()->visualIndex(logicalColumn);
}
/*!
\fn QTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const
Returns a pointer to the item at the given \a point, or returns 0 if
\a point is not covered by an item in the table widget.
\sa item()
*/
QTableWidgetItem *QTableWidget::itemAt(const QPoint &p) const
{
Q_D(const QTableWidget);
return d->tableModel()->item(indexAt(p));
}
/*!
Returns the rectangle on the viewport occupied by the item at \a item.
*/
QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
if (!item)
return QRect();
QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
Q_ASSERT(index.isValid());
return visualRect(index);
}
/*!
Scrolls the view if necessary to ensure that the \a item is visible.
The \a hint parameter specifies more precisely where the
\a item should be located after the operation.
*/
void QTableWidget::scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint)
{
Q_D(QTableWidget);
if (!item)
return;
QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item));
Q_ASSERT(index.isValid());
QTableView::scrollTo(index, hint);
}
/*!
Returns the item prototype used by the table.
\sa setItemPrototype()
*/
const QTableWidgetItem *QTableWidget::itemPrototype() const
{
Q_D(const QTableWidget);
return d->tableModel()->itemPrototype();
}
/*!
Sets the item prototype for the table to the specified \a item.
The table widget will use the item prototype clone function when it needs
to create a new table item. For example when the user is editing
editing in an empty cell. This is useful when you have a QTableWidgetItem
subclass and want to make sure that QTableWidget creates instances of
your subclass.
The table takes ownership of the prototype.
\sa itemPrototype()
*/
void QTableWidget::setItemPrototype(const QTableWidgetItem *item)
{
Q_D(QTableWidget);
d->tableModel()->setItemPrototype(item);
}
/*!
Inserts an empty row into the table at \a row.
*/
void QTableWidget::insertRow(int row)
{
Q_D(QTableWidget);
d->tableModel()->insertRows(row);
}
/*!
Inserts an empty column into the table at \a column.
*/
void QTableWidget::insertColumn(int column)
{
Q_D(QTableWidget);
d->tableModel()->insertColumns(column);
}
/*!
Removes the row \a row and all its items from the table.
*/
void QTableWidget::removeRow(int row)
{
Q_D(QTableWidget);
d->tableModel()->removeRows(row);
}
/*!
Removes the column \a column and all its items from the table.
*/
void QTableWidget::removeColumn(int column)
{
Q_D(QTableWidget);
d->tableModel()->removeColumns(column);
}
/*!
Removes all items in the view.
This will also remove all selections.
The table dimensions stay the same.
*/
void QTableWidget::clear()
{
Q_D(QTableWidget);
selectionModel()->clear();
d->tableModel()->clear();
}
/*!
\since 4.2
Removes all items not in the headers from the view.
This will also remove all selections.
The table dimensions stay the same.
*/
void QTableWidget::clearContents()
{
Q_D(QTableWidget);
selectionModel()->clear();
d->tableModel()->clearContents();
}
/*!
Returns a list of MIME types that can be used to describe a list of
tablewidget items.
\sa mimeData()
*/
QStringList QTableWidget::mimeTypes() const
{
return d_func()->tableModel()->QAbstractTableModel::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, 0 is returned rather than a serialized
empty list.
*/
QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*>) const
{
return d_func()->tableModel()->internalMimeData();
}
/*!
Handles the \a data supplied by a drag and drop operation that ended with
the given \a action in the given \a row and \a column.
Returns true if the data and action can be handled by the model;
otherwise returns false.
\sa supportedDropActions()
*/
bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
{
QModelIndex idx;
#ifndef QT_NO_DRAGANDDROP
if (dropIndicatorPosition() == QAbstractItemView::OnItem) {
// QAbstractTableModel::dropMimeData will overwrite on the index if row == -1 and column == -1
idx = model()->index(row, column);
row = -1;
column = -1;
}
#endif
return d_func()->tableModel()->QAbstractTableModel::dropMimeData(data, action , row, column, idx);
}
/*!
Returns the drop actions supported by this view.
\sa Qt::DropActions
*/
Qt::DropActions QTableWidget::supportedDropActions() const
{
return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
}
/*!
Returns a list of pointers to the items contained in the \a data object.
If the object was not created by a QTreeWidget in the same process, the list
is empty.
*/
QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const
{
const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data);
if (twd)
return twd->items;
return QList<QTableWidgetItem*>();
}
/*!
Returns the QModelIndex assocated with the given \a item.
*/
QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
{
Q_D(const QTableWidget);
return d->tableModel()->index(item);
}
/*!
Returns a pointer to the QTableWidgetItem assocated with the given \a index.
*/
QTableWidgetItem *QTableWidget::itemFromIndex(const QModelIndex &index) const
{
Q_D(const QTableWidget);
return d->tableModel()->item(index);
}
/*!
\internal
*/
void QTableWidget::setModel(QAbstractItemModel * /*model*/)
{
Q_ASSERT(!"QTableWidget::setModel() - Changing the model of the QTableWidget is not allowed.");
}
/*! \reimp */
bool QTableWidget::event(QEvent *e)
{
return QTableView::event(e);
}
#ifndef QT_NO_DRAGANDDROP
/*! \reimp */
void QTableWidget::dropEvent(QDropEvent *event) {
Q_D(QTableWidget);
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)) {
QModelIndexList indexes = selectedIndexes();
int top = INT_MAX;
int left = INT_MAX;
for (int i = 0; i < indexes.count(); ++i) {
top = qMin(indexes.at(i).row(), top);
left = qMin(indexes.at(i).column(), left);
}
QList<QTableWidgetItem *> taken;
for (int i = 0; i < indexes.count(); ++i)
taken.append(takeItem(indexes.at(i).row(), indexes.at(i).column()));
for (int i = 0; i < indexes.count(); ++i) {
QModelIndex index = indexes.at(i);
int r = index.row() - top + topIndex.row();
int c = index.column() - left + topIndex.column();
setItem(r, c, taken.takeFirst());
}
event->accept();
// Don't want QAbstractItemView to delete it because it was "moved" we already did it
event->setDropAction(Qt::CopyAction);
}
}
QTableView::dropEvent(event);
}
#endif
QT_END_NAMESPACE
#include "moc_qtablewidget.cpp"
#endif // QT_NO_TABLEWIDGET
|