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
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QProxyStyle>
#include <qabstractitemmodel.h>
#include <qapplication.h>
#include <qheaderview.h>
#include <private/qheaderview_p.h>
#include <qitemdelegate.h>
#include <qtreewidget.h>
#include <qdebug.h>
#include <qscreen.h>
typedef QList<int> IntList;
typedef QList<bool> BoolList;
class TestStyle : public QProxyStyle
{
public:
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
if (element == CE_HeaderSection) {
if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option))
lastPosition = header->position;
}
QProxyStyle::drawControl(element, option, painter, widget);
}
mutable QStyleOptionHeader::SectionPosition lastPosition;
};
class protected_QHeaderView : public QHeaderView
{
Q_OBJECT
public:
protected_QHeaderView(Qt::Orientation orientation) : QHeaderView(orientation) {
resizeSections();
};
void testEvent();
void testhorizontalOffset();
void testverticalOffset();
void testVisualRegionForSelection();
friend class tst_QHeaderView;
};
class XResetModel : public QStandardItemModel
{
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
{
blockSignals(true);
bool r = QStandardItemModel::removeRows(row, count, parent);
blockSignals(false);
emit reset();
return r;
}
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
{
blockSignals(true);
bool r = QStandardItemModel::insertRows(row, count, parent);
blockSignals(false);
emit reset();
return r;
}
};
class tst_QHeaderView : public QObject
{
Q_OBJECT
public:
tst_QHeaderView();
private slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
void getSetCheck();
void visualIndex();
void visualIndexAt_data();
void visualIndexAt();
void noModel();
void emptyModel();
void removeRows();
void removeCols();
void clickable();
void movable();
void hidden();
void stretch();
void sectionSize_data();
void sectionSize();
void length();
void offset();
void sectionSizeHint();
void logicalIndex();
void logicalIndexAt();
void swapSections();
void moveSection_data();
void moveSection();
void resizeMode();
void resizeSection_data();
void resizeSection();
void resizeAndMoveSection_data();
void resizeAndMoveSection();
void resizeHiddenSection_data();
void resizeHiddenSection();
void resizeAndInsertSection_data();
void resizeAndInsertSection();
void resizeWithResizeModes_data();
void resizeWithResizeModes();
void moveAndInsertSection_data();
void moveAndInsertSection();
void highlightSections();
void showSortIndicator();
void sortIndicatorTracking();
void removeAndInsertRow();
void unhideSection();
void testEvent();
void headerDataChanged();
void currentChanged();
void horizontalOffset();
void verticalOffset();
void stretchSectionCount();
void hiddenSectionCount();
void focusPolicy();
void moveSectionAndReset();
void moveSectionAndRemove();
void saveRestore();
void restoreQt4State();
void restoreToMoreColumns();
void restoreToMoreColumnsNoMovedColumns();
void restoreBeforeSetModel();
void defaultSectionSizeTest();
void defaultSectionSizeTestStyles();
void defaultAlignment_data();
void defaultAlignment();
void globalResizeMode_data();
void globalResizeMode();
void sectionPressedSignal_data();
void sectionPressedSignal();
void sectionClickedSignal_data() { sectionPressedSignal_data(); }
void sectionClickedSignal();
void defaultSectionSize_data();
void defaultSectionSize();
void oneSectionSize();
void hideAndInsert_data();
void hideAndInsert();
void removeSection();
void preserveHiddenSectionWidth();
void invisibleStretchLastSection();
void noSectionsWithNegativeSize();
void emptySectionSpan();
void task236450_hidden_data();
void task236450_hidden();
void task248050_hideRow();
void QTBUG6058_reset();
void QTBUG7833_sectionClicked();
void QTBUG8650_crashOnInsertSections();
void QTBUG12268_hiddenMovedSectionSorting();
void QTBUG14242_hideSectionAutoSize();
void QTBUG50171_visualRegionForSwappedItems();
void QTBUG53221_assertShiftHiddenRow();
void ensureNoIndexAtLength();
void offsetConsistent();
void initialSortOrderRole();
void logicalIndexAtTest_data() { setupTestData(); }
void visualIndexAtTest_data() { setupTestData(); }
void hideShowTest_data() { setupTestData(); }
void swapSectionsTest_data() { setupTestData(); }
void moveSectionTest_data() { setupTestData(); }
void defaultSizeTest_data() { setupTestData(); }
void removeTest_data() { setupTestData(true); }
void insertTest_data() { setupTestData(true); }
void mixedTests_data() { setupTestData(true); }
void resizeToContentTest_data() { setupTestData(); }
void logicalIndexAtTest();
void visualIndexAtTest();
void hideShowTest();
void swapSectionsTest();
void moveSectionTest();
void defaultSizeTest();
void removeTest();
void insertTest();
void mixedTests();
void resizeToContentTest();
void testStreamWithHide();
void testStylePosition();
void stretchAndRestoreLastSection();
void testMinMaxSectionSizeStretched();
void testMinMaxSectionSizeNotStretched();
void sizeHintCrash();
protected:
void setupTestData(bool use_reset_model = false);
void additionalInit();
void calculateAndCheck(int cppline, const int precalced_comparedata[]);
void testMinMaxSectionSize(bool stretchLastSection);
QWidget *topLevel;
QHeaderView *view;
QStandardItemModel *model;
QTableView *m_tableview;
bool m_using_reset_model;
QElapsedTimer timer;
};
class QtTestModel: public QAbstractTableModel
{
Q_OBJECT
public:
QtTestModel(QObject *parent = 0): QAbstractTableModel(parent),
cols(0), rows(0), wrongIndex(false) {}
int rowCount(const QModelIndex&) const { return rows; }
int columnCount(const QModelIndex&) const { return cols; }
bool isEditable(const QModelIndex &) const { return true; }
QVariant data(const QModelIndex &idx, int) const
{
if (idx.row() < 0 || idx.column() < 0 || idx.column() >= cols || idx.row() >= rows) {
wrongIndex = true;
qWarning("Invalid modelIndex [%d,%d,%p]", idx.row(), idx.column(), idx.internalPointer());
}
return QLatin1Char('[') + QString::number(idx.row()) + QLatin1Char(',')
+ QString::number(idx.column()) + QLatin1String(",0]");
}
void insertOneColumn(int col)
{
beginInsertColumns(QModelIndex(), col, col);
--cols;
endInsertColumns();
}
void removeLastRow()
{
beginRemoveRows(QModelIndex(), rows - 1, rows - 1);
--rows;
endRemoveRows();
}
void removeAllRows()
{
beginRemoveRows(QModelIndex(), 0, rows - 1);
rows = 0;
endRemoveRows();
}
void removeOneColumn(int col)
{
beginRemoveColumns(QModelIndex(), col, col);
--cols;
endRemoveColumns();
}
void removeLastColumn()
{
beginRemoveColumns(QModelIndex(), cols - 1, cols - 1);
--cols;
endRemoveColumns();
}
void removeAllColumns()
{
beginRemoveColumns(QModelIndex(), 0, cols - 1);
cols = 0;
endRemoveColumns();
}
void cleanup()
{
cols = 3;
rows = 3;
emit layoutChanged();
}
int cols, rows;
mutable bool wrongIndex;
};
// Testing get/set functions
void tst_QHeaderView::getSetCheck()
{
protected_QHeaderView obj1(Qt::Horizontal);
// bool QHeaderView::highlightSections()
// void QHeaderView::setHighlightSections(bool)
obj1.setHighlightSections(false);
QCOMPARE(false, obj1.highlightSections());
obj1.setHighlightSections(true);
QCOMPARE(true, obj1.highlightSections());
// bool QHeaderView::stretchLastSection()
// void QHeaderView::setStretchLastSection(bool)
obj1.setStretchLastSection(false);
QCOMPARE(false, obj1.stretchLastSection());
obj1.setStretchLastSection(true);
QCOMPARE(true, obj1.stretchLastSection());
// int QHeaderView::defaultSectionSize()
// void QHeaderView::setDefaultSectionSize(int)
obj1.setDefaultSectionSize(-1);
QVERIFY(obj1.defaultSectionSize() >= 0);
obj1.setDefaultSectionSize(0);
QCOMPARE(0, obj1.defaultSectionSize());
obj1.setDefaultSectionSize(99999);
QCOMPARE(99999, obj1.defaultSectionSize());
// int QHeaderView::minimumSectionSize()
// void QHeaderView::setMinimumSectionSize(int)
obj1.setMinimumSectionSize(-1);
QVERIFY(obj1.minimumSectionSize() >= 0);
obj1.setMinimumSectionSize(0);
QCOMPARE(0, obj1.minimumSectionSize());
obj1.setMinimumSectionSize(99999);
QCOMPARE(99999, obj1.minimumSectionSize());
obj1.setMinimumSectionSize(-1);
QVERIFY(obj1.minimumSectionSize() < 100);
// int QHeaderView::offset()
// void QHeaderView::setOffset(int)
obj1.setOffset(0);
QCOMPARE(0, obj1.offset());
obj1.setOffset(INT_MIN);
QCOMPARE(INT_MIN, obj1.offset());
obj1.setOffset(INT_MAX);
QCOMPARE(INT_MAX, obj1.offset());
}
tst_QHeaderView::tst_QHeaderView()
{
qRegisterMetaType<int>("Qt::SortOrder");
}
void tst_QHeaderView::initTestCase()
{
m_tableview = new QTableView();
}
void tst_QHeaderView::cleanupTestCase()
{
delete m_tableview;
}
void tst_QHeaderView::init()
{
topLevel = new QWidget();
view = new QHeaderView(Qt::Vertical,topLevel);
// Some initial value tests before a model is added
QCOMPARE(view->length(), 0);
QCOMPARE(view->sizeHint(), QSize(0,0));
QCOMPARE(view->sectionSizeHint(0), -1);
view->setMinimumSectionSize(0); // system default min size can be to large
/*
model = new QStandardItemModel(1, 1);
view->setModel(model);
//qDebug() << view->count();
view->sizeHint();
*/
int rows = 4;
int columns = 4;
model = new QStandardItemModel(rows, columns);
/*
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
QModelIndex index = model->index(row, column, QModelIndex());
model->setData(index, QVariant((row+1) * (column+1)));
}
}
*/
QSignalSpy spy(view, SIGNAL(sectionCountChanged(int,int)));
view->setModel(model);
QCOMPARE(spy.count(), 1);
view->resize(200,200);
}
void tst_QHeaderView::cleanup()
{
m_tableview->setUpdatesEnabled(true);
if (view && view->parent() != m_tableview)
delete view;
view = 0;
delete model;
model = 0;
delete topLevel;
topLevel = 0;
}
void tst_QHeaderView::noModel()
{
QHeaderView emptyView(Qt::Vertical);
QCOMPARE(emptyView.count(), 0);
}
void tst_QHeaderView::emptyModel()
{
QtTestModel testmodel;
view->setModel(&testmodel);
QVERIFY(!testmodel.wrongIndex);
QCOMPARE(view->count(), testmodel.rows);
view->setModel(model);
}
void tst_QHeaderView::removeRows()
{
QtTestModel model;
model.rows = model.cols = 10;
QHeaderView vertical(Qt::Vertical);
QHeaderView horizontal(Qt::Horizontal);
vertical.setModel(&model);
horizontal.setModel(&model);
vertical.show();
horizontal.show();
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
model.removeLastRow();
QVERIFY(!model.wrongIndex);
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
model.removeAllRows();
QVERIFY(!model.wrongIndex);
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
}
void tst_QHeaderView::removeCols()
{
QtTestModel model;
model.rows = model.cols = 10;
QHeaderView vertical(Qt::Vertical);
QHeaderView horizontal(Qt::Horizontal);
vertical.setModel(&model);
horizontal.setModel(&model);
vertical.show();
horizontal.show();
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
model.removeLastColumn();
QVERIFY(!model.wrongIndex);
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
model.removeAllColumns();
QVERIFY(!model.wrongIndex);
QCOMPARE(vertical.count(), model.rows);
QCOMPARE(horizontal.count(), model.cols);
}
void tst_QHeaderView::movable()
{
QCOMPARE(view->sectionsMovable(), false);
view->setSectionsMovable(false);
QCOMPARE(view->sectionsMovable(), false);
view->setSectionsMovable(true);
QCOMPARE(view->sectionsMovable(), true);
QCOMPARE(view->firstSectionMovable(), true);
view->setFirstSectionMovable(false);
QCOMPARE(view->firstSectionMovable(), false);
view->setFirstSectionMovable(true);
QCOMPARE(view->firstSectionMovable(), true);
}
void tst_QHeaderView::clickable()
{
QCOMPARE(view->sectionsClickable(), false);
view->setSectionsClickable(false);
QCOMPARE(view->sectionsClickable(), false);
view->setSectionsClickable(true);
QCOMPARE(view->sectionsClickable(), true);
}
void tst_QHeaderView::hidden()
{
//hideSection() & showSection call setSectionHidden
// Test bad arguments
QCOMPARE(view->isSectionHidden(-1), false);
QCOMPARE(view->isSectionHidden(view->count()), false);
QCOMPARE(view->isSectionHidden(999999), false);
view->setSectionHidden(-1, true);
view->setSectionHidden(view->count(), true);
view->setSectionHidden(999999, true);
view->setSectionHidden(-1, false);
view->setSectionHidden(view->count(), false);
view->setSectionHidden(999999, false);
// Hidden sections shouldn't have visual properties (except position)
int pos = view->defaultSectionSize();
view->setSectionHidden(1, true);
QCOMPARE(view->sectionSize(1), 0);
QCOMPARE(view->sectionPosition(1), pos);
view->resizeSection(1, 100);
QCOMPARE(view->sectionViewportPosition(1), pos);
QCOMPARE(view->sectionSize(1), 0);
view->setSectionHidden(1, false);
QCOMPARE(view->isSectionHidden(0), false);
QCOMPARE(view->sectionSize(0), view->defaultSectionSize());
}
void tst_QHeaderView::stretch()
{
// Show before resize and setStretchLastSection
QSize viewSize(500, 500);
view->resize(viewSize);
view->setStretchLastSection(true);
QCOMPARE(view->stretchLastSection(), true);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
QCOMPARE(view->width(), viewSize.width());
QCOMPARE(view->visualIndexAt(view->viewport()->height() - 5), 3);
view->setSectionHidden(3, true);
QCOMPARE(view->visualIndexAt(view->viewport()->height() - 5), 2);
view->setStretchLastSection(false);
QCOMPARE(view->stretchLastSection(), false);
}
void tst_QHeaderView::oneSectionSize()
{
//this ensures that if there is only one section, it gets a correct width (more than 0)
QHeaderView view (Qt::Vertical);
QtTestModel model;
model.cols = 1;
model.rows = 1;
view.setSectionResizeMode(QHeaderView::Interactive);
view.setModel(&model);
view.show();
QVERIFY(view.sectionSize(0) > 0);
}
void tst_QHeaderView::sectionSize_data()
{
QTest::addColumn<QList<int> >("boundsCheck");
QTest::addColumn<QList<int> >("defaultSizes");
QTest::addColumn<int>("initialDefaultSize");
QTest::addColumn<int>("lastVisibleSectionSize");
QTest::addColumn<int>("persistentSectionSize");
QTest::newRow("data set one")
<< (QList<int>() << -1 << 0 << 4 << 9999)
<< (QList<int>() << 10 << 30 << 30)
<< 30
<< 300
<< 20;
}
void tst_QHeaderView::sectionSize()
{
#if defined Q_OS_QNX
QSKIP("The section size is dpi dependent on QNX");
#endif
QFETCH(QList<int>, boundsCheck);
QFETCH(QList<int>, defaultSizes);
QFETCH(int, initialDefaultSize);
QFETCH(int, lastVisibleSectionSize);
QFETCH(int, persistentSectionSize);
// bounds check
foreach (int val, boundsCheck)
view->sectionSize(val);
// default size
QCOMPARE(view->defaultSectionSize(), initialDefaultSize);
foreach (int def, defaultSizes) {
view->setDefaultSectionSize(def);
QCOMPARE(view->defaultSectionSize(), def);
}
view->setDefaultSectionSize(initialDefaultSize);
for (int s = 0; s < view->count(); ++s)
QCOMPARE(view->sectionSize(s), initialDefaultSize);
view->doItemsLayout();
// stretch last section
view->setStretchLastSection(true);
int lastSection = view->count() - 1;
//test that when hiding the last column,
//resizing the new last visible columns still works
view->hideSection(lastSection);
view->resizeSection(lastSection - 1, lastVisibleSectionSize);
QCOMPARE(view->sectionSize(lastSection - 1), lastVisibleSectionSize);
view->showSection(lastSection);
// turn off stretching
view->setStretchLastSection(false);
QCOMPARE(view->sectionSize(lastSection), initialDefaultSize);
// test persistence
int sectionCount = view->count();
for (int i = 0; i < sectionCount; ++i)
view->resizeSection(i, persistentSectionSize);
QtTestModel model;
model.cols = sectionCount * 2;
model.rows = sectionCount * 2;
view->setModel(&model);
for (int j = 0; j < sectionCount; ++j)
QCOMPARE(view->sectionSize(j), persistentSectionSize);
for (int k = sectionCount; k < view->count(); ++k)
QCOMPARE(view->sectionSize(k), initialDefaultSize);
}
void tst_QHeaderView::visualIndex()
{
// Test bad arguments
QCOMPARE(view->visualIndex(999999), -1);
QCOMPARE(view->visualIndex(-1), -1);
QCOMPARE(view->visualIndex(1), 1);
view->setSectionHidden(1, true);
QCOMPARE(view->visualIndex(1), 1);
QCOMPARE(view->visualIndex(2), 2);
view->setSectionHidden(1, false);
QCOMPARE(view->visualIndex(1), 1);
QCOMPARE(view->visualIndex(2), 2);
}
void tst_QHeaderView::visualIndexAt_data()
{
QTest::addColumn<QList<int> >("hidden");
QTest::addColumn<QList<int> >("from");
QTest::addColumn<QList<int> >("to");
QTest::addColumn<QList<int> >("coordinate");
QTest::addColumn<QList<int> >("visual");
QList<int> coordinateList;
coordinateList << -1 << 0 << 31 << 91 << 99999;
QTest::newRow("no hidden, no moved sections")
<< QList<int>()
<< QList<int>()
<< QList<int>()
<< coordinateList
<< (QList<int>() << -1 << 0 << 1 << 3 << -1);
QTest::newRow("no hidden, moved sections")
<< QList<int>()
<< (QList<int>() << 0)
<< (QList<int>() << 1)
<< coordinateList
<< (QList<int>() << -1 << 0 << 1 << 3 << -1);
QTest::newRow("hidden, no moved sections")
<< (QList<int>() << 0)
<< QList<int>()
<< QList<int>()
<< coordinateList
<< (QList<int>() << -1 << 1 << 2 << 3 << -1);
}
void tst_QHeaderView::visualIndexAt()
{
#if defined Q_OS_QNX
QSKIP("The section size is dpi dependent on QNX");
#endif
QFETCH(QList<int>, hidden);
QFETCH(QList<int>, from);
QFETCH(QList<int>, to);
QFETCH(QList<int>, coordinate);
QFETCH(QList<int>, visual);
view->setStretchLastSection(true);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
for (int i = 0; i < hidden.count(); ++i)
view->setSectionHidden(hidden.at(i), true);
for (int j = 0; j < from.count(); ++j)
view->moveSection(from.at(j), to.at(j));
QTest::qWait(100);
for (int k = 0; k < coordinate.count(); ++k)
QCOMPARE(view->visualIndexAt(coordinate.at(k)), visual.at(k));
}
void tst_QHeaderView::length()
{
view->setStretchLastSection(true);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
//minimumSectionSize should be the size of the last section of the widget is not tall enough
int length = view->minimumSectionSize();
for (int i=0; i < view->count()-1; i++) {
length += view->sectionSize(i);
}
length = qMax(length, view->viewport()->height());
QCOMPARE(length, view->length());
view->setStretchLastSection(false);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
QVERIFY(length != view->length());
// layoutChanged might mean rows have been removed
QtTestModel model;
model.cols = 10;
model.rows = 10;
view->setModel(&model);
int oldLength = view->length();
model.cleanup();
QCOMPARE(model.rows, view->count());
QVERIFY(oldLength != view->length());
}
void tst_QHeaderView::offset()
{
QCOMPARE(view->offset(), 0);
view->setOffset(10);
QCOMPARE(view->offset(), 10);
view->setOffset(0);
QCOMPARE(view->offset(), 0);
// Test odd arguments
view->setOffset(-1);
}
void tst_QHeaderView::sectionSizeHint()
{
// Test bad arguments
view->sectionSizeHint(-1);
view->sectionSizeHint(99999);
// TODO how to test the return value?
}
void tst_QHeaderView::logicalIndex()
{
// Test bad arguments
QCOMPARE(view->logicalIndex(-1), -1);
QCOMPARE(view->logicalIndex(99999), -1);
}
void tst_QHeaderView::logicalIndexAt()
{
// Test bad arguments
view->logicalIndexAt(-1);
view->logicalIndexAt(99999);
QCOMPARE(view->logicalIndexAt(0), 0);
QCOMPARE(view->logicalIndexAt(1), 0);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
view->setStretchLastSection(true);
// First item
QCOMPARE(view->logicalIndexAt(0), 0);
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)-1), 0);
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)+1), 1);
// Last item
int last = view->length() - 1;//view->viewport()->height() - 10;
QCOMPARE(view->logicalIndexAt(last), 3);
// Not in widget
int outofbounds = view->length() + 1;//view->viewport()->height() + 1;
QCOMPARE(view->logicalIndexAt(outofbounds), -1);
view->moveSection(0,1);
// First item
QCOMPARE(view->logicalIndexAt(0), 1);
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)-1), 1);
QCOMPARE(view->logicalIndexAt(view->sectionSize(0)+1), 0);
// Last item
QCOMPARE(view->logicalIndexAt(last), 3);
view->moveSection(1,0);
}
void tst_QHeaderView::swapSections()
{
view->swapSections(-1, 1);
view->swapSections(99999, 1);
view->swapSections(1, -1);
view->swapSections(1, 99999);
QVector<int> logical = (QVector<int>() << 0 << 1 << 2 << 3);
QSignalSpy spy1(view, SIGNAL(sectionMoved(int,int,int)));
QCOMPARE(view->sectionsMoved(), false);
view->swapSections(1, 1);
QCOMPARE(view->sectionsMoved(), false);
view->swapSections(1, 2);
QCOMPARE(view->sectionsMoved(), true);
view->swapSections(2, 1);
QCOMPARE(view->sectionsMoved(), true);
for (int i = 0; i < view->count(); ++i)
QCOMPARE(view->logicalIndex(i), logical.at(i));
QCOMPARE(spy1.count(), 4);
logical = (QVector<int>() << 3 << 1 << 2 << 0);
view->swapSections(3, 0);
QCOMPARE(view->sectionsMoved(), true);
for (int j = 0; j < view->count(); ++j)
QCOMPARE(view->logicalIndex(j), logical.at(j));
QCOMPARE(spy1.count(), 6);
}
void tst_QHeaderView::moveSection_data()
{
QTest::addColumn<QList<int> >("hidden");
QTest::addColumn<QList<int> >("from");
QTest::addColumn<QList<int> >("to");
QTest::addColumn<QList<bool> >("moved");
QTest::addColumn<QList<int> >("logical");
QTest::addColumn<int>("count");
QTest::newRow("bad args, no hidden")
<< QList<int>()
<< (QList<int>() << -1 << 1 << 99999 << 1)
<< (QList<int>() << 1 << -1 << 1 << 99999)
<< (QList<bool>() << false << false << false << false)
<< (QList<int>() << 0 << 1 << 2 << 3)
<< 0;
QTest::newRow("good args, no hidden")
<< QList<int>()
<< (QList<int>() << 1 << 1 << 2 << 1)
<< (QList<int>() << 1 << 2 << 1 << 2)
<< (QList<bool>() << false << true << true << true)
<< (QList<int>() << 0 << 2 << 1 << 3)
<< 3;
QTest::newRow("hidden sections")
<< (QList<int>() << 0 << 3)
<< (QList<int>() << 1 << 1 << 2 << 1)
<< (QList<int>() << 1 << 2 << 1 << 2)
<< (QList<bool>() << false << true << true << true)
<< (QList<int>() << 0 << 2 << 1 << 3)
<< 3;
}
void tst_QHeaderView::moveSection()
{
QFETCH(QList<int>, hidden);
QFETCH(QList<int>, from);
QFETCH(QList<int>, to);
QFETCH(QList<bool>, moved);
QFETCH(QList<int>, logical);
QFETCH(int, count);
QCOMPARE(from.count(), to.count());
QCOMPARE(from.count(), moved.count());
QCOMPARE(view->count(), logical.count());
QSignalSpy spy1(view, SIGNAL(sectionMoved(int,int,int)));
QCOMPARE(view->sectionsMoved(), false);
for (int h = 0; h < hidden.count(); ++h)
view->setSectionHidden(hidden.at(h), true);
for (int i = 0; i < from.count(); ++i) {
view->moveSection(from.at(i), to.at(i));
QCOMPARE(view->sectionsMoved(), moved.at(i));
}
for (int j = 0; j < view->count(); ++j)
QCOMPARE(view->logicalIndex(j), logical.at(j));
QCOMPARE(spy1.count(), count);
}
void tst_QHeaderView::resizeAndMoveSection_data()
{
QTest::addColumn<IntList>("logicalIndexes");
QTest::addColumn<IntList>("sizes");
QTest::addColumn<int>("logicalFrom");
QTest::addColumn<int>("logicalTo");
QTest::newRow("resizeAndMove-1")
<< (IntList() << 0 << 1)
<< (IntList() << 20 << 40)
<< 0 << 1;
QTest::newRow("resizeAndMove-2")
<< (IntList() << 0 << 1 << 2 << 3)
<< (IntList() << 20 << 60 << 10 << 80)
<< 0 << 2;
QTest::newRow("resizeAndMove-3")
<< (IntList() << 0 << 1 << 2 << 3)
<< (IntList() << 100 << 60 << 40 << 10)
<< 0 << 3;
QTest::newRow("resizeAndMove-4")
<< (IntList() << 0 << 1 << 2 << 3)
<< (IntList() << 10 << 40 << 80 << 30)
<< 1 << 2;
QTest::newRow("resizeAndMove-5")
<< (IntList() << 2 << 3)
<< (IntList() << 100 << 200)
<< 3 << 2;
}
void tst_QHeaderView::resizeAndMoveSection()
{
QFETCH(IntList, logicalIndexes);
QFETCH(IntList, sizes);
QFETCH(int, logicalFrom);
QFETCH(int, logicalTo);
// Save old visual indexes and sizes
IntList oldVisualIndexes;
IntList oldSizes;
foreach (int logical, logicalIndexes) {
oldVisualIndexes.append(view->visualIndex(logical));
oldSizes.append(view->sectionSize(logical));
}
// Resize sections
for (int i = 0; i < logicalIndexes.size(); ++i) {
int logical = logicalIndexes.at(i);
view->resizeSection(logical, sizes.at(i));
}
// Move sections
int visualFrom = view->visualIndex(logicalFrom);
int visualTo = view->visualIndex(logicalTo);
view->moveSection(visualFrom, visualTo);
QCOMPARE(view->visualIndex(logicalFrom), visualTo);
// Check that sizes are still correct
for (int i = 0; i < logicalIndexes.size(); ++i) {
int logical = logicalIndexes.at(i);
QCOMPARE(view->sectionSize(logical), sizes.at(i));
}
// Move sections back
view->moveSection(visualTo, visualFrom);
// Check that sizes are still correct
for (int i = 0; i < logicalIndexes.size(); ++i) {
int logical = logicalIndexes.at(i);
QCOMPARE(view->sectionSize(logical), sizes.at(i));
}
// Put everything back as it was
for (int i = 0; i < logicalIndexes.size(); ++i) {
int logical = logicalIndexes.at(i);
view->resizeSection(logical, oldSizes.at(i));
QCOMPARE(view->visualIndex(logical), oldVisualIndexes.at(i));
}
}
void tst_QHeaderView::resizeHiddenSection_data()
{
QTest::addColumn<int>("section");
QTest::addColumn<int>("initialSize");
QTest::addColumn<int>("finalSize");
QTest::newRow("section 0 resize 50 to 20")
<< 0 << 50 << 20;
QTest::newRow("section 1 resize 50 to 20")
<< 1 << 50 << 20;
QTest::newRow("section 2 resize 50 to 20")
<< 2 << 50 << 20;
QTest::newRow("section 3 resize 50 to 20")
<< 3 << 50 << 20;
}
void tst_QHeaderView::resizeHiddenSection()
{
QFETCH(int, section);
QFETCH(int, initialSize);
QFETCH(int, finalSize);
view->resizeSection(section, initialSize);
view->setSectionHidden(section, true);
QCOMPARE(view->sectionSize(section), 0);
view->resizeSection(section, finalSize);
QCOMPARE(view->sectionSize(section), 0);
view->setSectionHidden(section, false);
QCOMPARE(view->sectionSize(section), finalSize);
}
void tst_QHeaderView::resizeAndInsertSection_data()
{
QTest::addColumn<int>("section");
QTest::addColumn<int>("size");
QTest::addColumn<int>("insert");
QTest::addColumn<int>("compare");
QTest::addColumn<int>("expected");
QTest::newRow("section 0 size 50 insert 0")
<< 0 << 50 << 0 << 1 << 50;
QTest::newRow("section 1 size 50 insert 1")
<< 0 << 50 << 1 << 0 << 50;
QTest::newRow("section 1 size 50 insert 0")
<< 1 << 50 << 0 << 2 << 50;
}
void tst_QHeaderView::resizeAndInsertSection()
{
QFETCH(int, section);
QFETCH(int, size);
QFETCH(int, insert);
QFETCH(int, compare);
QFETCH(int, expected);
view->setStretchLastSection(false);
view->resizeSection(section, size);
QCOMPARE(view->sectionSize(section), size);
model->insertRow(insert);
QCOMPARE(view->sectionSize(compare), expected);
}
void tst_QHeaderView::resizeWithResizeModes_data()
{
QTest::addColumn<int>("size");
QTest::addColumn<QList<int> >("sections");
QTest::addColumn<QList<int> >("modes");
QTest::addColumn<QList<int> >("expected");
QTest::newRow("stretch first section")
<< 600
<< (QList<int>() << 100 << 100 << 100 << 100)
<< (QList<int>() << ((int)QHeaderView::Stretch)
<< ((int)QHeaderView::Interactive)
<< ((int)QHeaderView::Interactive)
<< ((int)QHeaderView::Interactive))
<< (QList<int>() << 300 << 100 << 100 << 100);
}
void tst_QHeaderView::resizeWithResizeModes()
{
QFETCH(int, size);
QFETCH(QList<int>, sections);
QFETCH(QList<int>, modes);
QFETCH(QList<int>, expected);
view->setStretchLastSection(false);
for (int i = 0; i < sections.count(); ++i) {
view->resizeSection(i, sections.at(i));
view->setSectionResizeMode(i, (QHeaderView::ResizeMode)modes.at(i));
}
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
view->resize(size, size);
for (int j = 0; j < expected.count(); ++j)
QCOMPARE(view->sectionSize(j), expected.at(j));
}
void tst_QHeaderView::moveAndInsertSection_data()
{
QTest::addColumn<int>("from");
QTest::addColumn<int>("to");
QTest::addColumn<int>("insert");
QTest::addColumn<QList<int> >("mapping");
QTest::newRow("move from 1 to 3, insert 0")
<< 1 << 3 << 0 <<(QList<int>() << 0 << 1 << 3 << 4 << 2);
}
void tst_QHeaderView::moveAndInsertSection()
{
QFETCH(int, from);
QFETCH(int, to);
QFETCH(int, insert);
QFETCH(QList<int>, mapping);
view->setStretchLastSection(false);
view->moveSection(from, to);
model->insertRow(insert);
for (int i = 0; i < mapping.count(); ++i)
QCOMPARE(view->logicalIndex(i), mapping.at(i));
}
void tst_QHeaderView::resizeMode()
{
// resizeMode must not be called with an invalid index
int last = view->count() - 1;
view->setSectionResizeMode(QHeaderView::Interactive);
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Interactive);
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Interactive);
view->setSectionResizeMode(QHeaderView::Stretch);
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Stretch);
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Stretch);
view->setSectionResizeMode(QHeaderView::Custom);
QCOMPARE(view->sectionResizeMode(last), QHeaderView::Custom);
QCOMPARE(view->sectionResizeMode(1), QHeaderView::Custom);
// test when sections have been moved
view->setStretchLastSection(false);
for (int i=0; i < (view->count() - 1); ++i)
view->setSectionResizeMode(i, QHeaderView::Interactive);
int logicalIndex = view->count() / 2;
view->setSectionResizeMode(logicalIndex, QHeaderView::Stretch);
view->moveSection(view->visualIndex(logicalIndex), 0);
for (int i=0; i < (view->count() - 1); ++i) {
if (i == logicalIndex)
QCOMPARE(view->sectionResizeMode(i), QHeaderView::Stretch);
else
QCOMPARE(view->sectionResizeMode(i), QHeaderView::Interactive);
}
}
void tst_QHeaderView::resizeSection_data()
{
QTest::addColumn<int>("initial");
QTest::addColumn<QList<int> >("logical");
QTest::addColumn<QList<int> >("size");
QTest::addColumn<QList<int> >("mode");
QTest::addColumn<int>("resized");
QTest::addColumn<QList<int> >("expected");
QTest::newRow("bad args")
<< 100
<< (QList<int>() << -1 << -1 << 99999 << 99999 << 4)
<< (QList<int>() << -1 << 0 << 99999 << -1 << -1)
<< (QList<int>()
<< int(QHeaderView::Interactive)
<< int(QHeaderView::Interactive)
<< int(QHeaderView::Interactive)
<< int(QHeaderView::Interactive))
<< 0
<< (QList<int>() << 0 << 0 << 0 << 0 << 0);
}
void tst_QHeaderView::resizeSection()
{
QFETCH(int, initial);
QFETCH(QList<int>, logical);
QFETCH(QList<int>, size);
QFETCH(QList<int>, mode);
QFETCH(int, resized);
QFETCH(QList<int>, expected);
view->resize(400, 400);
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
view->setSectionsMovable(true);
view->setStretchLastSection(false);
for (int i = 0; i < logical.count(); ++i)
if (logical.at(i) > -1 && logical.at(i) < view->count()) // for now
view->setSectionResizeMode(logical.at(i), (QHeaderView::ResizeMode)mode.at(i));
for (int j = 0; j < logical.count(); ++j)
view->resizeSection(logical.at(j), initial);
QSignalSpy spy(view, SIGNAL(sectionResized(int,int,int)));
for (int k = 0; k < logical.count(); ++k)
view->resizeSection(logical.at(k), size.at(k));
QCOMPARE(spy.count(), resized);
for (int l = 0; l < logical.count(); ++l)
QCOMPARE(view->sectionSize(logical.at(l)), expected.at(l));
}
void tst_QHeaderView::highlightSections()
{
view->setHighlightSections(true);
QCOMPARE(view->highlightSections(), true);
view->setHighlightSections(false);
QCOMPARE(view->highlightSections(), false);
}
void tst_QHeaderView::showSortIndicator()
{
view->setSortIndicatorShown(true);
QCOMPARE(view->isSortIndicatorShown(), true);
QCOMPARE(view->sortIndicatorOrder(), Qt::DescendingOrder);
view->setSortIndicator(1, Qt::AscendingOrder);
QCOMPARE(view->sortIndicatorOrder(), Qt::AscendingOrder);
view->setSortIndicator(1, Qt::DescendingOrder);
QCOMPARE(view->sortIndicatorOrder(), Qt::DescendingOrder);
view->setSortIndicatorShown(false);
QCOMPARE(view->isSortIndicatorShown(), false);
view->setSortIndicator(999999, Qt::DescendingOrder);
// Don't segfault baby :)
view->setSortIndicatorShown(true);
view->setSortIndicator(0, Qt::DescendingOrder);
// Don't assert baby :)
}
void tst_QHeaderView::sortIndicatorTracking()
{
QtTestModel model;
model.rows = model.cols = 10;
QHeaderView hv(Qt::Horizontal);
hv.setModel(&model);
hv.show();
hv.setSortIndicatorShown(true);
hv.setSortIndicator(1, Qt::DescendingOrder);
model.removeOneColumn(8);
QCOMPARE(hv.sortIndicatorSection(), 1);
model.removeOneColumn(2);
QCOMPARE(hv.sortIndicatorSection(), 1);
model.insertOneColumn(2);
QCOMPARE(hv.sortIndicatorSection(), 1);
model.insertOneColumn(1);
QCOMPARE(hv.sortIndicatorSection(), 2);
model.removeOneColumn(0);
QCOMPARE(hv.sortIndicatorSection(), 1);
model.removeOneColumn(1);
QCOMPARE(hv.sortIndicatorSection(), -1);
}
void tst_QHeaderView::removeAndInsertRow()
{
// Check if logicalIndex returns the correct value after we have removed a row
// we might as well te
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
while (model->removeRow(0)) {
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
}
int pass = 0;
for (pass = 0; pass < 5; pass++) {
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
model->insertRow(0);
}
while (model->removeRows(0, 2)) {
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
}
for (pass = 0; pass < 3; pass++) {
model->insertRows(0, 2);
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
}
for (pass = 0; pass < 3; pass++) {
model->insertRows(3, 2);
for (int i = 0; i < model->rowCount(); ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
}
// Insert at end
for (pass = 0; pass < 3; pass++) {
int rowCount = model->rowCount();
model->insertRows(rowCount, 1);
for (int i = 0; i < rowCount; ++i) {
QCOMPARE(i, view->logicalIndex(i));
}
}
}
void tst_QHeaderView::unhideSection()
{
// You should not necessarily expect the same size back again, so the best test we can do is to test if it is larger than 0 after a unhide.
QCOMPARE(view->sectionsHidden(), false);
view->setSectionHidden(0, true);
QCOMPARE(view->sectionsHidden(), true);
QCOMPARE(view->sectionSize(0), 0);
view->setSectionResizeMode(QHeaderView::Interactive);
view->setSectionHidden(0, false);
QVERIFY(view->sectionSize(0) > 0);
view->setSectionHidden(0, true);
QCOMPARE(view->sectionSize(0), 0);
view->setSectionHidden(0, true);
QCOMPARE(view->sectionSize(0), 0);
view->setSectionResizeMode(QHeaderView::Stretch);
view->setSectionHidden(0, false);
QVERIFY(view->sectionSize(0) > 0);
}
void tst_QHeaderView::testEvent()
{
protected_QHeaderView x(Qt::Vertical);
x.testEvent();
protected_QHeaderView y(Qt::Horizontal);
y.testEvent();
}
void protected_QHeaderView::testEvent()
{
// No crashy please
QHoverEvent enterEvent(QEvent::HoverEnter, QPoint(), QPoint());
event(&enterEvent);
QHoverEvent eventLeave(QEvent::HoverLeave, QPoint(), QPoint());
event(&eventLeave);
QHoverEvent eventMove(QEvent::HoverMove, QPoint(), QPoint());
event(&eventMove);
}
void tst_QHeaderView::headerDataChanged()
{
// This shouldn't asserver because view is Vertical
view->headerDataChanged(Qt::Horizontal, -1, -1);
#if 0
// This will assert
view->headerDataChanged(Qt::Vertical, -1, -1);
#endif
// No crashing please
view->headerDataChanged(Qt::Horizontal, 0, 1);
view->headerDataChanged(Qt::Vertical, 0, 1);
}
void tst_QHeaderView::currentChanged()
{
view->setCurrentIndex(QModelIndex());
}
void tst_QHeaderView::horizontalOffset()
{
protected_QHeaderView x(Qt::Vertical);
x.testhorizontalOffset();
protected_QHeaderView y(Qt::Horizontal);
y.testhorizontalOffset();
}
void tst_QHeaderView::verticalOffset()
{
protected_QHeaderView x(Qt::Vertical);
x.testverticalOffset();
protected_QHeaderView y(Qt::Horizontal);
y.testverticalOffset();
}
void protected_QHeaderView::testhorizontalOffset()
{
if(orientation() == Qt::Horizontal){
QCOMPARE(horizontalOffset(), 0);
setOffset(10);
QCOMPARE(horizontalOffset(), 10);
}
else
QCOMPARE(horizontalOffset(), 0);
}
void protected_QHeaderView::testverticalOffset()
{
if(orientation() == Qt::Vertical){
QCOMPARE(verticalOffset(), 0);
setOffset(10);
QCOMPARE(verticalOffset(), 10);
}
else
QCOMPARE(verticalOffset(), 0);
}
void tst_QHeaderView::stretchSectionCount()
{
view->setStretchLastSection(false);
QCOMPARE(view->stretchSectionCount(), 0);
view->setStretchLastSection(true);
QCOMPARE(view->stretchSectionCount(), 0);
view->setSectionResizeMode(0, QHeaderView::Stretch);
QCOMPARE(view->stretchSectionCount(), 1);
}
void tst_QHeaderView::hiddenSectionCount()
{
model->clear();
model->insertRows(0, 10);
// Hide every other one
for (int i=0; i<10; i++)
view->setSectionHidden(i, (i & 1) == 0);
QCOMPARE(view->hiddenSectionCount(), 5);
view->setSectionResizeMode(QHeaderView::Stretch);
QCOMPARE(view->hiddenSectionCount(), 5);
// Remove some rows and make sure they are now still counted
model->removeRow(9);
model->removeRow(8);
model->removeRow(7);
model->removeRow(6);
QCOMPARE(view->count(), 6);
QCOMPARE(view->hiddenSectionCount(), 3);
model->removeRows(0,5);
QCOMPARE(view->count(), 1);
QCOMPARE(view->hiddenSectionCount(), 0);
QVERIFY(view->count() >= view->hiddenSectionCount());
}
void tst_QHeaderView::focusPolicy()
{
QHeaderView view(Qt::Horizontal);
QCOMPARE(view.focusPolicy(), Qt::NoFocus);
QTreeWidget widget;
QCOMPARE(widget.header()->focusPolicy(), Qt::NoFocus);
QVERIFY(!widget.focusProxy());
QVERIFY(!widget.hasFocus());
QVERIFY(!widget.header()->focusProxy());
QVERIFY(!widget.header()->hasFocus());
widget.show();
widget.setFocus(Qt::OtherFocusReason);
QApplication::setActiveWindow(&widget);
widget.activateWindow();
QVERIFY(QTest::qWaitForWindowActive(&widget));
QVERIFY(widget.hasFocus());
QVERIFY(!widget.header()->hasFocus());
widget.setFocusPolicy(Qt::NoFocus);
widget.clearFocus();
QTRY_VERIFY(!widget.hasFocus());
QVERIFY(!widget.header()->hasFocus());
QTest::keyPress(&widget, Qt::Key_Tab);
qApp->processEvents();
qApp->processEvents();
QVERIFY(!widget.hasFocus());
QVERIFY(!widget.header()->hasFocus());
}
class SimpleModel : public QAbstractItemModel
{
Q_OBJECT
public:
SimpleModel( QObject* parent=0)
: QAbstractItemModel(parent),
m_col_count(3) {}
QModelIndex parent(const QModelIndex &/*child*/) const
{
return QModelIndex();
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const
{
return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
}
int rowCount(const QModelIndex & /*parent*/ = QModelIndex()) const
{
return 8;
}
int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const
{
return m_col_count;
}
QVariant data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
if (role == Qt::DisplayRole) {
return QString::number(index.row()) + QLatin1Char(',') + QString::number(index.column());
}
return QVariant();
}
void setColumnCount( int c )
{
m_col_count = c;
}
private:
int m_col_count;
};
void tst_QHeaderView::moveSectionAndReset()
{
SimpleModel m;
QHeaderView v(Qt::Horizontal);
v.setModel(&m);
int cc = 2;
for (cc = 2; cc < 4; ++cc) {
m.setColumnCount(cc);
int movefrom = 0;
int moveto;
for (moveto = 1; moveto < cc; ++moveto) {
v.moveSection(movefrom, moveto);
m.setColumnCount(cc - 1);
v.reset();
for (int i = 0; i < cc - 1; ++i) {
QCOMPARE(v.logicalIndex(v.visualIndex(i)), i);
}
}
}
}
void tst_QHeaderView::moveSectionAndRemove()
{
QStandardItemModel m;
QHeaderView v(Qt::Horizontal);
v.setModel(&m);
v.model()->insertColumns(0, 3);
v.moveSection(0, 1);
QCOMPARE(v.count(), 3);
v.model()->removeColumns(0, v.model()->columnCount());
QCOMPARE(v.count(), 0);
}
static QByteArray savedState()
{
QStandardItemModel m(4, 4);
QHeaderView h1(Qt::Horizontal);
h1.setModel(&m);
h1.setMinimumSectionSize(0); // system default min size can be to large
h1.swapSections(0, 2);
h1.resizeSection(1, 10);
h1.setSortIndicatorShown(true);
h1.setSortIndicator(2, Qt::DescendingOrder);
h1.setSectionHidden(3, true);
return h1.saveState();
}
void tst_QHeaderView::saveRestore()
{
QStandardItemModel m(4, 4);
const QByteArray s1 = savedState();
QHeaderView h2(Qt::Vertical);
QSignalSpy spy(&h2, SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)));
h2.setModel(&m);
QVERIFY(h2.restoreState(s1));
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toInt(), 2);
QCOMPARE(h2.logicalIndex(0), 2);
QCOMPARE(h2.logicalIndex(2), 0);
QCOMPARE(h2.sectionSize(1), 10);
QCOMPARE(h2.sortIndicatorSection(), 2);
QCOMPARE(h2.sortIndicatorOrder(), Qt::DescendingOrder);
QCOMPARE(h2.isSortIndicatorShown(), true);
QVERIFY(!h2.isSectionHidden(2));
QVERIFY(h2.isSectionHidden(3));
QCOMPARE(h2.hiddenSectionCount(), 1);
QByteArray s2 = h2.saveState();
QCOMPARE(s1, s2);
QVERIFY(!h2.restoreState(QByteArrayLiteral("Garbage")));
}
void tst_QHeaderView::restoreQt4State()
{
// QTBUG-40462
// Setting from Qt4, where information about multiple sections were grouped together in one
// sectionItem object
QStandardItemModel m(4, 10);
QHeaderView h2(Qt::Vertical);
QByteArray settings_qt4 =
QByteArray::fromHex("000000ff00000000000000010000000100000000010000000000000000000000000000"
"0000000003e80000000a0101000100000000000000000000000064ffffffff00000081"
"0000000000000001000003e80000000a00000000");
QVERIFY(h2.restoreState(settings_qt4));
int sectionItemsLengthTotal = 0;
for (int i = 0; i < h2.count(); ++i)
sectionItemsLengthTotal += h2.sectionSize(i);
QCOMPARE(sectionItemsLengthTotal, h2.length());
// Buggy setting where sum(sectionItems) != length. Check false is returned and this corrupted
// state isn't restored
QByteArray settings_buggy_length =
QByteArray::fromHex("000000ff000000000000000100000000000000050100000000000000000000000a4000"
"000000010000000600000258000000fb0000000a010100010000000000000000000000"
"0064ffffffff00000081000000000000000a000000d30000000100000000000000c800"
"000001000000000000008000000001000000000000005c00000001000000000000003c"
"0000000100000000000002580000000100000000000000000000000100000000000002"
"580000000100000000000002580000000100000000000003c000000001000000000000"
"03e8");
int old_length = h2.length();
QByteArray old_state = h2.saveState();
// Check setting is correctly recognized as corrupted
QVERIFY(!h2.restoreState(settings_buggy_length));
// Check nothing has been actually restored
QCOMPARE(h2.length(), old_length);
QCOMPARE(h2.saveState(), old_state);
}
void tst_QHeaderView::restoreToMoreColumns()
{
// Restore state onto a model with more columns
const QByteArray s1 = savedState();
QHeaderView h4(Qt::Horizontal);
QStandardItemModel fiveColumnsModel(1, 5);
h4.setModel(&fiveColumnsModel);
QCOMPARE(fiveColumnsModel.columnCount(), 5);
QCOMPARE(h4.count(), 5);
QVERIFY(h4.restoreState(s1));
QCOMPARE(fiveColumnsModel.columnCount(), 5);
QCOMPARE(h4.count(), 5);
QCOMPARE(h4.sectionSize(1), 10);
for (int i = 0; i < h4.count(); ++i)
QVERIFY(h4.sectionSize(i) > 0 || h4.isSectionHidden(i));
QVERIFY(!h4.isSectionHidden(2));
QVERIFY(h4.isSectionHidden(3));
QCOMPARE(h4.hiddenSectionCount(), 1);
QCOMPARE(h4.sortIndicatorSection(), 2);
QCOMPARE(h4.sortIndicatorOrder(), Qt::DescendingOrder);
QCOMPARE(h4.logicalIndex(0), 2);
QCOMPARE(h4.logicalIndex(1), 1);
QCOMPARE(h4.logicalIndex(2), 0);
QCOMPARE(h4.visualIndex(0), 2);
QCOMPARE(h4.visualIndex(1), 1);
QCOMPARE(h4.visualIndex(2), 0);
// Repainting shouldn't crash
h4.show();
QVERIFY(QTest::qWaitForWindowExposed(&h4));
}
void tst_QHeaderView::restoreToMoreColumnsNoMovedColumns()
{
// Given a model with 2 columns, for saving state
QHeaderView h1(Qt::Horizontal);
QStandardItemModel model1(1, 2);
h1.setModel(&model1);
QCOMPARE(h1.visualIndex(0), 0);
QCOMPARE(h1.visualIndex(1), 1);
QCOMPARE(h1.logicalIndex(0), 0);
QCOMPARE(h1.logicalIndex(1), 1);
const QByteArray savedState = h1.saveState();
// And a model with 3 columns, to apply that state upon
QHeaderView h2(Qt::Horizontal);
QStandardItemModel model2(1, 3);
h2.setModel(&model2);
QCOMPARE(h2.visualIndex(0), 0);
QCOMPARE(h2.visualIndex(1), 1);
QCOMPARE(h2.visualIndex(2), 2);
QCOMPARE(h2.logicalIndex(0), 0);
QCOMPARE(h2.logicalIndex(1), 1);
QCOMPARE(h2.logicalIndex(2), 2);
// When calling restoreState()
QVERIFY(h2.restoreState(savedState));
// Then the index mapping should still be as default
QCOMPARE(h2.visualIndex(0), 0);
QCOMPARE(h2.visualIndex(1), 1);
QCOMPARE(h2.visualIndex(2), 2);
QCOMPARE(h2.logicalIndex(0), 0);
QCOMPARE(h2.logicalIndex(1), 1);
QCOMPARE(h2.logicalIndex(2), 2);
// And repainting shouldn't crash
h2.show();
QVERIFY(QTest::qWaitForWindowExposed(&h2));
}
void tst_QHeaderView::restoreBeforeSetModel()
{
QHeaderView h2(Qt::Horizontal);
const QByteArray s1 = savedState();
// First restore
QVERIFY(h2.restoreState(s1));
// Then setModel
QStandardItemModel model(4, 4);
h2.setModel(&model);
// Check the result
QCOMPARE(h2.logicalIndex(0), 2);
QCOMPARE(h2.logicalIndex(2), 0);
QCOMPARE(h2.sectionSize(1), 10);
QCOMPARE(h2.sortIndicatorSection(), 2);
QCOMPARE(h2.sortIndicatorOrder(), Qt::DescendingOrder);
QCOMPARE(h2.isSortIndicatorShown(), true);
QVERIFY(!h2.isSectionHidden(2));
QVERIFY(h2.isSectionHidden(3));
QCOMPARE(h2.hiddenSectionCount(), 1);
}
void tst_QHeaderView::defaultSectionSizeTest()
{
// Setup
QTableView qtv;
QHeaderView *hv = qtv.verticalHeader();
hv->setDefaultSectionSize(99); // Set it to a value different from defaultSize.
QStandardItemModel amodel(4, 4);
qtv.setModel(&amodel);
QCOMPARE(hv->sectionSize(0), 99);
QCOMPARE(hv->visualIndexAt(50), 0); // <= also make sure that indexes are calculated
hv->setDefaultSectionSize(40); // Set it to a value different from defaultSize.
QCOMPARE(hv->visualIndexAt(50), 1);
const int defaultSize = 26;
hv->setDefaultSectionSize(defaultSize + 1); // Set it to a value different from defaultSize.
// no hidden Sections
hv->resizeSection(1, 0);
hv->setDefaultSectionSize(defaultSize);
QCOMPARE(hv->sectionSize(1), defaultSize);
// with hidden sections
hv->resizeSection(1, 0);
hv->hideSection(2);
hv->setDefaultSectionSize(defaultSize);
QVERIFY(hv->sectionSize(0) == defaultSize); // trivial case.
QVERIFY(hv->sectionSize(1) == defaultSize); // just sized 0. Now it should be 10
QVERIFY(hv->sectionSize(2) == 0); // section is hidden. It should not be resized.
}
class TestHeaderViewStyle : public QProxyStyle
{
public:
TestHeaderViewStyle() : horizontalSectionSize(100) {}
int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const override
{
if (metric == QStyle::PM_HeaderDefaultSectionSizeHorizontal)
return horizontalSectionSize;
else
return QProxyStyle::pixelMetric(metric, option, widget);
}
int horizontalSectionSize;
};
void tst_QHeaderView::defaultSectionSizeTestStyles()
{
TestHeaderViewStyle style1;
TestHeaderViewStyle style2;
style1.horizontalSectionSize = 100;
style2.horizontalSectionSize = 200;
QHeaderView hv(Qt::Horizontal);
hv.setStyle(&style1);
QCOMPARE(hv.defaultSectionSize(), style1.horizontalSectionSize);
hv.setStyle(&style2);
QCOMPARE(hv.defaultSectionSize(), style2.horizontalSectionSize);
hv.setDefaultSectionSize(70);
QCOMPARE(hv.defaultSectionSize(), 70);
hv.setStyle(&style1);
QCOMPARE(hv.defaultSectionSize(), 70);
hv.resetDefaultSectionSize();
QCOMPARE(hv.defaultSectionSize(), style1.horizontalSectionSize);
hv.setStyle(&style2);
QCOMPARE(hv.defaultSectionSize(), style2.horizontalSectionSize);
}
void tst_QHeaderView::defaultAlignment_data()
{
QTest::addColumn<int>("direction");
QTest::addColumn<int>("initial");
QTest::addColumn<int>("alignment");
QTest::newRow("horizontal right aligned")
<< int(Qt::Horizontal)
<< int(Qt::AlignCenter)
<< int(Qt::AlignRight);
QTest::newRow("horizontal left aligned")
<< int(Qt::Horizontal)
<< int(Qt::AlignCenter)
<< int(Qt::AlignLeft);
QTest::newRow("vertical right aligned")
<< int(Qt::Vertical)
<< int(Qt::AlignLeft|Qt::AlignVCenter)
<< int(Qt::AlignRight);
QTest::newRow("vertical left aligned")
<< int(Qt::Vertical)
<< int(Qt::AlignLeft|Qt::AlignVCenter)
<< int(Qt::AlignLeft);
}
void tst_QHeaderView::defaultAlignment()
{
QFETCH(int, direction);
QFETCH(int, initial);
QFETCH(int, alignment);
SimpleModel m;
QHeaderView header((Qt::Orientation)direction);
header.setModel(&m);
QCOMPARE(header.defaultAlignment(), (Qt::Alignment)initial);
header.setDefaultAlignment((Qt::Alignment)alignment);
QCOMPARE(header.defaultAlignment(), (Qt::Alignment)alignment);
}
void tst_QHeaderView::globalResizeMode_data()
{
QTest::addColumn<int>("direction");
QTest::addColumn<int>("mode");
QTest::addColumn<int>("insert");
QTest::newRow("horizontal ResizeToContents 0")
<< int(Qt::Horizontal)
<< int(QHeaderView::ResizeToContents)
<< 0;
}
void tst_QHeaderView::globalResizeMode()
{
QFETCH(int, direction);
QFETCH(int, mode);
QFETCH(int, insert);
QStandardItemModel m(4, 4);
QHeaderView h((Qt::Orientation)direction);
h.setModel(&m);
h.setSectionResizeMode((QHeaderView::ResizeMode)mode);
m.insertRow(insert);
for (int i = 0; i < h.count(); ++i)
QCOMPARE(h.sectionResizeMode(i), (QHeaderView::ResizeMode)mode);
}
void tst_QHeaderView::sectionPressedSignal_data()
{
QTest::addColumn<int>("direction");
QTest::addColumn<bool>("clickable");
QTest::addColumn<int>("count");
QTest::newRow("horizontal unclickable 0")
<< int(Qt::Horizontal)
<< false
<< 0;
QTest::newRow("horizontal clickable 1")
<< int(Qt::Horizontal)
<< true
<< 1;
}
void tst_QHeaderView::sectionPressedSignal()
{
QFETCH(int, direction);
QFETCH(bool, clickable);
QFETCH(int, count);
QStandardItemModel m(4, 4);
QHeaderView h((Qt::Orientation)direction);
h.setModel(&m);
h.show();
h.setSectionsClickable(clickable);
QSignalSpy spy(&h, SIGNAL(sectionPressed(int)));
QCOMPARE(spy.count(), 0);
QTest::mousePress(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
QCOMPARE(spy.count(), count);
}
void tst_QHeaderView::sectionClickedSignal()
{
QFETCH(int, direction);
QFETCH(bool, clickable);
QFETCH(int, count);
QStandardItemModel m(4, 4);
QHeaderView h((Qt::Orientation)direction);
h.setModel(&m);
h.show();
h.setSectionsClickable(clickable);
h.setSortIndicatorShown(true);
QSignalSpy spy(&h, SIGNAL(sectionClicked(int)));
QSignalSpy spy2(&h, SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)));
QCOMPARE(spy.count(), 0);
QCOMPARE(spy2.count(), 0);
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
QCOMPARE(spy.count(), count);
QCOMPARE(spy2.count(), count);
//now let's try with the sort indicator hidden (the result should be the same
spy.clear();
spy2.clear();
h.setSortIndicatorShown(false);
QTest::mouseClick(h.viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(5, 5));
QCOMPARE(spy.count(), count);
QCOMPARE(spy2.count(), count);
}
void tst_QHeaderView::defaultSectionSize_data()
{
QTest::addColumn<int>("direction");
QTest::addColumn<int>("oldDefaultSize");
QTest::addColumn<int>("newDefaultSize");
//QTest::newRow("horizontal,-5") << int(Qt::Horizontal) << 100 << -5;
QTest::newRow("horizontal, 0") << int(Qt::Horizontal) << 100 << 0;
QTest::newRow("horizontal, 5") << int(Qt::Horizontal) << 100 << 5;
QTest::newRow("horizontal,25") << int(Qt::Horizontal) << 100 << 5;
}
void tst_QHeaderView::defaultSectionSize()
{
QFETCH(int, direction);
QFETCH(int, oldDefaultSize);
QFETCH(int, newDefaultSize);
QStandardItemModel m(4, 4);
QHeaderView h((Qt::Orientation)direction);
h.setModel(&m);
QCOMPARE(h.defaultSectionSize(), oldDefaultSize);
h.setDefaultSectionSize(newDefaultSize);
QCOMPARE(h.defaultSectionSize(), newDefaultSize);
h.reset();
for (int i = 0; i < h.count(); ++i)
QCOMPARE(h.sectionSize(i), newDefaultSize);
}
void tst_QHeaderView::hideAndInsert_data()
{
QTest::addColumn<int>("direction");
QTest::addColumn<int>("hide");
QTest::addColumn<int>("insert");
QTest::addColumn<int>("hidden");
QTest::newRow("horizontal, 0, 0") << int(Qt::Horizontal) << 0 << 0 << 1;
}
void tst_QHeaderView::hideAndInsert()
{
QFETCH(int, direction);
QFETCH(int, hide);
QFETCH(int, insert);
QFETCH(int, hidden);
QStandardItemModel m(4, 4);
QHeaderView h((Qt::Orientation)direction);
h.setModel(&m);
h.setSectionHidden(hide, true);
if (direction == Qt::Vertical)
m.insertRow(insert);
else
m.insertColumn(insert);
for (int i = 0; i < h.count(); ++i)
if (i != hidden)
QCOMPARE(h.isSectionHidden(i), false);
else
QCOMPARE(h.isSectionHidden(i), true);
}
void tst_QHeaderView::removeSection()
{
//test that removing a hidden section gives the expected result: the next row should be hidden
//(see task
const int hidden = 3; //section that will be hidden
const QStringList list = QStringList() << "0" << "1" << "2" << "3" << "4" << "5" << "6";
QStringListModel model( list );
QHeaderView view(Qt::Vertical);
view.setModel(&model);
view.hideSection(hidden);
view.hideSection(1);
model.removeRow(1);
view.show();
for(int i = 0; i < view.count(); i++) {
if (i == (hidden-1)) { //-1 because we removed a row in the meantime
QCOMPARE(view.sectionSize(i), 0);
QVERIFY(view.isSectionHidden(i));
} else {
QCOMPARE(view.sectionSize(i), view.defaultSectionSize() );
QVERIFY(!view.isSectionHidden(i));
}
}
}
void tst_QHeaderView::preserveHiddenSectionWidth()
{
const QStringList list = QStringList() << "0" << "1" << "2" << "3";
QStringListModel model( list );
QHeaderView view(Qt::Vertical);
view.setModel(&model);
view.resizeSection(0, 100);
view.resizeSection(1, 10);
view.resizeSection(2, 50);
view.setSectionResizeMode(3, QHeaderView::Stretch);
view.show();
view.hideSection(2);
model.removeRow(1);
view.showSection(1);
QCOMPARE(view.sectionSize(0), 100);
QCOMPARE(view.sectionSize(1), 50);
view.hideSection(1);
model.insertRow(1);
view.showSection(2);
QCOMPARE(view.sectionSize(0), 100);
QCOMPARE(view.sectionSize(1), view.defaultSectionSize());
QCOMPARE(view.sectionSize(2), 50);
}
void tst_QHeaderView::invisibleStretchLastSection()
{
int count = 6;
QStandardItemModel model(1, count);
QHeaderView view(Qt::Horizontal);
view.setModel(&model);
int height = view.height();
view.resize(view.defaultSectionSize() * (count / 2), height); // don't show all sections
view.show();
view.setStretchLastSection(true);
// stretch section is not visible; it should not be stretched
for (int i = 0; i < count; ++i)
QCOMPARE(view.sectionSize(i), view.defaultSectionSize());
view.resize(view.defaultSectionSize() * (count + 1), height); // give room to stretch
// stretch section is visible; it should be stretched
for (int i = 0; i < count - 1; ++i)
QCOMPARE(view.sectionSize(i), view.defaultSectionSize());
QCOMPARE(view.sectionSize(count - 1), view.defaultSectionSize() * 2);
}
void tst_QHeaderView::noSectionsWithNegativeSize()
{
QStandardItemModel m(4, 4);
QHeaderView h(Qt::Horizontal);
h.setModel(&m);
h.resizeSection(1, -5);
QVERIFY(h.sectionSize(1) >= 0); // Sections with negative sizes not well defined.
}
void tst_QHeaderView::emptySectionSpan()
{
QHeaderViewPrivate::SectionItem section;
QCOMPARE(section.sectionSize(), 0);
}
void tst_QHeaderView::task236450_hidden_data()
{
QTest::addColumn<QList<int> >("hide1");
QTest::addColumn<QList<int> >("hide2");
QTest::newRow("set 1") << (QList<int>() << 1 << 3)
<< (QList<int>() << 1 << 5);
QTest::newRow("set 2") << (QList<int>() << 2 << 3)
<< (QList<int>() << 1 << 5);
QTest::newRow("set 3") << (QList<int>() << 0 << 2 << 4)
<< (QList<int>() << 2 << 3 << 5);
}
void tst_QHeaderView::task236450_hidden()
{
QFETCH(QList<int>, hide1);
QFETCH(QList<int>, hide2);
const QStringList list = QStringList() << "0" << "1" << "2" << "3" << "4" << "5";
QStringListModel model( list );
protected_QHeaderView view(Qt::Vertical);
view.setModel(&model);
view.show();
foreach (int i, hide1)
view.hideSection(i);
QCOMPARE(view.hiddenSectionCount(), hide1.count());
for (int i = 0; i < 6; i++) {
QCOMPARE(!view.isSectionHidden(i), !hide1.contains(i));
}
view.setDefaultSectionSize(2);
view.scheduleDelayedItemsLayout();
view.executeDelayedItemsLayout(); //force to do a relayout
QCOMPARE(view.hiddenSectionCount(), hide1.count());
for (int i = 0; i < 6; i++) {
QCOMPARE(!view.isSectionHidden(i), !hide1.contains(i));
view.setSectionHidden(i, hide2.contains(i));
}
QCOMPARE(view.hiddenSectionCount(), hide2.count());
for (int i = 0; i < 6; i++) {
QCOMPARE(!view.isSectionHidden(i), !hide2.contains(i));
}
}
void tst_QHeaderView::task248050_hideRow()
{
//this is the sequence of events that make the task fail
protected_QHeaderView header(Qt::Vertical);
QStandardItemModel model(0, 1);
header.setMinimumSectionSize(0); // system default min size can be to large
header.setStretchLastSection(false);
header.setDefaultSectionSize(17);
header.setModel(&model);
header.doItemsLayout();
model.setRowCount(3);
QCOMPARE(header.sectionPosition(2), 17*2);
header.hideSection(1);
QCOMPARE(header.sectionPosition(2), 17);
QTest::qWait(100);
//the size of the section shouldn't have changed
QCOMPARE(header.sectionPosition(2), 17);
}
//returns 0 if everything is fine.
static int checkHeaderViewOrder(QHeaderView *view, const QVector<int> &expected)
{
if (view->count() != expected.count())
return 1;
for (int i = 0; i < expected.count(); i++) {
if (view->logicalIndex(i) != expected.at(i))
return i + 10;
if (view->visualIndex(expected.at(i)) != i)
return i + 100;
}
return 0;
}
void tst_QHeaderView::QTBUG6058_reset()
{
QStringListModel model1( QStringList() << "0" << "1" << "2" << "3" << "4" << "5" );
QStringListModel model2( QStringList() << "a" << "b" << "c" );
QSortFilterProxyModel proxy;
QHeaderView view(Qt::Vertical);
view.setModel(&proxy);
view.show();
QTest::qWait(20);
proxy.setSourceModel(&model1);
QApplication::processEvents();
view.swapSections(0,2);
view.swapSections(1,4);
QApplication::processEvents();
QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 4 << 0 << 3 << 1 << 5) , 0);
proxy.setSourceModel(&model2);
QApplication::processEvents();
QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 0 << 1 ) , 0);
proxy.setSourceModel(&model1);
QApplication::processEvents();
QCOMPARE(checkHeaderViewOrder(&view, QVector<int>() << 2 << 0 << 1 << 3 << 4 << 5 ) , 0);
}
void tst_QHeaderView::QTBUG7833_sectionClicked()
{
QTableView tv;
QStandardItemModel *sim = new QStandardItemModel(&tv);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(&tv);
proxyModel->setSourceModel(sim);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
QList<QStandardItem *> row;
for (int i = 0; i < 12; i++)
row.append(new QStandardItem(QString(QLatin1Char('A' + i))));
sim->appendRow(row);
row.clear();
for (int i = 12; i > 0; i--)
row.append(new QStandardItem(QString(QLatin1Char('A' + i))));
sim->appendRow(row);
tv.setSortingEnabled(true);
tv.horizontalHeader()->setSortIndicatorShown(true);
tv.horizontalHeader()->setSectionsClickable(true);
tv.horizontalHeader()->setStretchLastSection(true);
tv.horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
tv.setModel(proxyModel);
tv.setColumnHidden(5, true);
tv.setColumnHidden(6, true);
tv.horizontalHeader()->swapSections(8, 10);
tv.sortByColumn(1, Qt::AscendingOrder);
QSignalSpy clickedSpy(tv.horizontalHeader(), SIGNAL(sectionClicked(int)));
QSignalSpy pressedSpy(tv.horizontalHeader(), SIGNAL(sectionPressed(int)));
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
QPoint(tv.horizontalHeader()->sectionViewportPosition(11) + tv.horizontalHeader()->sectionSize(11)/2, 5));
QCOMPARE(clickedSpy.count(), 1);
QCOMPARE(pressedSpy.count(), 1);
QCOMPARE(clickedSpy.at(0).at(0).toInt(), 11);
QCOMPARE(pressedSpy.at(0).at(0).toInt(), 11);
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
QPoint(tv.horizontalHeader()->sectionViewportPosition(8) + tv.horizontalHeader()->sectionSize(0)/2, 5));
QCOMPARE(clickedSpy.count(), 2);
QCOMPARE(pressedSpy.count(), 2);
QCOMPARE(clickedSpy.at(1).at(0).toInt(), 8);
QCOMPARE(pressedSpy.at(1).at(0).toInt(), 8);
QTest::mouseClick(tv.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier,
QPoint(tv.horizontalHeader()->sectionViewportPosition(0) + tv.horizontalHeader()->sectionSize(0)/2, 5));
QCOMPARE(clickedSpy.count(), 3);
QCOMPARE(pressedSpy.count(), 3);
QCOMPARE(clickedSpy.at(2).at(0).toInt(), 0);
QCOMPARE(pressedSpy.at(2).at(0).toInt(), 0);
}
void tst_QHeaderView::QTBUG8650_crashOnInsertSections()
{
QStringList headerLabels;
QHeaderView view(Qt::Horizontal);
QStandardItemModel model(2,2);
view.setModel(&model);
view.moveSection(1, 0);
view.hideSection(0);
QList<QStandardItem *> items;
items << new QStandardItem("c");
model.insertColumn(0, items);
}
static void setModelTexts(QStandardItemModel *model)
{
const int columnCount = model->columnCount();
for (int i = 0, rowCount = model->rowCount(); i < rowCount; ++i) {
const QString prefix = QLatin1String("item [") + QString::number(i) + QLatin1Char(',');
for (int j = 0; j < columnCount; ++j)
model->setData(model->index(i, j), prefix + QString::number(j) + QLatin1Char(']'));
}
}
void tst_QHeaderView::QTBUG12268_hiddenMovedSectionSorting()
{
QTableView view; // ### this test fails on QTableView &view = *m_tableview; !? + shadowing view member
QStandardItemModel *model = new QStandardItemModel(4,3, &view);
setModelTexts(model);
view.setModel(model);
view.horizontalHeader()->setSectionsMovable(true);
view.setSortingEnabled(true);
view.sortByColumn(1, Qt::AscendingOrder);
view.horizontalHeader()->moveSection(0,2);
view.setColumnHidden(1, true);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QCOMPARE(view.horizontalHeader()->hiddenSectionCount(), 1);
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton);
QCOMPARE(view.horizontalHeader()->hiddenSectionCount(), 1);
}
void tst_QHeaderView::QTBUG14242_hideSectionAutoSize()
{
QTableView qtv;
QStandardItemModel amodel(4, 4);
qtv.setModel(&amodel);
QHeaderView *hv = qtv.verticalHeader();
hv->setDefaultSectionSize(25);
hv->setSectionResizeMode(QHeaderView::ResizeToContents);
qtv.show();
hv->hideSection(0);
int afterlength = hv->length();
int calced_length = 0;
for (int u = 0; u < hv->count(); ++u)
calced_length += hv->sectionSize(u);
QCOMPARE(calced_length, afterlength);
}
void tst_QHeaderView::QTBUG50171_visualRegionForSwappedItems()
{
protected_QHeaderView headerView(Qt::Horizontal);
QtTestModel model;
model.rows = 2;
model.cols = 3;
headerView.setModel(&model);
headerView.swapSections(1, 2);
headerView.hideSection(0);
headerView.testVisualRegionForSelection();
}
class QTBUG53221_Model : public QAbstractItemModel
{
public:
void insertRowAtBeginning()
{
Q_EMIT layoutAboutToBeChanged();
m_displayNames.insert(0, QStringLiteral("Item %1").arg(m_displayNames.count()));
// Rows are always inserted at the beginning, so move all others.
foreach (const QModelIndex &persIndex, persistentIndexList())
{
// The vertical header view will have a persistent index stored here on the second call to insertRowAtBeginning.
changePersistentIndex(persIndex, index(persIndex.row() + 1, persIndex.column(), persIndex.parent()));
}
Q_EMIT layoutChanged();
}
QVariant data(const QModelIndex &index, int role) const override
{
return (role == Qt::DisplayRole) ? m_displayNames.at(index.row()) : QVariant();
}
QModelIndex index(int row, int column, const QModelIndex &) const override { return createIndex(row, column); }
QModelIndex parent(const QModelIndex &) const override { return QModelIndex(); }
int rowCount(const QModelIndex &) const override { return m_displayNames.count(); }
int columnCount(const QModelIndex &) const override { return 1; }
private:
QStringList m_displayNames;
};
void tst_QHeaderView::QTBUG53221_assertShiftHiddenRow()
{
QTableView tableView;
QTBUG53221_Model modelTableView;
tableView.setModel(&modelTableView);
modelTableView.insertRowAtBeginning();
tableView.setRowHidden(0, true);
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), true);
modelTableView.insertRowAtBeginning();
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), false);
QCOMPARE(tableView.verticalHeader()->isSectionHidden(1), true);
modelTableView.insertRowAtBeginning();
QCOMPARE(tableView.verticalHeader()->isSectionHidden(0), false);
QCOMPARE(tableView.verticalHeader()->isSectionHidden(1), false);
QCOMPARE(tableView.verticalHeader()->isSectionHidden(2), true);
}
void protected_QHeaderView::testVisualRegionForSelection()
{
QRegion r = visualRegionForSelection(QItemSelection(model()->index(1, 0), model()->index(1, 2)));
QCOMPARE(r.boundingRect().contains(QRect(1, 1, length() - 2, 1)), true);
}
void tst_QHeaderView::ensureNoIndexAtLength()
{
QTableView qtv;
QStandardItemModel amodel(4, 4);
qtv.setModel(&amodel);
QHeaderView *hv = qtv.verticalHeader();
QCOMPARE(hv->visualIndexAt(hv->length()), -1);
hv->resizeSection(hv->count() - 1, 0);
QCOMPARE(hv->visualIndexAt(hv->length()), -1);
}
void tst_QHeaderView::offsetConsistent()
{
// Ensure that a hidden section 'far away'
// does not affect setOffsetToSectionPosition ..
const int sectionToHide = 513;
QTableView qtv;
QStandardItemModel amodel(1000, 4);
qtv.setModel(&amodel);
QHeaderView *hv = qtv.verticalHeader();
for (int u = 0; u < 100; u += 2)
hv->resizeSection(u, 0);
hv->setOffsetToSectionPosition(150);
int offset1 = hv->offset();
hv->hideSection(sectionToHide);
hv->setOffsetToSectionPosition(150);
int offset2 = hv->offset();
QCOMPARE(offset1, offset2);
// Ensure that hidden indexes (still) is considered.
hv->resizeSection(sectionToHide, hv->sectionSize(200) * 2);
hv->setOffsetToSectionPosition(800);
offset1 = hv->offset();
hv->showSection(sectionToHide);
hv->setOffsetToSectionPosition(800);
offset2 = hv->offset();
QVERIFY(offset2 > offset1);
}
void tst_QHeaderView::initialSortOrderRole()
{
QTableView view; // ### Shadowing member view (of type QHeaderView)
QStandardItemModel *model = new QStandardItemModel(4, 3, &view);
setModelTexts(model);
QStandardItem *ascendingItem = new QStandardItem();
QStandardItem *descendingItem = new QStandardItem();
ascendingItem->setData(Qt::AscendingOrder, Qt::InitialSortOrderRole);
descendingItem->setData(Qt::DescendingOrder, Qt::InitialSortOrderRole);
model->setHorizontalHeaderItem(1, ascendingItem);
model->setHorizontalHeaderItem(2, descendingItem);
view.setModel(model);
view.setSortingEnabled(true);
view.sortByColumn(0, Qt::AscendingOrder);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
const int column1Pos = view.horizontalHeader()->sectionViewportPosition(1) + 5; // +5 not to be on the handle
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column1Pos, 0));
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 1);
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::AscendingOrder);
const int column2Pos = view.horizontalHeader()->sectionViewportPosition(2) + 5; // +5 not to be on the handle
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column2Pos, 0));
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 2);
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::DescendingOrder);
const int column0Pos = view.horizontalHeader()->sectionViewportPosition(0) + 5; // +5 not to be on the handle
QTest::mouseClick(view.horizontalHeader()->viewport(), Qt::LeftButton, Qt::NoModifier, QPoint(column0Pos, 0));
QCOMPARE(view.horizontalHeader()->sortIndicatorSection(), 0);
QCOMPARE(view.horizontalHeader()->sortIndicatorOrder(), Qt::AscendingOrder);
}
const bool block_some_signals = true; // The test should also work with this set to false
const int rowcount = 500;
const int colcount = 10;
QString istr(int n, bool comma = true)
{
QString s;
s.setNum(n);
if (comma)
s += ", ";
return s;
}
void tst_QHeaderView::calculateAndCheck(int cppline, const int precalced_comparedata[])
{
qint64 endtimer = timer.elapsed();
const bool silentmode = true;
if (!silentmode)
qDebug().nospace() << "(Time:" << endtimer << ")";
QString sline;
sline.setNum(cppline - 1);
const int p1 = 3133777; // just a prime (maybe not that random ;) )
const int p2 = 135928393; // just a random large prime - a bit less than signed 32-bit
int sum_visual = 0;
int sum_logical = 0;
int sum_size = 0;
int sum_hidden_size = 0;
int sum_lookup_visual = 0;
int sum_lookup_logical = 0;
int chk_visual = 1;
int chk_logical = 1;
int chk_sizes = 1;
int chk_hidden_size = 1;
int chk_lookup_visual = 1;
int chk_lookup_logical = 1;
int header_lenght = 0;
int lastindex = view->count() - 1;
// calculate information based on index
for (int i = 0; i <= lastindex; ++i) {
int visual = view->visualIndex(i);
int logical = view->logicalIndex(i);
int ssize = view->sectionSize(i);
sum_visual += visual;
sum_logical += logical;
sum_size += ssize;
if (visual >= 0) {
chk_visual %= p2;
chk_visual *= (visual + 1) * (i + 1) * p1;
}
if (logical >= 0) {
chk_logical %= p2;
chk_logical *= (logical + 1) * (i + 1 + (logical != i) ) * p1;
}
if (ssize >= 0) {
chk_sizes %= p2;
chk_sizes *= ( (ssize + 2) * (i + 1) * p1);
}
if (view->isSectionHidden(i)) {
view->showSection(i);
int hiddensize = view->sectionSize(i);
sum_hidden_size += hiddensize;
chk_hidden_size %= p2;
chk_hidden_size += ( (hiddensize + 1) * (i + 1) * p1);
// (hiddensize + 1) in the above to differ between hidden and size 0
// Though it can be changed (why isn't sections with size 0 hidden?)
view->hideSection(i);
}
}
// lookup indexes by pixel position
const int max_lookup_count = 500;
int lookup_to = view->height() + 1;
if (lookup_to > max_lookup_count)
lookup_to = max_lookup_count; // We limit this lookup - not to spend years when testing.
// Notice that lookupTest also has its own extra test
for (int u = 0; u < max_lookup_count; ++u) {
int visu = view->visualIndexAt(u);
int logi = view->logicalIndexAt(u);
sum_lookup_visual += logi;
sum_lookup_logical += visu;
chk_lookup_visual %= p2;
chk_lookup_visual *= ( (u + 1) * p1 * (visu + 2));
chk_lookup_logical %= p2;
chk_lookup_logical *= ( (u + 1) * p1 * (logi + 2));
}
header_lenght = view->length();
// visual and logical indexes.
int sum_to_last_index = (lastindex * (lastindex + 1)) / 2; // == 0 + 1 + 2 + 3 + ... + lastindex
const bool write_calced_data = false; // Do not write calculated output (unless the test fails)
if (write_calced_data) {
qDebug().nospace() << "(" << cppline - 1 << ")" // << " const int precalced[] = "
<< " {" << chk_visual << ", " << chk_logical << ", " << chk_sizes << ", " << chk_hidden_size
<< ", " << chk_lookup_visual << ", " << chk_lookup_logical << ", " << header_lenght << "};";
}
const bool sanity_checks = true;
if (sanity_checks) {
QString msg = QString("sanity problem at ") + sline;
const QScopedArrayPointer<char> holder(QTest::toString(msg));
const auto verifytext = holder.data();
QVERIFY2(m_tableview->model()->rowCount() == view->count() , verifytext);
QVERIFY2(view->visualIndex(lastindex + 1) <= 0, verifytext); // there is no such index in model
QVERIFY2(view->logicalIndex(lastindex + 1) <= 0, verifytext); // there is no such index in model.
QVERIFY2(view->logicalIndex(lastindex + 1) <= 0, verifytext); // there is no such index in model.
QVERIFY2(lastindex < 0 || view->visualIndex(0) >= 0, verifytext); // no rows or legal index
QVERIFY2(lastindex < 0 || view->logicalIndex(0) >= 0, verifytext); // no rows or legal index
QVERIFY2(lastindex < 0 || view->visualIndex(lastindex) >= 0, verifytext); // no rows or legal index
QVERIFY2(lastindex < 0 || view->logicalIndex(lastindex) >= 0, verifytext); // no rows or legal index
QVERIFY2(view->visualIndexAt(-1) == -1, verifytext);
QVERIFY2(view->logicalIndexAt(-1) == -1, verifytext);
QVERIFY2(view->visualIndexAt(view->length()) == -1, verifytext);
QVERIFY2(view->logicalIndexAt(view->length()) == -1, verifytext);
QVERIFY2(sum_visual == sum_logical, verifytext);
QVERIFY2(sum_to_last_index == sum_logical, verifytext);
}
// Semantic test
const bool check_semantics = true; // Otherwise there is no 'real' test
if (!check_semantics)
return;
const int *x = precalced_comparedata;
QString msg = "semantic problem at " + QString(__FILE__) + " (" + sline + ")";
msg += "\nThe *expected* result was : {" + istr(x[0]) + istr(x[1]) + istr(x[2]) + istr(x[3])
+ istr(x[4]) + istr(x[5]) + istr(x[6], false) + QLatin1Char('}');
msg += "\nThe calculated result was : {";
msg += istr(chk_visual) + istr(chk_logical) + istr(chk_sizes) + istr(chk_hidden_size)
+ istr(chk_lookup_visual) + istr(chk_lookup_logical) + istr(header_lenght, false) + "};";
const QScopedArrayPointer<char> holder(QTest::toString(msg));
const auto verifytext = holder.data();
QVERIFY2(chk_visual == x[0], verifytext);
QVERIFY2(chk_logical == x[1], verifytext);
QVERIFY2(chk_sizes == x[2], verifytext);
QVERIFY2(chk_hidden_size == x[3], verifytext);
QVERIFY2(chk_lookup_visual == x[4], verifytext);
QVERIFY2(chk_lookup_logical == x[5], verifytext);
QVERIFY2(header_lenght == x[6], verifytext);
}
void tst_QHeaderView::setupTestData(bool also_use_reset_model)
{
QTest::addColumn<bool>("updates_enabled");
QTest::addColumn<bool>("special_prepare");
QTest::addColumn<bool>("reset_model");
if (also_use_reset_model) {
QTest::newRow("no_updates+normal+reset") << false << false << true;
QTest::newRow("hasupdates+normal+reset") << true << false << true;
QTest::newRow("no_updates+special+reset") << false << true << true;
QTest::newRow("hasupdates+special+reset") << true << true << true;
}
QTest::newRow("no_updates+normal") << false << false << false;
QTest::newRow("hasupdates+normal") << true << false << false;
QTest::newRow("no_updates+special") << false << true << false;
QTest::newRow("hasupdates+special") << true << true << false;
}
void tst_QHeaderView::additionalInit()
{
m_tableview->setVerticalHeader(view);
QFETCH(bool, updates_enabled);
QFETCH(bool, special_prepare);
QFETCH(bool, reset_model);
m_using_reset_model = reset_model;
if (m_using_reset_model) {
XResetModel *m = new XResetModel();
m_tableview->setModel(m);
delete model;
model = m;
} else {
m_tableview->setModel(model);
}
const int default_section_size = 25;
view->setDefaultSectionSize(default_section_size); // Important - otherwise there will be semantic changes
model->clear();
if (special_prepare) {
for (int u = 0; u <= rowcount; ++u) // ensures fragmented spans in e.g Qt 4.7
model->setRowCount(u);
model->setColumnCount(colcount);
view->swapSections(0, rowcount - 1);
view->swapSections(0, rowcount - 1); // No real change - setup visual and log-indexes
view->hideSection(rowcount - 1);
view->showSection(rowcount - 1); // No real change - (maybe init hidden vector)
} else {
model->setColumnCount(colcount);
model->setRowCount(rowcount);
}
QString s;
for (int i = 0; i < model->rowCount(); ++i) {
model->setData(model->index(i, 0), QVariant(i));
s.setNum(i);
s += QLatin1Char('.');
s += 'a' + (i % 25);
model->setData(model->index(i, 1), QVariant(s));
}
m_tableview->setUpdatesEnabled(updates_enabled);
view->blockSignals(block_some_signals);
timer.start();
}
void tst_QHeaderView::logicalIndexAtTest()
{
additionalInit();
view->swapSections(4, 9); // Make sure that visual and logical Indexes are not just the same.
int check1 = 0;
int check2 = 0;
for (int u = 0; u < model->rowCount(); ++u) {
view->resizeSection(u, 10 + u % 30);
int v = view->visualIndexAt(u * 29);
view->visualIndexAt(u * 29);
check1 += v;
check2 += u * v;
}
view->resizeSection(0, 0); // Make sure that we have a 0 size section - before the result set
view->setSectionHidden(6, true); // Make sure we have a real hidden section before result set
//qDebug() << "logicalIndexAtTest" << check1 << check2;
const int precalced_check1 = 106327;
const int precalced_check2 = 29856418;
QCOMPARE(precalced_check1, check1);
QCOMPARE(precalced_check2, check2);
const int precalced_results[] = { 1145298384, -1710423344, -650981936, 372919464, -1544372176, -426463328, 12124 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::visualIndexAtTest()
{
additionalInit();
view->swapSections(4, 9); // Make sure that visual and logical Indexes are not just the same.
int check1 = 0;
int check2 = 0;
for (int u = 0; u < model->rowCount(); ++u) {
view->resizeSection(u, 3 + u % 17);
int v = view->visualIndexAt(u * 29);
check1 += v;
check2 += u * v;
}
view->resizeSection(1, 0); // Make sure that we have a 0 size section - before the result set
view->setSectionHidden(5, true); // Make sure we have a real hidden section before result set
//qDebug() << "visualIndexAtTest" << check1 << check2;
const int precalced_check1 = 72665;
const int precalced_check2 = 14015890;
QCOMPARE(precalced_check1, check1);
QCOMPARE(precalced_check2, check2);
const int precalced_results[] = { 1145298384, -1710423344, -1457520212, 169223959, 557466160, -324939600, 5453 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::hideShowTest()
{
additionalInit();
for (int u = model->rowCount(); u >= 0; --u)
if (u % 8 != 0)
view->hideSection(u);
for (int u = model->rowCount(); u >= 0; --u)
if (u % 3 == 0)
view->showSection(u);
view->setSectionHidden(model->rowCount(), true); // invalid hide (should be ignored)
view->setSectionHidden(-1, true); // invalid hide (should be ignored)
const int precalced_results[] = { -1523279360, -1523279360, -1321506816, 2105322423, 1879611280, 1879611280, 5225 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::swapSectionsTest()
{
additionalInit();
for (int u = 0; u < rowcount / 2; ++u)
view->swapSections(u, rowcount - u - 1);
for (int u = 0; u < rowcount; u += 2)
view->swapSections(u, u + 1);
view->swapSections(0, model->rowCount()); // invalid swapsection (should be ignored)
const int precalced_results[] = { -1536450048, -1774641430, -1347156568, 1, 1719705216, -240077576, 12500 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::moveSectionTest()
{
additionalInit();
for (int u = 1; u < 5; ++u)
view->moveSection(u, model->rowCount() - u);
view->moveSection(2, model->rowCount() / 2);
view->moveSection(0, 10);
view->moveSection(0, model->rowCount() - 10);
view->moveSection(0, model->rowCount()); // invalid move (should be ignored)
const int precalced_results[] = { 645125952, 577086896, -1347156568, 1, 1719705216, 709383416, 12500 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::defaultSizeTest()
{
additionalInit();
view->hideSection(rowcount / 2);
int restore_to = view->defaultSectionSize();
view->setDefaultSectionSize(restore_to + 5);
const int precalced_results[] = { -1523279360, -1523279360, -1739688320, -1023807777, 997629696, 997629696, 14970 };
calculateAndCheck(__LINE__, precalced_results);
view->setDefaultSectionSize(restore_to);
}
void tst_QHeaderView::removeTest()
{
additionalInit();
view->swapSections(0, 5);
model->removeRows(0, 1); // remove one row
model->removeRows(4, 10);
model->setRowCount(model->rowCount() / 2 - 1);
if (m_using_reset_model) {
const int precalced_results[] = { 1741224292, -135269187, -569519837, 1, 1719705216, -1184395000, 6075 };
calculateAndCheck(__LINE__, precalced_results);
} else {
const int precalced_results[] = { 289162397, 289162397, -569519837, 1, 1719705216, 1719705216, 6075 };
calculateAndCheck(__LINE__, precalced_results);
}
}
void tst_QHeaderView::insertTest()
{
additionalInit();
view->swapSections(0, model->rowCount() - 1);
model->insertRows(0, 1); // insert one row
model->insertRows(4, 10);
model->setRowCount(model->rowCount() * 2 - 1);
if (m_using_reset_model) {
const int precalced_results[] = { 2040508069, -1280266538, -150350734, 1, 1719705216, 1331312784, 25525 };
calculateAndCheck(__LINE__, precalced_results);
} else {
const int precalced_results[] = { -1909447021, 339092083, -150350734, 1, 1719705216, -969712728, 25525 };
calculateAndCheck(__LINE__, precalced_results);
}
}
void tst_QHeaderView::mixedTests()
{
additionalInit();
model->setRowCount(model->rowCount() + 10);
for (int u = 0; u < model->rowCount(); u += 2)
view->swapSections(u, u + 1);
view->moveSection(0, 5);
for (int u = model->rowCount(); u >= 0; --u) {
if (u % 5 != 0)
view->hideSection(u);
if (u % 3 != 0)
view->showSection(u);
}
model->insertRows(3, 7);
model->removeRows(8, 3);
model->setRowCount(model->rowCount() - 10);
if (m_using_reset_model) {
const int precalced_results[] = { 898296472, 337096378, -543340640, 1, -1251526424, -568618976, 9250 };
calculateAndCheck(__LINE__, precalced_results);
} else {
const int precalced_results[] = { 1911338224, 1693514365, -613398968, -1912534953, 1582159424, -1851079000, 9300 };
calculateAndCheck(__LINE__, precalced_results);
}
}
void tst_QHeaderView::resizeToContentTest()
{
additionalInit();
QModelIndex idx = model->index(2, 2);
model->setData(idx, QVariant("A normal string"));
idx = model->index(1, 3);
model->setData(idx, QVariant("A normal longer string to test resize"));
QHeaderView *hh = m_tableview->horizontalHeader();
hh->resizeSections(QHeaderView::ResizeToContents);
QVERIFY(hh->sectionSize(3) > hh->sectionSize(2));
for (int u = 0; u < 10; ++u)
view->resizeSection(u, 1);
view->resizeSections(QHeaderView::ResizeToContents);
QVERIFY(view->sectionSize(1) > 1);
QVERIFY(view->sectionSize(2) > 1);
// Check minimum section size
hh->setMinimumSectionSize(150);
model->setData(idx, QVariant("i"));
hh->resizeSections(QHeaderView::ResizeToContents);
QCOMPARE(hh->sectionSize(3), 150);
hh->setMinimumSectionSize(-1);
// Check maximumSection size
hh->setMaximumSectionSize(200);
model->setData(idx, QVariant("This is a even longer string that is expected to be more than 200 pixels"));
hh->resizeSections(QHeaderView::ResizeToContents);
QCOMPARE(hh->sectionSize(3), 200);
hh->setMaximumSectionSize(-1);
view->setDefaultSectionSize(25); // To make sure our precalced data are correct. We do not know font height etc.
const int precalced_results[] = { -1523279360, -1523279360, -1347156568, 1, 1719705216, 1719705216, 12500 };
calculateAndCheck(__LINE__, precalced_results);
}
void tst_QHeaderView::testStreamWithHide()
{
#ifndef QT_NO_DATASTREAM
m_tableview->setVerticalHeader(view);
m_tableview->setModel(model);
view->setDefaultSectionSize(25);
view->hideSection(2);
view->swapSections(1, 2);
QByteArray s = view->saveState();
view->swapSections(1, 2);
view->setDefaultSectionSize(30); // To make sure our precalced data are correct.
view->restoreState(s);
const int precalced_results[] = { -1116614432, -1528653200, -1914165644, 244434607, -1111214068, 750357900, 75};
calculateAndCheck(__LINE__, precalced_results);
#else
QSKIP("Datastream required for testStreamWithHide. Skipping this test.");
#endif
}
void tst_QHeaderView::testStylePosition()
{
topLevel->show();
QVERIFY(QTest::qWaitForWindowExposed(topLevel));
protected_QHeaderView *header = static_cast<protected_QHeaderView *>(view);
TestStyle proxy;
header->setStyle(&proxy);
QImage image(1, 1, QImage::Format_ARGB32);
QPainter p(&image);
// 0, 1, 2, 3
header->paintSection(&p, view->rect(), 0);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning);
header->paintSection(&p, view->rect(), 1);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle);
header->paintSection(&p, view->rect(), 2);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle);
header->paintSection(&p, view->rect(), 3);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End);
// (0),2,1,3
view->setSectionHidden(0, true);
view->swapSections(1, 2);
header->paintSection(&p, view->rect(), 1);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Middle);
header->paintSection(&p, view->rect(), 2);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning);
header->paintSection(&p, view->rect(), 3);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End);
// (1),2,0,(3)
view->setSectionHidden(3, true);
view->setSectionHidden(0, false);
view->setSectionHidden(1, true);
view->swapSections(0, 1);
header->paintSection(&p, view->rect(), 0);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::End);
header->paintSection(&p, view->rect(), 2);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::Beginning);
// (1),2,(0),(3)
view->setSectionHidden(0, true);
header->paintSection(&p, view->rect(), 2);
QCOMPARE(proxy.lastPosition, QStyleOptionHeader::OnlyOneSection);
}
void tst_QHeaderView::sizeHintCrash()
{
QTreeView treeView;
QStandardItemModel *model = new QStandardItemModel(&treeView);
model->appendRow(new QStandardItem("QTBUG-48543"));
treeView.setModel(model);
treeView.header()->sizeHintForColumn(0);
treeView.header()->sizeHintForRow(0);
}
void tst_QHeaderView::stretchAndRestoreLastSection()
{
QStandardItemModel m(10, 10);
QTableView tv;
tv.setModel(&m);
tv.showMaximized();
const int minimumSectionSize = 20;
const int defaultSectionSize = 30;
const int someOtherSectionSize = 40;
const int biggerSizeThanAnySection = 50;
QVERIFY(QTest::qWaitForWindowExposed(&tv));
QHeaderView &header = *tv.horizontalHeader();
// set minimum size before resizeSections() is called
// which is done inside setStretchLastSection
header.setMinimumSectionSize(minimumSectionSize);
header.setDefaultSectionSize(defaultSectionSize);
header.resizeSection(9, someOtherSectionSize);
header.setStretchLastSection(true);
// Default last section is larger
QCOMPARE(header.sectionSize(8), defaultSectionSize);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
// Moving last section away (restore old last section 9 - and make 8 larger)
header.swapSections(9, 8);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
QVERIFY(header.sectionSize(8) >= biggerSizeThanAnySection);
// Make section 9 the large one again
header.hideSection(8);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
// Show section 8 again - and make that one the last one.
header.showSection(8);
QVERIFY(header.sectionSize(8) > biggerSizeThanAnySection);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
// Swap the sections so the logical indexes are equal to visible indexes again.
header.moveSection(9, 8);
QCOMPARE(header.sectionSize(8), defaultSectionSize);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
// Append sections
m.setColumnCount(15);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
QVERIFY(header.sectionSize(14) >= biggerSizeThanAnySection);
// Truncate sections (remove sections with the last section)
m.setColumnCount(10);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
for (int u = 0; u < 9; ++u)
QCOMPARE(header.sectionSize(u), defaultSectionSize);
// Insert sections
m.insertColumns(2, 2);
QCOMPARE(header.sectionSize(9), defaultSectionSize);
QCOMPARE(header.sectionSize(10), defaultSectionSize);
QVERIFY(header.sectionSize(11) >= biggerSizeThanAnySection);
// Append an extra section and check restore
m.setColumnCount(m.columnCount() + 1);
QCOMPARE(header.sectionSize(11), someOtherSectionSize);
QVERIFY(header.sectionSize(12) >= biggerSizeThanAnySection);
// Remove some sections but not the last one.
m.removeColumns(2, 2);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
QVERIFY(header.sectionSize(10) >= biggerSizeThanAnySection);
for (int u = 0; u < 9; ++u)
QCOMPARE(header.sectionSize(u), defaultSectionSize);
// Empty the header and start over with some more tests
m.setColumnCount(0);
m.setColumnCount(10);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
// Check resize of the last section
header.resizeSection(9, someOtherSectionSize);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection); // It should still be stretched
header.swapSections(9, 8);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
// Restore the order
header.swapSections(9, 8);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
// Hide the last 3 sections and test stretch last section on swap/move
// when hidden sections with a larger visual index exists.
header.hideSection(7);
header.hideSection(8);
header.hideSection(9);
QVERIFY(header.sectionSize(6) >= biggerSizeThanAnySection);
header.moveSection(2, 7);
QVERIFY(header.sectionSize(2) >= biggerSizeThanAnySection);
header.swapSections(1, 8);
QCOMPARE(header.sectionSize(2), defaultSectionSize);
QVERIFY(header.sectionSize(1) >= biggerSizeThanAnySection);
// Inserting sections 2
m.setColumnCount(0);
m.setColumnCount(10);
header.resizeSection(1, someOtherSectionSize);
header.swapSections(1, 9);
m.insertColumns(9, 2);
header.swapSections(1, 11);
QCOMPARE(header.sectionSize(1), someOtherSectionSize);
// Clear and re-add. This triggers a different code path than seColumnCount(0)
m.clear();
m.setColumnCount(3);
QVERIFY(header.sectionSize(2) >= biggerSizeThanAnySection);
// Test import/export of the original (not stretched) sectionSize.
m.setColumnCount(0);
m.setColumnCount(10);
header.resizeSection(9, someOtherSectionSize);
QVERIFY(header.sectionSize(9) >= biggerSizeThanAnySection);
QByteArray b = header.saveState();
m.setColumnCount(0);
m.setColumnCount(10);
header.restoreState(b);
header.setStretchLastSection(false);
QCOMPARE(header.sectionSize(9), someOtherSectionSize);
}
void tst_QHeaderView::testMinMaxSectionSizeStretched()
{
testMinMaxSectionSize(true);
}
void tst_QHeaderView::testMinMaxSectionSizeNotStretched()
{
testMinMaxSectionSize(false);
}
static void waitFor(const std::function<bool()> &func)
{
for (int i = 0; i < 100; i++)
{
if (func())
return;
QTest::qWait(10);
}
}
void tst_QHeaderView::testMinMaxSectionSize(bool stretchLastSection)
{
QStandardItemModel m(5, 5);
QTableView tv;
tv.setModel(&m);
tv.show();
const int sectionSizeMin = 20;
const int sectionSizeMax = 40;
const int defaultSectionSize = 30;
QVERIFY(QTest::qWaitForWindowExposed(&tv));
QHeaderView &header = *tv.horizontalHeader();
header.setMinimumSectionSize(sectionSizeMin);
header.setMaximumSectionSize(sectionSizeMax);
header.setDefaultSectionSize(defaultSectionSize);
header.setStretchLastSection(stretchLastSection);
// check defaults
QCOMPARE(header.sectionSize(0), defaultSectionSize);
QCOMPARE(header.sectionSize(3), defaultSectionSize);
// do not go above maxSectionSize
header.resizeSection(0, sectionSizeMax + 1);
QCOMPARE(header.sectionSize(0), sectionSizeMax);
// do not go below minSectionSize
header.resizeSection(0, sectionSizeMin - 1);
QCOMPARE(header.sectionSize(0), sectionSizeMin);
// change section size on max change
header.setMinimumSectionSize(sectionSizeMin);
header.setMaximumSectionSize(sectionSizeMax);
header.resizeSection(0, sectionSizeMax);
QCOMPARE(header.sectionSize(0), sectionSizeMax);
header.setMaximumSectionSize(defaultSectionSize);
waitFor([this, &header, defaultSectionSize]() { return header.sectionSize(0) == defaultSectionSize; });
QCOMPARE(header.sectionSize(0), defaultSectionSize);
// change section size on min change
header.setMinimumSectionSize(sectionSizeMin);
header.setMaximumSectionSize(sectionSizeMax);
header.resizeSection(0, sectionSizeMin);
QCOMPARE(header.sectionSize(0), sectionSizeMin);
header.setMinimumSectionSize(defaultSectionSize);
waitFor([this, &header, defaultSectionSize]() { return header.sectionSize(0) == defaultSectionSize; });
QCOMPARE(header.sectionSize(0), defaultSectionSize);
}
QTEST_MAIN(tst_QHeaderView)
#include "tst_qheaderview.moc"
|