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
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** 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 <QListWidget>
#include <QScrollBar>
#include <QSignalSpy>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QStyledItemDelegate>
#include <QStyleFactory>
#include <QTest>
#include <QTimer>
#include <QtMath>
#include <QtTest/private/qtesthelpers_p.h>
#include <QtWidgets/private/qlistview_p.h>
using namespace QTestPrivate;
#if defined(Q_OS_WIN)
# include <windows.h>
# include <QDialog>
# include <QGuiApplication>
# include <QVBoxLayout>
#include <qpa/qplatformnativeinterface.h>
#endif // Q_OS_WIN
#if defined(Q_OS_WIN)
static inline HWND getHWNDForWidget(const QWidget *widget)
{
QWindow *window = widget->windowHandle();
return static_cast<HWND> (QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", window));
}
#endif // Q_OS_WIN
Q_DECLARE_METATYPE(QAbstractItemView::ScrollMode)
Q_DECLARE_METATYPE(QMargins)
Q_DECLARE_METATYPE(QSize)
using IntList = QVector<int>;
static QStringList generateList(const QString &prefix, int size)
{
QStringList result;
result.reserve(size);
for (int i = 0; i < size; ++i)
result.append(prefix + QString::number(i));
return result;
}
class PublicListView : public QListView
{
public:
using QListView::QListView;
using QListView::contentsSize;
using QListView::moveCursor;
using QListView::selectedIndexes;
using QListView::setPositionForIndex;
using QListView::setSelection;
using QListView::setViewportMargins;
using QListView::startDrag;
using QListView::viewOptions;
QRegion getVisualRegionForSelection() const
{
return QListView::visualRegionForSelection(selectionModel()->selection());
}
friend class tst_QListView;
};
class tst_QListView : public QObject
{
Q_OBJECT
private slots:
void cleanup();
void getSetCheck();
void noDelegate();
void noModel();
void emptyModel();
void removeRows();
void cursorMove();
void hideRows();
void moveCursor();
void moveCursor2();
void moveCursor3();
void indexAt();
void clicked();
void singleSelectionRemoveRow();
void singleSelectionRemoveColumn();
void modelColumn();
void hideFirstRow();
void batchedMode();
void setCurrentIndex();
void selection_data();
void selection();
void scrollTo();
void scrollBarRanges();
void scrollBarAsNeeded_data();
void scrollBarAsNeeded();
void moveItems();
void wordWrap();
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
void setCurrentIndexAfterAppendRowCrash();
#endif
void emptyItemSize();
void task203585_selectAll();
void task228566_infiniteRelayout();
void task248430_crashWith0SizedItem();
void task250446_scrollChanged();
void task196118_visualRegionForSelection();
void task254449_draggingItemToNegativeCoordinates();
void keyboardSearch();
void shiftSelectionWithNonUniformItemSizes();
void shiftSelectionWithItemAlignment();
void clickOnViewportClearsSelection();
void task262152_setModelColumnNavigate();
void taskQTBUG_2233_scrollHiddenItems_data();
void taskQTBUG_2233_scrollHiddenItems();
void taskQTBUG_633_changeModelData();
void taskQTBUG_435_deselectOnViewportClick();
void taskQTBUG_2678_spacingAndWrappedText();
void taskQTBUG_5877_skippingItemInPageDownUp();
void taskQTBUG_9455_wrongScrollbarRanges();
void styleOptionViewItem();
void taskQTBUG_12308_artihmeticException();
void taskQTBUG_12308_wrongFlowLayout();
void taskQTBUG_21115_scrollToAndHiddenItems_data();
void taskQTBUG_21115_scrollToAndHiddenItems();
void draggablePaintPairs_data();
void draggablePaintPairs();
void taskQTBUG_21804_hiddenItemsAndScrollingWithKeys_data();
void taskQTBUG_21804_hiddenItemsAndScrollingWithKeys();
void spacing_data();
void spacing();
void testScrollToWithHidden();
void testViewOptions();
void taskQTBUG_39902_mutualScrollBars_data();
void taskQTBUG_39902_mutualScrollBars();
void horizontalScrollingByVerticalWheelEvents();
void taskQTBUG_7232_AllowUserToControlSingleStep();
void taskQTBUG_51086_skippingIndexesInSelectedIndexes();
void taskQTBUG_47694_indexOutOfBoundBatchLayout();
void itemAlignment();
void internalDragDropMove_data();
void internalDragDropMove();
void scrollOnRemove_data();
void scrollOnRemove();
};
// Testing get/set functions
void tst_QListView::getSetCheck()
{
QListView obj1;
// Movement QListView::movement()
// void QListView::setMovement(Movement)
obj1.setMovement(QListView::Static);
QCOMPARE(QListView::Static, obj1.movement());
obj1.setMovement(QListView::Free);
QCOMPARE(QListView::Free, obj1.movement());
obj1.setMovement(QListView::Snap);
QCOMPARE(QListView::Snap, obj1.movement());
// Flow QListView::flow()
// void QListView::setFlow(Flow)
obj1.setFlow(QListView::LeftToRight);
QCOMPARE(QListView::LeftToRight, obj1.flow());
obj1.setFlow(QListView::TopToBottom);
QCOMPARE(QListView::TopToBottom, obj1.flow());
// ResizeMode QListView::resizeMode()
// void QListView::setResizeMode(ResizeMode)
obj1.setResizeMode(QListView::Fixed);
QCOMPARE(QListView::Fixed, obj1.resizeMode());
obj1.setResizeMode(QListView::Adjust);
QCOMPARE(QListView::Adjust, obj1.resizeMode());
// LayoutMode QListView::layoutMode()
// void QListView::setLayoutMode(LayoutMode)
obj1.setLayoutMode(QListView::SinglePass);
QCOMPARE(QListView::SinglePass, obj1.layoutMode());
obj1.setLayoutMode(QListView::Batched);
QCOMPARE(QListView::Batched, obj1.layoutMode());
// int QListView::spacing()
// void QListView::setSpacing(int)
obj1.setSpacing(0);
QCOMPARE(0, obj1.spacing());
obj1.setSpacing(std::numeric_limits<int>::min());
QCOMPARE(std::numeric_limits<int>::min(), obj1.spacing());
obj1.setSpacing(std::numeric_limits<int>::max());
QCOMPARE(std::numeric_limits<int>::max(), obj1.spacing());
// ViewMode QListView::viewMode()
// void QListView::setViewMode(ViewMode)
obj1.setViewMode(QListView::ListMode);
QCOMPARE(QListView::ListMode, obj1.viewMode());
obj1.setViewMode(QListView::IconMode);
QCOMPARE(QListView::IconMode, obj1.viewMode());
// int QListView::modelColumn()
// void QListView::setModelColumn(int)
obj1.setModelColumn(0);
QCOMPARE(0, obj1.modelColumn());
obj1.setModelColumn(std::numeric_limits<int>::min());
QCOMPARE(0, obj1.modelColumn()); // Less than 0 => 0
obj1.setModelColumn(std::numeric_limits<int>::max());
QCOMPARE(0, obj1.modelColumn()); // No model => 0
// bool QListView::uniformItemSizes()
// void QListView::setUniformItemSizes(bool)
obj1.setUniformItemSizes(false);
QCOMPARE(false, obj1.uniformItemSizes());
obj1.setUniformItemSizes(true);
QCOMPARE(true, obj1.uniformItemSizes());
// make sure setViewMode() doesn't reset resizeMode
obj1.clearPropertyFlags();
obj1.setResizeMode(QListView::Adjust);
obj1.setViewMode(QListView::IconMode);
QCOMPARE(obj1.resizeMode(), QListView::Adjust);
obj1.setWordWrap(false);
QCOMPARE(false, obj1.wordWrap());
obj1.setWordWrap(true);
QCOMPARE(true, obj1. wordWrap());
}
class QtTestModel: public QAbstractListModel
{
Q_OBJECT
public:
QtTestModel(int rc, int cc, QObject *parent = nullptr)
: QAbstractListModel(parent), rCount(rc), cCount(cc) {}
int rowCount(const QModelIndex &) const override { return rCount; }
int columnCount(const QModelIndex &) const override { return cCount; }
QVariant data(const QModelIndex &idx, int role) const override
{
if (!m_icon.isNull() && role == Qt::DecorationRole)
return m_icon;
if (role != Qt::DisplayRole)
return QVariant();
if (idx.row() < 0 || idx.column() < 0 || idx.column() >= cCount
|| idx.row() >= rCount) {
wrongIndex = true;
qWarning("got invalid modelIndex %d/%d", idx.row(), idx.column());
}
return QString::number(idx.row()) + QLatin1Char('/') + QString::number(idx.column());
}
void removeLastRow()
{
beginRemoveRows(QModelIndex(), rCount - 2, rCount - 1);
--rCount;
endRemoveRows();
}
void removeAllRows()
{
beginRemoveRows(QModelIndex(), 0, rCount - 1);
rCount = 0;
endRemoveRows();
}
void setDataIcon(const QIcon &icon)
{
m_icon = icon;
}
QIcon m_icon;
int rCount, cCount;
mutable bool wrongIndex = false;
};
class ScrollPerItemListView : public QListView
{
Q_OBJECT
public:
explicit ScrollPerItemListView(QWidget *parent = nullptr)
: QListView(parent)
{
// Force per item scroll mode since it comes from the style by default
setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
}
};
void tst_QListView::cleanup()
{
QTRY_VERIFY(QApplication::topLevelWidgets().isEmpty());
}
void tst_QListView::noDelegate()
{
QtTestModel model(10, 10);
QListView view;
view.setModel(&model);
view.setItemDelegate(nullptr);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
}
void tst_QListView::noModel()
{
QListView view;
view.show();
view.setRowHidden(0, true);
// no model -> not able to hide a row
QVERIFY(!view.isRowHidden(0));
}
void tst_QListView::emptyModel()
{
QtTestModel model(0, 0);
QListView view;
view.setModel(&model);
view.show();
QVERIFY(!model.wrongIndex);
}
void tst_QListView::removeRows()
{
QtTestModel model(10, 10);
QListView view;
view.setModel(&model);
view.show();
model.removeLastRow();
QVERIFY(!model.wrongIndex);
model.removeAllRows();
QVERIFY(!model.wrongIndex);
}
void tst_QListView::cursorMove()
{
int rows = 6*6;
int columns = 6;
QStandardItemModel model(rows, columns);
QWidget topLevel;
QListView view(&topLevel);
view.setModel(&model);
for (int j = 0; j < columns; ++j) {
const QString postfix = QLatin1Char(',') + QString::number(j) + QLatin1Char(']');
view.setModelColumn(j);
for (int i = 0; i < rows; ++i) {
QModelIndex index = model.index(i, j);
model.setData(index, QLatin1Char('[') + QString::number(i) + postfix);
view.setCurrentIndex(index);
QTRY_COMPARE(view.currentIndex(), index);
}
}
QSize cellsize(60, 25);
int gap = 1; // compensate for the scrollbars
int displayColumns = 6;
view.resize((displayColumns + gap) * cellsize.width(),
int((ceil(double(rows) / displayColumns) + gap) * cellsize.height()));
view.setResizeMode(QListView::Adjust);
view.setGridSize(cellsize);
view.setViewMode(QListView::IconMode);
view.doItemsLayout();
topLevel.show();
static const Qt::Key keymoves[] {
Qt::Key_Up, Qt::Key_Up, Qt::Key_Right, Qt::Key_Right, Qt::Key_Up,
Qt::Key_Left, Qt::Key_Left, Qt::Key_Up, Qt::Key_Down, Qt::Key_Up,
Qt::Key_Up, Qt::Key_Up, Qt::Key_Up, Qt::Key_Up, Qt::Key_Up,
Qt::Key_Left, Qt::Key_Left, Qt::Key_Up, Qt::Key_Down
};
int lastRow = rows / displayColumns - 1;
int lastColumn = displayColumns - 1;
int displayRow = lastRow;
int displayColumn = lastColumn - (rows % displayColumns);
QCoreApplication::processEvents();
for (Qt::Key key : keymoves) {
QTest::keyClick(&view, key);
switch (key) {
case Qt::Key_Up:
displayRow = qMax(0, displayRow - 1);
break;
case Qt::Key_Down:
displayRow = qMin(rows / displayColumns - 1, displayRow + 1);
break;
case Qt::Key_Left:
if (displayColumn > 0) {
displayColumn--;
} else {
if (displayRow > 0) {
displayRow--;
displayColumn = lastColumn;
}
}
break;
case Qt::Key_Right:
if (displayColumn < lastColumn) {
displayColumn++;
} else {
if (displayRow < lastRow) {
displayRow++;
displayColumn = 0;
}
}
break;
default:
QVERIFY(false);
}
QCoreApplication::processEvents();
int row = displayRow * displayColumns + displayColumn;
int column = columns - 1;
QModelIndex index = model.index(row, column);
QCOMPARE(view.currentIndex().row(), row);
QCOMPARE(view.currentIndex().column(), column);
QCOMPARE(view.currentIndex(), index);
}
}
void tst_QListView::hideRows()
{
QtTestModel model(10, 10);
QListView view;
view.setModel(&model);
view.show();
// hide then show
QVERIFY(!view.isRowHidden(2));
view.setRowHidden(2, true);
QVERIFY(view.isRowHidden(2));
view.setRowHidden(2, false);
QVERIFY(!view.isRowHidden(2));
// re show same row
QVERIFY(!view.isRowHidden(2));
view.setRowHidden(2, false);
QVERIFY(!view.isRowHidden(2));
// double hidding
QVERIFY(!view.isRowHidden(2));
view.setRowHidden(2, true);
QVERIFY(view.isRowHidden(2));
view.setRowHidden(2, true);
QVERIFY(view.isRowHidden(2));
view.setRowHidden(2, false);
QVERIFY(!view.isRowHidden(2));
// show in per-item mode, then hide the first row
view.setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
QVERIFY(!view.isRowHidden(0));
view.setRowHidden(0, true);
QVERIFY(view.isRowHidden(0));
view.setRowHidden(0, false);
QVERIFY(!view.isRowHidden(0));
QStandardItemModel sim;
QStandardItem *root = new QStandardItem("Root row");
for (int i = 0;i < 5; i++)
root->appendRow(new QStandardItem(QLatin1String("Row ") + QString::number(i)));
sim.appendRow(root);
view.setModel(&sim);
view.setRootIndex(root->index());
QVERIFY(!view.isRowHidden(0));
view.setRowHidden(0, true);
QVERIFY(view.isRowHidden(0));
view.setRowHidden(0, false);
QVERIFY(!view.isRowHidden(0));
}
void tst_QListView::moveCursor()
{
QtTestModel model(10, 10);
QListView view;
view.setModel(&model);
QTest::keyClick(&view, Qt::Key_Down);
view.setModel(nullptr);
view.setModel(&model);
view.setRowHidden(0, true);
QTest::keyClick(&view, Qt::Key_Down);
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0));
}
void tst_QListView::moveCursor2()
{
QtTestModel model(100, 1);
QPixmap pm(32, 32);
pm.fill(Qt::green);
model.setDataIcon(QIcon(pm));
PublicListView vu;
vu.setModel(&model);
vu.setIconSize(QSize(36,48));
vu.setGridSize(QSize(34,56));
//Standard framesize is 1. If Framesize > 2 increase size
int frameSize = qApp->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
vu.resize(300 + frameSize * 2, 300);
vu.setFlow(QListView::LeftToRight);
vu.setMovement(QListView::Static);
vu.setWrapping(true);
vu.setViewMode(QListView::IconMode);
vu.setLayoutMode(QListView::Batched);
vu.show();
vu.selectionModel()->setCurrentIndex(model.index(0,0), QItemSelectionModel::SelectCurrent);
QCoreApplication::processEvents();
QModelIndex idx = vu.moveCursor(PublicListView::MoveHome, Qt::NoModifier);
QCOMPARE(idx, model.index(0, 0));
idx = vu.moveCursor(PublicListView::MoveDown, Qt::NoModifier);
QCOMPARE(idx, model.index(8, 0));
}
void tst_QListView::moveCursor3()
{
//this tests is for task 159792
//it tests that navigation works even with non uniform item sizes
QListView view;
QStandardItemModel model(0, 1);
QStandardItem *i1 = new QStandardItem("First item, long name");
QStandardItem *i2 = new QStandardItem("2nd item");
QStandardItem *i3 = new QStandardItem("Third item, long name");
i1->setSizeHint(QSize(200, 32));
model.appendRow(i1);
model.appendRow(i2);
model.appendRow(i3);
view.setModel(&model);
view.setCurrentIndex(model.index(0, 0));
QCOMPARE(view.selectionModel()->currentIndex(), model.index(0, 0));
QTest::keyClick(&view, Qt::Key_Down);
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0));
QTest::keyClick(&view, Qt::Key_Down);
QCOMPARE(view.selectionModel()->currentIndex(), model.index(2, 0));
QTest::keyClick(&view, Qt::Key_Up);
QCOMPARE(view.selectionModel()->currentIndex(), model.index(1, 0));
QTest::keyClick(&view, Qt::Key_Up);
QCOMPARE(view.selectionModel()->currentIndex(), model.index(0, 0));
}
class QListViewShowEventListener : public QListView
{
Q_OBJECT
public:
using QListView::QListView;
void showEvent(QShowEvent *e) override
{
QListView::showEvent(e);
int columnwidth = sizeHintForColumn(0);
QSize sz = sizeHintForIndex(model()->index(0, 0));
// This should retrieve a model index in the 2nd section
m_index = indexAt(QPoint(columnwidth +2, sz.height() / 2));
m_shown = true;
}
QModelIndex m_index;
bool m_shown = false;
};
void tst_QListView::indexAt()
{
QtTestModel model(2, 1);
QListView view;
view.setModel(&model);
view.setViewMode(QListView::ListMode);
view.setFlow(QListView::TopToBottom);
QSize sz = view.sizeHintForIndex(model.index(0, 0));
QModelIndex index;
index = view.indexAt(QPoint(20, 0));
QVERIFY(index.isValid());
QCOMPARE(index.row(), 0);
index = view.indexAt(QPoint(20, sz.height()));
QVERIFY(index.isValid());
QCOMPARE(index.row(), 1);
index = view.indexAt(QPoint(20, 2 * sz.height()));
QVERIFY(!index.isValid());
// Check when peeking out of the viewport bounds
index = view.indexAt(QPoint(view.viewport()->rect().width(), 0));
QVERIFY(!index.isValid());
index = view.indexAt(QPoint(-1, 0));
QVERIFY(!index.isValid());
index = view.indexAt(QPoint(20, view.viewport()->rect().height()));
QVERIFY(!index.isValid());
index = view.indexAt(QPoint(20, -1));
QVERIFY(!index.isValid());
model.rCount = 30;
QListViewShowEventListener view2;
// Set the height to a small enough value so that it wraps to a new section.
view2.resize(300, 100);
view2.setModel(&model);
view2.setFlow(QListView::TopToBottom);
view2.setViewMode(QListView::ListMode);
view2.setWrapping(true);
// We really want to make sure it is shown, because the layout won't be known until it is shown
view2.show();
QVERIFY(QTest::qWaitForWindowExposed(&view2));
QTRY_VERIFY(view2.m_shown);
QVERIFY(view2.m_index.isValid());
QVERIFY(view2.m_index.row() != 0);
}
void tst_QListView::clicked()
{
QtTestModel model(10, 2);
QListView view;
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QModelIndex firstIndex = model.index(0, 0, QModelIndex());
QVERIFY(firstIndex.isValid());
int itemHeight = view.visualRect(firstIndex).height();
view.resize(200, itemHeight * (model.rCount + 1));
for (int i = 0; i < model.rCount; ++i) {
QPoint p(5, 1 + itemHeight * i);
QModelIndex index = view.indexAt(p);
if (!index.isValid())
continue;
QSignalSpy spy(&view, &QListView::clicked);
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::NoModifier, p);
QCOMPARE(spy.count(), 1);
}
}
void tst_QListView::singleSelectionRemoveRow()
{
QStringListModel model({"item1", "item2", "item3", "item4"});
QListView view;
view.setModel(&model);
view.show();
QModelIndex index;
view.setCurrentIndex(model.index(1));
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("item2"));
model.removeRow(1);
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("item3"));
model.removeRow(0);
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("item3"));
}
void tst_QListView::singleSelectionRemoveColumn()
{
int numCols = 3;
int numRows = 3;
QStandardItemModel model(numCols, numRows);
for (int r = 0; r < numRows; ++r) {
const QString prefix = QString::number(r) + QLatin1Char(',');
for (int c = 0; c < numCols; ++c)
model.setData(model.index(r, c), prefix + QString::number(c));
}
QListView view;
view.setModel(&model);
view.show();
QModelIndex index;
view.setCurrentIndex(model.index(1, 1));
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("1,1"));
model.removeColumn(1);
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("1,0"));
model.removeColumn(0);
index = view.currentIndex();
QCOMPARE(view.model()->data(index).toString(), QString("1,2"));
}
void tst_QListView::modelColumn()
{
int numCols = 3;
int numRows = 3;
QStandardItemModel model(numCols, numRows);
for (int r = 0; r < numRows; ++r) {
const QString prefix = QString::number(r) + QLatin1Char(',');
for (int c = 0; c < numCols; ++c)
model.setData(model.index(r, c), prefix + QString::number(c));
}
QListView view;
view.setModel(&model);
//
// Set and get with a valid model
//
// Default is column 0
QCOMPARE(view.modelColumn(), 0);
view.setModelColumn(0);
QCOMPARE(view.modelColumn(), 0);
view.setModelColumn(1);
QCOMPARE(view.modelColumn(), 1);
view.setModelColumn(2);
QCOMPARE(view.modelColumn(), 2);
// Out of bound cases should not modify the modelColumn
view.setModelColumn(-1);
QCOMPARE(view.modelColumn(), 2);
view.setModelColumn(std::numeric_limits<int>::max());
QCOMPARE(view.modelColumn(), 2);
// See if it displays the right column using indexAt()...
view.resize(400,400);
view.show();
for (int c = 0; c < 3; ++c) {
view.setModelColumn(c);
int startrow = 0;
for (int y = 0; y < view.height(); ++y) {
QModelIndex idx = view.indexAt( QPoint(1, y) );
if (idx.row() == startrow + 1) ++startrow;
else if (idx.row() == -1) break;
QCOMPARE(idx.row(), startrow);
QCOMPARE(idx.column(), c);
}
QCOMPARE(startrow, 2);
}
}
void tst_QListView::hideFirstRow()
{
QStringListModel model(generateList(QLatin1String("item"), 100));
QListView view;
view.setModel(&model);
view.setUniformItemSizes(true);
view.setRowHidden(0, true);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
}
static int modelIndexCount(const QAbstractItemView *view)
{
QBitArray ba;
for (int y = 0, height = view->height(); y < height; ++y) {
const QModelIndex idx = view->indexAt( QPoint(1, y) );
if (!idx.isValid())
break;
if (idx.row() >= ba.size())
ba.resize(idx.row() + 1);
ba.setBit(idx.row(), true);
}
return ba.size();
}
void tst_QListView::batchedMode()
{
const int rowCount = 3;
QStringListModel model(generateList(QLatin1String("item "), rowCount));
QListView view;
view.setWindowTitle(QTest::currentTestFunction());
view.setModel(&model);
view.setUniformItemSizes(true);
view.setViewMode(QListView::ListMode);
view.setLayoutMode(QListView::Batched);
view.setBatchSize(2);
view.resize(200, 400);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QTRY_COMPARE(modelIndexCount(&view), rowCount);
// Test the dynamic listview too.
view.setViewMode(QListView::IconMode);
view.setLayoutMode(QListView::Batched);
view.setFlow(QListView::TopToBottom);
view.setBatchSize(2);
QTRY_COMPARE(modelIndexCount(&view), rowCount);
}
void tst_QListView::setCurrentIndex()
{
QStringListModel model(generateList(QLatin1String("item "), 20));
ScrollPerItemListView view;
view.setModel(&model);
view.resize(220,182);
view.show();
for (int pass = 0; pass < 2; ++pass) {
view.setFlow(pass == 0 ? QListView::TopToBottom : QListView::LeftToRight);
QScrollBar *sb = pass == 0 ? view.verticalScrollBar() : view.horizontalScrollBar();
for (const QSize &gridSize : {QSize(), QSize(200, 38)}) {
if (pass == 1 && !gridSize.isValid()) // the width of an item varies, so it might jump two times
continue;
view.setGridSize(gridSize);
QCoreApplication::processEvents();
int offset = sb->value();
// first "scroll" down, verify that we scroll one step at a time
int i = 0;
for (i = 0; i < 20; ++i) {
QModelIndex idx = model.index(i,0);
view.setCurrentIndex(idx);
if (offset != sb->value()) {
// If it has scrolled, it should have scrolled only by one.
QCOMPARE(sb->value(), offset + 1);
++offset;
}
}
--i; // item 20 does not exist
// and then "scroll" up, verify that we scroll one step at a time
for (; i >= 0; --i) {
QModelIndex idx = model.index(i,0);
view.setCurrentIndex(idx);
if (offset != sb->value()) {
// If it has scrolled, it should have scrolled only by one.
QCOMPARE(sb->value(), offset - 1);
--offset;
}
}
}
}
while (model.rowCount()) {
view.setCurrentIndex(model.index(model.rowCount() - 1, 0));
model.removeRow(model.rowCount() - 1);
}
}
class TestDelegate : public QStyledItemDelegate
{
public:
explicit TestDelegate(QObject *parent, const QSize &sizeHint = QSize(50, 50))
: QStyledItemDelegate(parent), m_sizeHint(sizeHint) {}
QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const override { return m_sizeHint; }
const QSize m_sizeHint;
};
void tst_QListView::selection_data()
{
QTest::addColumn<int>("itemCount");
QTest::addColumn<QListView::ViewMode>("viewMode");
QTest::addColumn<QListView::Flow>("flow");
QTest::addColumn<bool>("wrapping");
QTest::addColumn<int>("spacing");
QTest::addColumn<QSize>("gridSize");
QTest::addColumn<IntList>("hiddenRows");
QTest::addColumn<QRect>("selectionRect");
QTest::addColumn<IntList>("expectedItems");
QTest::newRow("select all")
<< 4 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< false // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(0, 0, 10, 200) // selection rectangle
<< (IntList() << 0 << 1 << 2 << 3); // expected items
QTest::newRow("select below, (on viewport)")
<< 4 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< false // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(10, 250, 1, 1) // selection rectangle
<< IntList(); // expected items
QTest::newRow("select below 2, (on viewport)")
<< 4 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(10, 250, 1, 1) // selection rectangle
<< IntList(); // expected items
QTest::newRow("select to the right, (on viewport)")
<< 40 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(300, 10, 1, 1) // selection rectangle
<< IntList(); // expected items
QTest::newRow("select to the right 2, (on viewport)")
<< 40 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(300, 0, 1, 300) // selection rectangle
<< IntList(); // expected items
QTest::newRow("select inside contents, (on viewport)")
<< 35 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(175, 275, 1, 1) // selection rectangle
<< IntList(); // expected items
QTest::newRow("select a tall rect in LeftToRight flow, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::LeftToRight
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(90, 90, 1, 100) // selection rectangle
<< (IntList() // expected items
<< 11 << 12 << 13 << 14 << 15 << 16 << 17 << 18 << 19
<< 20 << 21 << 22 << 23 << 24 << 25 << 26 << 27 << 28 << 29
<< 30 << 31);
QTest::newRow("select a wide rect in LeftToRight, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::LeftToRight
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(90, 90, 200, 1) // selection rectangle
<< (IntList() // expected items
<< 11 << 12 << 13 << 14 << 15);
QTest::newRow("select a wide negative rect in LeftToRight flow, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::LeftToRight
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(290, 90, -200, 1) // selection rectangle
<< (IntList() // expected items
<< 11 << 12 << 13 << 14 << 15);
QTest::newRow("select a tall rect in TopToBottom flow, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(90, 90, 1, 100) // selection rectangle
<< (IntList() // expected items
<< 11
<< 12
<< 13);
QTest::newRow("select a tall negative rect in TopToBottom flow, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(90, 190, 1, -100) // selection rectangle
<< (IntList() // expected items
<< 11
<< 12
<< 13);
QTest::newRow("select a wide rect in TopToBottom, wrap items")
<< 70 // itemCount
<< QListView::ListMode
<< QListView::TopToBottom
<< true // wrapping
<< 0 // spacing
<< QSize() // gridSize
<< IntList() // hiddenRows
<< QRect(90, 90, 100, 1) // selection rectangle
<< (IntList() // expected items
<< 20 << 30
<< 11 << 21 << 31
<< 12 << 22
<< 13 << 23
<< 14 << 24
<< 15 << 25
<< 16 << 26
<< 17 << 27
<< 18 << 28
<< 19 << 29);
}
void tst_QListView::selection()
{
QFETCH(int, itemCount);
QFETCH(QListView::ViewMode, viewMode);
QFETCH(QListView::Flow, flow);
QFETCH(bool, wrapping);
QFETCH(int, spacing);
QFETCH(QSize, gridSize);
QFETCH(const IntList, hiddenRows);
QFETCH(QRect, selectionRect);
QFETCH(const IntList, expectedItems);
QWidget topLevel;
PublicListView v(&topLevel);
QtTestModel model(itemCount, 1);
// avoid scrollbar size mismatches among different styles
v.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
v.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
v.setItemDelegate(new TestDelegate(&v));
v.setModel(&model);
v.setViewMode(viewMode);
v.setFlow(flow);
v.setWrapping(wrapping);
v.setResizeMode(QListView::Adjust);
v.setSpacing(spacing);
if (gridSize.isValid())
v.setGridSize(gridSize);
for (int row : hiddenRows)
v.setRowHidden(row, true);
v.resize(525, 525);
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
v.setSelection(selectionRect, QItemSelectionModel::ClearAndSelect);
const QModelIndexList selected = v.selectionModel()->selectedIndexes();
QCOMPARE(selected.count(), expectedItems.count());
for (const auto &idx : selected)
QVERIFY(expectedItems.contains(idx.row()));
}
void tst_QListView::scrollTo()
{
QWidget topLevel;
setFrameless(&topLevel);
ScrollPerItemListView lv(&topLevel);
QStringListModel model(&lv);
QStringList list;
list << "Short item 1";
list << "Short item 2";
list << "Short item 3";
list << "Short item 4";
list << "Short item 5";
list << "Short item 6";
list << "Begin This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\n"
"This is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item\nThis is a very long item End\n";
list << "Short item";
list << "Short item";
list << "Short item";
list << "Short item";
list << "Short item";
list << "Short item";
list << "Short item";
list << "Short item";
model.setStringList(list);
lv.setModel(&model);
lv.setFixedSize(110, 200);
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
//by default, the list view scrolls per item and has no wrapping
QModelIndex index = model.index(6, 0);
//we save the size of the item for later comparisons
const QSize itemsize = lv.visualRect(index).size();
QVERIFY(itemsize.height() > lv.height());
QVERIFY(itemsize.width() > lv.width());
//we click the item
QPoint p = lv.visualRect(index).center();
QTest::mouseClick(lv.viewport(), Qt::LeftButton, Qt::NoModifier, p);
//let's wait because the scrolling is delayed
QTRY_COMPARE(lv.visualRect(index).y(), 0);
//we scroll down. As the item is to tall for the view, it will disappear
QTest::keyClick(lv.viewport(), Qt::Key_Down, Qt::NoModifier);
QTRY_COMPARE(lv.visualRect(index).y(), -itemsize.height());
QTest::keyClick(lv.viewport(), Qt::Key_Up, Qt::NoModifier);
QTRY_COMPARE(lv.visualRect(index).y(), 0);
//Let's enable wrapping
lv.setWrapping(true);
lv.horizontalScrollBar()->setValue(0); //let's scroll to the beginning
//we click the item
p = lv.visualRect(index).center();
QTest::mouseClick(lv.viewport(), Qt::LeftButton, Qt::NoModifier, p);
QTRY_COMPARE(lv.visualRect(index).x(), 0);
//we scroll right. As the item is too wide for the view, it will disappear
QTest::keyClick(lv.viewport(), Qt::Key_Right, Qt::NoModifier);
QTRY_COMPARE(lv.visualRect(index).x(), -itemsize.width());
QTest::keyClick(lv.viewport(), Qt::Key_Left, Qt::NoModifier);
QTRY_COMPARE(lv.visualRect(index).x(), 0);
lv.setWrapping(false);
QCoreApplication::processEvents(); //let the layout happen
//Let's try with scrolling per pixel
lv.setHorizontalScrollMode(QListView::ScrollPerPixel);
lv.verticalScrollBar()->setValue(0); //scrolls back to the first item
//we click the item
p = lv.visualRect(index).center();
QTest::mouseClick(lv.viewport(), Qt::LeftButton, Qt::NoModifier, p);
//let's wait because the scrolling is delayed
QTest::qWait(QApplication::doubleClickInterval() + 150);
QTRY_COMPARE(lv.visualRect(index).y(), 0);
//we scroll down. As the item is too tall for the view, it will partially disappear
QTest::keyClick(lv.viewport(), Qt::Key_Down, Qt::NoModifier);
QVERIFY(lv.visualRect(index).y() < 0);
QTest::keyClick(lv.viewport(), Qt::Key_Up, Qt::NoModifier);
QCOMPARE(lv.visualRect(index).y(), 0);
}
void tst_QListView::scrollBarRanges()
{
const int rowCount = 10;
const int rowHeight = 20;
QWidget topLevel;
ScrollPerItemListView lv(&topLevel);
QStringListModel model(&lv);
model.setStringList(generateList(QLatin1String("Item "), rowCount));
lv.setModel(&model);
lv.resize(250, 130);
lv.setItemDelegate(new TestDelegate(&lv, QSize(100, rowHeight)));
topLevel.show();
for (int h = 30; h <= 210; ++h) {
lv.resize(250, h);
int visibleRowCount = lv.viewport()->size().height() / rowHeight;
int invisibleRowCount = rowCount - visibleRowCount;
QTRY_COMPARE(lv.verticalScrollBar()->maximum(), invisibleRowCount);
}
}
void tst_QListView::scrollBarAsNeeded_data()
{
QTest::addColumn<QSize>("size");
QTest::addColumn<int>("itemCount");
QTest::addColumn<QAbstractItemView::ScrollMode>("verticalScrollMode");
QTest::addColumn<QMargins>("viewportMargins");
QTest::addColumn<QSize>("delegateSize");
QTest::addColumn<QListView::Flow>("flow");
QTest::addColumn<bool>("horizontalScrollBarVisible");
QTest::addColumn<bool>("verticalScrollBarVisible");
QTest::newRow("TopToBottom, count:0")
<< QSize(200, 100)
<< 0
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::TopToBottom
<< false
<< false;
QTest::newRow("TopToBottom, count:1")
<< QSize(200, 100)
<< 1
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::TopToBottom
<< false
<< false;
QTest::newRow("TopToBottom, count:20")
<< QSize(200, 100)
<< 20
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::TopToBottom
<< false
<< true;
QTest::newRow("TopToBottom, fixed size, count:4")
<< QSize(200, 200)
<< 4
<< QListView::ScrollPerPixel
<< QMargins() << QSize(40, 40)
<< QListView::TopToBottom
<< false
<< false;
// QTBUG-61383, vertical case: take viewport margins into account
QTest::newRow("TopToBottom, fixed size, vertical margins, count:4")
<< QSize(200, 200)
<< 4
<< QListView::ScrollPerPixel
<< QMargins(0, 50, 0, 50) << QSize(40, 40)
<< QListView::TopToBottom
<< false
<< true;
// QTBUG-61383, horizontal case: take viewport margins into account
QTest::newRow("TopToBottom, fixed size, horizontal margins, count:4")
<< QSize(200, 200)
<< 4
<< QListView::ScrollPerPixel
<< QMargins(50, 0, 50, 0) << QSize(120, 40)
<< QListView::TopToBottom
<< true
<< false;
QTest::newRow("LeftToRight, count:0")
<< QSize(200, 100)
<< 0
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::LeftToRight
<< false
<< false;
QTest::newRow("LeftToRight, count:1")
<< QSize(200, 100)
<< 1
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::LeftToRight
<< false
<< false;
QTest::newRow("LeftToRight, count:20")
<< QSize(200, 100)
<< 20
<< QListView::ScrollPerItem
<< QMargins() << QSize()
<< QListView::LeftToRight
<< true
<< false;
}
void tst_QListView::scrollBarAsNeeded()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
QFETCH(QSize, size);
QFETCH(int, itemCount);
QFETCH(QAbstractItemView::ScrollMode, verticalScrollMode);
QFETCH(QMargins, viewportMargins);
QFETCH(QSize, delegateSize);
QFETCH(QListView::Flow, flow);
QFETCH(bool, horizontalScrollBarVisible);
QFETCH(bool, verticalScrollBarVisible);
constexpr int rowCounts[3] = {0, 1, 20};
QWidget topLevel;
topLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QStringLiteral("::")
+ QLatin1String(QTest::currentDataTag()));
PublicListView lv(&topLevel);
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
lv.setVerticalScrollMode(verticalScrollMode);
lv.setViewportMargins(viewportMargins);
lv.setFlow(flow);
if (!delegateSize.isEmpty())
lv.setItemDelegate(new TestDelegate(&lv, delegateSize));
QStringListModel model(&lv);
lv.setModel(&model);
lv.resize(size);
topLevel.show();
QVERIFY(QTest::qWaitForWindowActive(&topLevel));
for (uint r = 0; r < sizeof(rowCounts) / sizeof(int); ++r) {
model.setStringList(generateList(QLatin1String("Item "), rowCounts[r]));
model.setStringList(generateList(QLatin1String("Item "), itemCount));
QTRY_COMPARE(lv.horizontalScrollBar()->isVisible(), horizontalScrollBarVisible);
QTRY_COMPARE(lv.verticalScrollBar()->isVisible(), verticalScrollBarVisible);
}
}
void tst_QListView::moveItems()
{
QStandardItemModel model;
for (int r = 0; r < 4; ++r) {
const QString prefix = QLatin1String("standard item (") + QString::number(r) + QLatin1Char(',');
for (int c = 0; c < 4; ++c)
model.setItem(r, c, new QStandardItem(prefix + QString::number(c) + QLatin1Char(')')));
}
PublicListView view;
view.setViewMode(QListView::IconMode);
view.setResizeMode(QListView::Fixed);
view.setWordWrap(true);
view.setModel(&model);
view.setItemDelegate(new TestDelegate(&view));
for (int r = 0; r < model.rowCount(); ++r) {
for (int c = 0; c < model.columnCount(); ++c) {
const QModelIndex& idx = model.index(r, c);
view.setPositionForIndex(QPoint(r * 75, r * 75), idx);
}
}
QCOMPARE(view.contentsSize(), QSize(275, 275));
}
void tst_QListView::wordWrap()
{
QListView lv;
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
QStringListModel model(&lv);
QStringList list;
list << "Short item 1";
list << "Short item 2";
list << "Short item 3";
list << "Begin\nThis item take severals Lines\nEnd";
list << "And this is a very long item very long item this is a very vary vary long item"
"very long very very long long long this is a long item a very long item a very very long item";
list << "And this is a second even a little more long very long item very long item this is a very vary vary long item"
"very long very very long long long this is a long item a very long item a very very long item";
list << "Short item";
list << "rzeofig zerig fslfgj smdlfkgj qmsdlfj amrzriougf qsla zrg fgsdf gsdfg sdfgs dfg sdfgcvb sdfg qsdjfh qsdfjklh qs";
list << "Short item";
model.setStringList(list);
lv.setModel(&model);
lv.setWordWrap(true);
lv.setFixedSize(400, 150);
lv.showNormal();
QTRY_COMPARE(lv.horizontalScrollBar()->isVisible(), false);
#ifdef Q_OS_WINRT
QSKIP("setFixedSize does not work on WinRT. Vertical scroll bar will not be visible.");
#endif
QTRY_COMPARE(lv.verticalScrollBar()->isVisible(), true);
}
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
class SetCurrentIndexAfterAppendRowCrashDialog : public QDialog
{
Q_OBJECT
public:
SetCurrentIndexAfterAppendRowCrashDialog()
{
setWindowTitle(QTest::currentTestFunction());
listView = new QListView(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(listView);
listView->setViewMode(QListView::IconMode);
model = new QStandardItemModel(this);
listView->setModel(model);
timer = new QTimer(this);
connect(timer, &QTimer::timeout,
this, &SetCurrentIndexAfterAppendRowCrashDialog::buttonClicked);
timer->start(1000);
}
protected:
void showEvent(QShowEvent *event) override
{
QDialog::showEvent(event);
DWORD lParam = 0xFFFFFFFC/*OBJID_CLIENT*/;
DWORD wParam = 0;
if (const HWND hwnd = getHWNDForWidget(this))
SendMessage(hwnd, WM_GETOBJECT, wParam, lParam);
}
private slots:
void buttonClicked()
{
timer->stop();
QStandardItem *item = new QStandardItem("test");
model->appendRow(item);
listView->setCurrentIndex(model->indexFromItem(item));
close();
}
private:
QListView *listView;
QStandardItemModel *model;
QTimer *timer;
};
void tst_QListView::setCurrentIndexAfterAppendRowCrash()
{
SetCurrentIndexAfterAppendRowCrashDialog w;
w.exec();
}
#endif // Q_OS_WIN && !Q_OS_WINRT
void tst_QListView::emptyItemSize()
{
QStandardItemModel model;
for (int r = 0; r < 4; ++r) {
const QString text = QLatin1String("standard item (") + QString::number(r) + QLatin1Char(')');
model.setItem(r, new QStandardItem(text));
}
model.setItem(4, 0, new QStandardItem());
PublicListView view;
view.setModel(&model);
for (int i = 0; i < 5; ++i)
QVERIFY(!view.visualRect(model.index(i, 0)).isEmpty());
}
void tst_QListView::task203585_selectAll()
{
//we make sure that "select all" doesn't select the hidden items
QListView view;
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
view.setModel(new QStringListModel({"foo"}, &view));
view.setRowHidden(0, true);
view.selectAll();
QVERIFY(view.selectionModel()->selectedIndexes().isEmpty());
view.setRowHidden(0, false);
view.selectAll();
QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1);
}
void tst_QListView::task228566_infiniteRelayout()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
QListView view;
QStringList list;
for (int i = 0; i < 10; ++i)
list << "small";
list << "BIGBIGBIGBIGBIGBIGBIGBIGBIGBIGBIGBIG"
<< "BIGBIGBIGBIGBIGBIGBIGBIGBIGBIGBIGBIG";
QStringListModel model(list);
view.setModel(&model);
view.setWrapping(true);
view.setResizeMode(QListView::Adjust);
const int itemHeight = view.visualRect( model.index(0, 0)).height();
view.setFixedHeight(itemHeight * 12);
view.show();
QVERIFY(QTest::qWaitForWindowActive(&view));
QTest::qWait(100); //make sure the layout is done once
QSignalSpy spy(view.horizontalScrollBar(), &QScrollBar::rangeChanged);
//the layout should already have been done
//so there should be no change made to the scrollbar
QVERIFY(!spy.wait(200));
}
void tst_QListView::task248430_crashWith0SizedItem()
{
QListView view;
view.setViewMode(QListView::IconMode);
QStringListModel model({QLatin1String("item1"), QString()});
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QTest::qWait(20);
}
void tst_QListView::task250446_scrollChanged()
{
QStandardItemModel model(200, 1);
QListView view;
view.setModel(&model);
QModelIndex index = model.index(0, 0);
QVERIFY(index.isValid());
view.setCurrentIndex(index);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
const int scrollValue = view.verticalScrollBar()->maximum();
view.verticalScrollBar()->setValue(scrollValue);
QTRY_COMPARE(view.verticalScrollBar()->value(), scrollValue);
QTRY_COMPARE(view.currentIndex(), index);
view.showMinimized();
QTRY_COMPARE(view.verticalScrollBar()->value(), scrollValue);
QTRY_COMPARE(view.currentIndex(), index);
view.showNormal();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QTRY_COMPARE(view.verticalScrollBar()->value(), scrollValue);
QTRY_COMPARE(view.currentIndex(), index);
}
void tst_QListView::task196118_visualRegionForSelection()
{
PublicListView view;
QStandardItemModel model;
QStandardItem top1("top1");
QStandardItem sub1("sub1");
top1.appendRow(&sub1);
model.appendColumn({&top1});
view.setModel(&model);
view.setRootIndex(top1.index());
view.selectionModel()->select(top1.index(), QItemSelectionModel::Select);
QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1);
QVERIFY(view.getVisualRegionForSelection().isEmpty());
}
void tst_QListView::task254449_draggingItemToNegativeCoordinates()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
//we'll check that the items are painted correctly
PublicListView list;
QStandardItemModel model(1, 1);
QModelIndex index = model.index(0, 0);
model.setData(index, QLatin1String("foo"));
list.setModel(&model);
list.setViewMode(QListView::IconMode);
list.show();
list.activateWindow();
QVERIFY(QTest::qWaitForWindowActive(&list));
class MyItemDelegate : public QStyledItemDelegate
{
public:
using QStyledItemDelegate::QStyledItemDelegate;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
numPaints++;
QStyledItemDelegate::paint(painter, option, index);
}
mutable int numPaints = 0;
} delegate;
list.setItemDelegate(&delegate);
QTRY_VERIFY(delegate.numPaints > 0); //makes sure the layout is done
//we'll make sure the item is repainted
delegate.numPaints = 0;
const QPoint topLeft(-6, 0);
list.setPositionForIndex(topLeft, index);
QTRY_COMPARE(delegate.numPaints, 1);
QCOMPARE(list.visualRect(index).topLeft(), topLeft);
}
void tst_QListView::keyboardSearch()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
QStringListModel model({"AB", "AC", "BA", "BB", "BD", "KAFEINE",
"KONQUEROR", "KOPETE", "KOOKA", "OKULAR"});
QListView view;
view.setModel(&model);
view.show();
QApplication::setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowActive(&view));
QTest::keyClick(&view, Qt::Key_K);
QTRY_COMPARE(view.currentIndex() , model.index(5, 0)); //KAFEINE
QTest::keyClick(&view, Qt::Key_O);
QTRY_COMPARE(view.currentIndex() , model.index(6, 0)); //KONQUEROR
QTest::keyClick(&view, Qt::Key_N);
QTRY_COMPARE(view.currentIndex() , model.index(6, 0)); //KONQUEROR
}
void tst_QListView::shiftSelectionWithNonUniformItemSizes()
{
// This checks that no items are selected unexpectedly by Shift-Arrow
// when items with non-uniform sizes are laid out in a grid
{ // First test: QListView::LeftToRight flow
QStringListModel model({"Long\nText", "Text", "Text","Text"});
QListView view;
view.setFixedSize(250, 250);
view.setFlow(QListView::LeftToRight);
view.setGridSize(QSize(100, 100));
view.setSelectionMode(QListView::ExtendedSelection);
view.setViewMode(QListView::IconMode);
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
// Verfify that item sizes are non-uniform
QVERIFY(view.sizeHintForIndex(model.index(0, 0)).height() > view.sizeHintForIndex(model.index(1, 0)).height());
QModelIndex index = model.index(3, 0);
view.setCurrentIndex(index);
QCOMPARE(view.currentIndex(), index);
QTest::keyClick(&view, Qt::Key_Up, Qt::ShiftModifier);
QTRY_COMPARE(view.currentIndex(), model.index(1, 0));
QModelIndexList selected = view.selectionModel()->selectedIndexes();
QCOMPARE(selected.count(), 3);
QVERIFY(!selected.contains(model.index(0, 0)));
}
{ // Second test: QListView::TopToBottom flow
QStringListModel model({"ab", "a", "a", "a"});
QListView view;
view.setFixedSize(250, 250);
view.setFlow(QListView::TopToBottom);
view.setGridSize(QSize(100, 100));
view.setSelectionMode(QListView::ExtendedSelection);
view.setViewMode(QListView::IconMode);
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
// Verfify that item sizes are non-uniform
QVERIFY(view.sizeHintForIndex(model.index(0, 0)).width() > view.sizeHintForIndex(model.index(1, 0)).width());
QModelIndex index = model.index(3, 0);
view.setCurrentIndex(index);
QCOMPARE(view.currentIndex(), index);
QTest::keyClick(&view, Qt::Key_Left, Qt::ShiftModifier);
QTRY_COMPARE(view.currentIndex(), model.index(1, 0));
QModelIndexList selected = view.selectionModel()->selectedIndexes();
QCOMPARE(selected.count(), 3);
QVERIFY(!selected.contains(model.index(0, 0)));
}
}
void tst_QListView::shiftSelectionWithItemAlignment()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
QStringList items;
for (int c = 0; c < 2; c++) {
for (int i = 10; i > 0; i--)
items << QString(i, QLatin1Char('*'));
for (int i = 1; i < 11; i++)
items << QString(i, QLatin1Char('*'));
}
QListView view;
view.setFlow(QListView::TopToBottom);
view.setWrapping(true);
view.setItemAlignment(Qt::AlignLeft);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
QStringListModel model(items);
view.setModel(&model);
QFont font = view.font();
font.setPixelSize(10);
view.setFont(font);
view.resize(300, view.sizeHintForRow(0) * items.size() / 2 + view.horizontalScrollBar()->height());
view.show();
QApplication::setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowActive(&view));
QCOMPARE(static_cast<QWidget *>(&view), QApplication::activeWindow());
QModelIndex index1 = view.model()->index(items.size() / 4, 0);
QPoint p = view.visualRect(index1).center();
QVERIFY(view.viewport()->rect().contains(p));
QTest::mouseClick(view.viewport(), Qt::LeftButton, {}, p);
QCOMPARE(view.currentIndex(), index1);
QCOMPARE(view.selectionModel()->selectedIndexes().size(), 1);
QModelIndex index2 = view.model()->index(items.size() / 4 * 3, 0);
p = view.visualRect(index2).center();
QVERIFY(view.viewport()->rect().contains(p));
QTest::mouseClick(view.viewport(), Qt::LeftButton, Qt::ShiftModifier, p);
QCOMPARE(view.currentIndex(), index2);
QCOMPARE(view.selectionModel()->selectedIndexes().size(), index2.row() - index1.row() + 1);
}
void tst_QListView::clickOnViewportClearsSelection()
{
QStringListModel model({"Text1"});
QListView view;
view.setModel(&model);
view.setSelectionMode(QListView::ExtendedSelection);
view.selectAll();
QModelIndex index = model.index(0);
QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1);
QVERIFY(view.selectionModel()->isSelected(index));
//we try to click outside of the index
const QPoint point = view.visualRect(index).bottomRight() + QPoint(10,10);
QTest::mousePress(view.viewport(), Qt::LeftButton, {}, point);
//at this point, the selection shouldn't have changed
QCOMPARE(view.selectionModel()->selectedIndexes().count(), 1);
QVERIFY(view.selectionModel()->isSelected(index));
QTest::mouseRelease(view.viewport(), Qt::LeftButton, {}, point);
//now the selection should be cleared
QVERIFY(!view.selectionModel()->hasSelection());
}
void tst_QListView::task262152_setModelColumnNavigate()
{
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
QSKIP("Wayland: This fails. Figure out why.");
QListView view;
QStandardItemModel model(3,2);
model.setItem(0, 1, new QStandardItem("[0,1]"));
model.setItem(1, 1, new QStandardItem("[1,1]"));
model.setItem(2, 1, new QStandardItem("[2,1]"));
view.setModel(&model);
view.setModelColumn(1);
view.show();
QApplication::setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowActive(&view));
QCOMPARE(&view, QApplication::activeWindow());
QTest::keyClick(&view, Qt::Key_Down);
QTRY_COMPARE(view.currentIndex(), model.index(1, 1));
QTest::keyClick(&view, Qt::Key_Down);
QTRY_COMPARE(view.currentIndex(), model.index(2, 1));
}
void tst_QListView::taskQTBUG_2233_scrollHiddenItems_data()
{
QTest::addColumn<QListView::Flow>("flow");
QTest::newRow("TopToBottom") << QListView::TopToBottom;
QTest::newRow("LeftToRight") << QListView::LeftToRight;
}
void tst_QListView::taskQTBUG_2233_scrollHiddenItems()
{
QFETCH(QListView::Flow, flow);
const int rowCount = 200;
QWidget topLevel;
setFrameless(&topLevel);
ScrollPerItemListView view(&topLevel);
QStringListModel model(&view);
model.setStringList(generateList(QString(), rowCount));
view.setModel(&model);
view.setUniformItemSizes(true);
view.setViewMode(QListView::ListMode);
for (int i = 0; i < rowCount / 2; ++i)
view.setRowHidden(2 * i, true);
view.setFlow(flow);
view.resize(130, 130);
for (int i = 0; i < 10; ++i) {
(view.flow() == QListView::TopToBottom
? view.verticalScrollBar()
: view.horizontalScrollBar())->setValue(i);
QModelIndex index = view.indexAt(QPoint(0, 0));
QVERIFY(index.isValid());
QCOMPARE(index.row(), 2 * i + 1);
}
//QTBUG-7929 should not crash
topLevel.show();
QVERIFY(QTest::qWaitForWindowExposed(&topLevel));
QScrollBar *bar = view.flow() == QListView::TopToBottom
? view.verticalScrollBar() : view.horizontalScrollBar();
int nbVisibleItem = rowCount / 2 - bar->maximum();
bar->setValue(bar->maximum());
for (int i = rowCount; i > rowCount / 2; i--)
view.setRowHidden(i, true);
QTRY_COMPARE(bar->maximum(), rowCount / 4 - nbVisibleItem);
QCOMPARE(bar->value(), bar->maximum());
}
void tst_QListView::taskQTBUG_633_changeModelData()
{
QListView view;
view.setFlow(QListView::LeftToRight);
QStandardItemModel model(5,1);
for (int i = 0; i < model.rowCount(); ++i)
model.setData(model.index(i, 0), QString::number(i));
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
model.setData( model.index(1, 0), QLatin1String("long long text"));
const auto longTextDoesNotIntersectNextItem = [&]() {
QRect rectLongText = view.visualRect(model.index(1,0));
QRect rect2 = view.visualRect(model.index(2,0));
return !rectLongText.intersects(rect2);
};
QTRY_VERIFY(longTextDoesNotIntersectNextItem());
}
void tst_QListView::taskQTBUG_435_deselectOnViewportClick()
{
QListView view;
QStringListModel model({"1", "2", "3", "4"});
view.setModel(&model);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);
view.selectAll();
QCOMPARE(view.selectionModel()->selectedIndexes().count(), model.rowCount());
const QRect itemRect = view.visualRect(model.index(model.rowCount() - 1));
QPoint p = view.visualRect(model.index(model.rowCount() - 1)).center() + QPoint(0, itemRect.height());
//first the left button
QTest::mouseClick(view.viewport(), Qt::LeftButton, {}, p);
QVERIFY(!view.selectionModel()->hasSelection());
view.selectAll();
QCOMPARE(view.selectionModel()->selectedIndexes().count(), model.rowCount());
//and now the right button
QTest::mouseClick(view.viewport(), Qt::RightButton, {}, p);
QVERIFY(!view.selectionModel()->hasSelection());
}
void tst_QListView::taskQTBUG_2678_spacingAndWrappedText()
{
static const QString lorem("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
QStringListModel model({lorem, lorem, "foo", lorem, "bar", lorem, lorem});
QListView w;
w.setModel(&model);
w.setViewMode(QListView::ListMode);
w.setWordWrap(true);
w.setSpacing(10);
w.show();
QVERIFY(QTest::qWaitForWindowExposed(&w));
QCOMPARE(w.horizontalScrollBar()->minimum(), w.horizontalScrollBar()->maximum());
}
void tst_QListView::taskQTBUG_5877_skippingItemInPageDownUp()
{
QtTestModel model(100, 1);
static const int currentItemIndexes[] =
{0, 6, 16, 25, 34, 42, 57, 68, 77, 83, 91, 94};
PublicListView vu;
vu.setModel(&model);
vu.show();
QVERIFY(QTest::qWaitForWindowExposed(&vu));
int itemHeight = vu.visualRect(model.index(0, 0)).height();
int visibleRowCount = vu.viewport()->height() / itemHeight;
int scrolledRowCount = visibleRowCount - 1;
for (int currentItemIndex : currentItemIndexes) {
vu.selectionModel()->setCurrentIndex(model.index(currentItemIndex, 0),
QItemSelectionModel::SelectCurrent);
QModelIndex idx = vu.moveCursor(PublicListView::MovePageDown, Qt::NoModifier);
int newCurrent = qMin(currentItemIndex + scrolledRowCount, 99);
QCOMPARE(idx, model.index(newCurrent, 0));
idx = vu.moveCursor(PublicListView::MovePageUp, Qt::NoModifier);
newCurrent = qMax(currentItemIndex - scrolledRowCount, 0);
QCOMPARE(idx, model.index(newCurrent, 0));
}
}
void tst_QListView::taskQTBUG_9455_wrongScrollbarRanges()
{
QStringListModel model(generateList("item ", 8));
PublicListView w;
setFrameless(&w);
w.setModel(&model);
w.setViewMode(QListView::IconMode);
w.resize(116, 132);
w.setMovement(QListView::Static);
w.setSpacing(200);
w.showNormal();
QVERIFY(QTest::qWaitForWindowExposed(&w));
QCOMPARE(w.verticalScrollBar()->maximum(),
w.contentsSize().height() - w.viewport()->geometry().height());
}
void tst_QListView::styleOptionViewItem()
{
class MyDelegate : public QStyledItemDelegate
{
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
QStyleOptionViewItem opt(option);
initStyleOption(&opt, index);
QCOMPARE(opt.index, index);
QStyledItemDelegate::paint(painter, option, index);
}
};
QListView view;
QStandardItemModel model;
view.setModel(&model);
MyDelegate delegate;
view.setItemDelegate(&delegate);
model.appendRow({new QStandardItem("Beginning"),
new QStandardItem("Middle"),
new QStandardItem("Middle"),
new QStandardItem("End")});
// Run test
view.showMaximized();
QVERIFY(QTest::qWaitForWindowExposed(&view));
}
void tst_QListView::taskQTBUG_12308_artihmeticException()
{
QListWidget lw;
lw.setLayoutMode(QListView::Batched);
lw.setViewMode(QListView::IconMode);
for (int i = 0; i < lw.batchSize() + 1; i++) {
QListWidgetItem *item = new QListWidgetItem(
QLatin1String("Item ") + QString::number(i));
lw.addItem(item);
item->setHidden(true);
}
lw.show();
QVERIFY(QTest::qWaitForWindowExposed(&lw));
// No crash, it's all right.
}
class Delegate12308 : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override
{
QVERIFY(option.rect.topLeft() != QPoint(-1, -1));
QStyledItemDelegate::paint(painter, option, index);
}
};
void tst_QListView::taskQTBUG_12308_wrongFlowLayout()
{
QListWidget lw;
Delegate12308 delegate;
lw.setLayoutMode(QListView::Batched);
lw.setViewMode(QListView::IconMode);
lw.setItemDelegate(&delegate);
for (int i = 0; i < lw.batchSize() + 1; i++) {
QListWidgetItem *item = new QListWidgetItem(
QLatin1String("Item ") + QString::number(i));
lw.addItem(item);
if (!item->text().contains(QLatin1Char('1')))
item->setHidden(true);
}
lw.show();
QVERIFY(QTest::qWaitForWindowExposed(&lw));
}
void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems_data()
{
QTest::addColumn<QListView::Flow>("flow");
QTest::newRow("flow TopToBottom") << QListView::TopToBottom;
QTest::newRow("flow LeftToRight") << QListView::LeftToRight;
}
void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems()
{
QFETCH(QListView::Flow, flow);
#ifdef Q_OS_WINRT
QSKIP("Fails on WinRT - QTBUG-68297");
#endif
ScrollPerItemListView lv;
lv.setUniformItemSizes(true);
lv.setFlow(flow);
QStringListModel model;
model.setStringList(generateList(QString(), 30));
lv.setModel(&model);
lv.showNormal();
QVERIFY(QTest::qWaitForWindowExposed(&lv));
// Save first item rect for reference
QRect firstItemRect = lv.visualRect(model.index(0, 0));
// Select an item and scroll to selection
QModelIndex index = model.index(2, 0);
lv.setCurrentIndex(index);
lv.scrollTo(index, QAbstractItemView::PositionAtTop);
QTRY_COMPARE(lv.visualRect(index), firstItemRect);
// Hide some rows and scroll to selection
for (int i = 0; i < 5; i++) {
if (i == index.row())
continue;
lv.setRowHidden(i, true);
}
lv.scrollTo(index, QAbstractItemView::PositionAtTop);
QTRY_COMPARE(lv.visualRect(index), firstItemRect);
}
void tst_QListView::draggablePaintPairs_data()
{
QTest::addColumn<int>("row");
for (int row = 0; row < 30; ++row)
QTest::newRow("row-" + QByteArray::number(row)) << row;
}
void tst_QListView::draggablePaintPairs()
{
QFETCH(int, row);
QListView view;
QStringListModel model;
model.setStringList(generateList(QString(), 30));
view.setModel(&model);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QModelIndex expectedIndex = model.index(row, 0);
QListViewPrivate *privateClass = static_cast<QListViewPrivate *>(QListViewPrivate::get(&view));
QRect rect;
const QModelIndexList indexList{ expectedIndex };
view.scrollTo(expectedIndex);
const QItemViewPaintPairs pairs = privateClass->draggablePaintPairs(indexList, &rect);
QCOMPARE(indexList.size(), pairs.size());
for (const QItemViewPaintPair &pair : pairs) {
QCOMPARE(rect, pair.rect);
QCOMPARE(expectedIndex, pair.index);
}
}
void tst_QListView::taskQTBUG_21804_hiddenItemsAndScrollingWithKeys_data()
{
QTest::addColumn<QListView::Flow>("flow");
QTest::addColumn<int>("spacing");
QTest::newRow("flow TopToBottom no spacing") << QListView::TopToBottom << 0;
QTest::newRow("flow TopToBottom with spacing") << QListView::TopToBottom << 5;
QTest::newRow("flow LeftToRight no spacing") << QListView::LeftToRight << 0;
QTest::newRow("flow LeftToRight with spacing") << QListView::LeftToRight << 5;
}
void tst_QListView::taskQTBUG_21804_hiddenItemsAndScrollingWithKeys()
{
QFETCH(QListView::Flow, flow);
QFETCH(int, spacing);
// create some items to show
QStringListModel model;
model.setStringList(generateList(QString(), 60));
// create listview
ScrollPerItemListView lv;
lv.setFlow(flow);
lv.setSpacing(spacing);
lv.setModel(&model);
lv.show();
QVERIFY(QTest::qWaitForWindowExposed(&lv));
// hide every odd number row
for (int i = 1; i < model.rowCount(); i+=2)
lv.setRowHidden(i, true);
// scroll forward and check that selected item is visible always
int visibleItemCount = model.rowCount() / 2;
for (int i = 0; i < visibleItemCount; i++) {
if (flow == QListView::TopToBottom)
QTest::keyClick(&lv, Qt::Key_Down);
else
QTest::keyClick(&lv, Qt::Key_Right);
QTRY_VERIFY(lv.rect().contains(lv.visualRect(lv.currentIndex())));
}
// scroll backward
for (int i = 0; i < visibleItemCount; i++) {
if (flow == QListView::TopToBottom)
QTest::keyClick(&lv, Qt::Key_Up);
else
QTest::keyClick(&lv, Qt::Key_Left);
QTRY_VERIFY(lv.rect().contains(lv.visualRect(lv.currentIndex())));
}
// scroll forward only half way
for (int i = 0; i < visibleItemCount / 2; i++) {
if (flow == QListView::TopToBottom)
QTest::keyClick(&lv, Qt::Key_Down);
else
QTest::keyClick(&lv, Qt::Key_Right);
QTRY_VERIFY(lv.rect().contains(lv.visualRect(lv.currentIndex())));
}
// scroll backward again
for (int i = 0; i < visibleItemCount / 2; i++) {
if (flow == QListView::TopToBottom)
QTest::keyClick(&lv, Qt::Key_Up);
else
QTest::keyClick(&lv, Qt::Key_Left);
QTRY_VERIFY(lv.rect().contains(lv.visualRect(lv.currentIndex())));
}
}
void tst_QListView::spacing_data()
{
QTest::addColumn<QListView::Flow>("flow");
QTest::addColumn<int>("spacing");
QTest::newRow("flow=TopToBottom spacing=0") << QListView::TopToBottom << 0;
QTest::newRow("flow=TopToBottom spacing=10") << QListView::TopToBottom << 10;
QTest::newRow("flow=LeftToRight spacing=0") << QListView::LeftToRight << 0;
QTest::newRow("flow=LeftToRight spacing=10") << QListView::LeftToRight << 10;
}
void tst_QListView::spacing()
{
QFETCH(QListView::Flow, flow);
QFETCH(int, spacing);
// create some items to show
QStringListModel model;
model.setStringList(generateList(QString(), 60));
// create listview
ScrollPerItemListView lv;
lv.setFlow(flow);
lv.setModel(&model);
lv.setSpacing(spacing);
lv.show();
QVERIFY(QTest::qWaitForWindowExposed(&lv));
// check size and position of first two items
QRect item1 = lv.visualRect(lv.model()->index(0, 0));
QRect item2 = lv.visualRect(lv.model()->index(1, 0));
QCOMPARE(item1.topLeft(), QPoint(flow == QListView::TopToBottom ? spacing : 0, spacing));
if (flow == QListView::TopToBottom) {
QCOMPARE(item1.width(), lv.viewport()->width() - 2 * spacing);
QCOMPARE(item2.topLeft(), QPoint(spacing, spacing + item1.height() + 2 * spacing));
}
else { // QListView::LeftToRight
QCOMPARE(item1.height(), lv.viewport()->height() - 2 * spacing);
QCOMPARE(item2.topLeft(), QPoint(spacing + item1.width() + spacing, spacing));
}
}
void tst_QListView::testScrollToWithHidden()
{
QListView lv;
QStringListModel model;
model.setStringList(generateList(QString(), 30));
lv.setModel(&model);
lv.setRowHidden(1, true);
lv.setSpacing(5);
lv.showNormal();
QVERIFY(QTest::qWaitForWindowExposed(&lv));
QCOMPARE(lv.verticalScrollBar()->value(), 0);
lv.scrollTo(model.index(26, 0));
int expectedScrollBarValue = lv.verticalScrollBar()->value();
#ifdef Q_OS_WINRT
QSKIP("Might fail on WinRT - QTBUG-68297");
#endif
QVERIFY(expectedScrollBarValue != 0);
lv.scrollTo(model.index(25, 0));
QCOMPARE(expectedScrollBarValue, lv.verticalScrollBar()->value());
}
void tst_QListView::testViewOptions()
{
PublicListView view;
QStyleOptionViewItem options = view.viewOptions();
QCOMPARE(options.decorationPosition, QStyleOptionViewItem::Left);
view.setViewMode(QListView::IconMode);
options = view.viewOptions();
QCOMPARE(options.decorationPosition, QStyleOptionViewItem::Top);
}
// make sure we have no transient scroll bars
class TempStyleSetter
{
public:
TempStyleSetter()
: m_oldStyle(QApplication::style())
{
m_oldStyle->setParent(nullptr);
QListView tempView;
if (QApplication::style()->styleHint(QStyle::SH_ScrollBar_Transient,
nullptr, tempView.horizontalScrollBar()))
QApplication::setStyle(QStyleFactory::create("Fusion"));
}
~TempStyleSetter()
{
QApplication::setStyle(m_oldStyle);
}
private:
QStyle *m_oldStyle;
};
void tst_QListView::taskQTBUG_39902_mutualScrollBars_data()
{
QTest::addColumn<QAbstractItemView::ScrollMode>("horizontalScrollMode");
QTest::addColumn<QAbstractItemView::ScrollMode>("verticalScrollMode");
QTest::newRow("per item / per item") << QAbstractItemView::ScrollPerItem
<< QAbstractItemView::ScrollPerItem;
QTest::newRow("per pixel / per item") << QAbstractItemView::ScrollPerPixel
<< QAbstractItemView::ScrollPerItem;
QTest::newRow("per item / per pixel") << QAbstractItemView::ScrollPerItem
<< QAbstractItemView::ScrollPerPixel;
QTest::newRow("per pixel / per pixel") << QAbstractItemView::ScrollPerPixel
<< QAbstractItemView::ScrollPerPixel;
}
void tst_QListView::taskQTBUG_39902_mutualScrollBars()
{
QFETCH(QAbstractItemView::ScrollMode, horizontalScrollMode);
QFETCH(QAbstractItemView::ScrollMode, verticalScrollMode);
QWidget window;
window.resize(400, 300);
QListView *view = new QListView(&window);
// make sure we have no transient scroll bars
TempStyleSetter styleSetter;
QStandardItemModel model(200, 1);
const QSize itemSize(100, 20);
for (int i = 0; i < model.rowCount(); ++i)
model.setData(model.index(i, 0), itemSize, Qt::SizeHintRole);
view->setModel(&model);
view->setVerticalScrollMode(verticalScrollMode);
view->setHorizontalScrollMode(horizontalScrollMode);
window.show();
QVERIFY(QTest::qWaitForWindowExposed(&window));
// make sure QListView is done with layouting the items (1/10 sec, like QListView)
QTest::qWait(100);
model.setRowCount(2);
for (int i = 0; i < model.rowCount(); ++i)
model.setData(model.index(i, 0), itemSize, Qt::SizeHintRole);
view->resize(itemSize.width() + view->frameWidth() * 2,
model.rowCount() * itemSize.height() + view->frameWidth() * 2);
// this will end up in a stack overflow, if QTBUG-39902 is not fixed
QTest::qWait(100);
// these tests do not apply with transient scroll bars enabled
QVERIFY (!view->style()->styleHint(QStyle::SH_ScrollBar_Transient,
nullptr, view->horizontalScrollBar()));
// make it double as large, no scroll bars should be visible
view->resize((itemSize.width() + view->frameWidth() * 2) * 2,
(model.rowCount() * itemSize.height() + view->frameWidth() * 2) * 2);
QTRY_VERIFY(!view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(!view->verticalScrollBar()->isVisible());
// make it half the size, both scroll bars should be visible
view->resize((itemSize.width() + view->frameWidth() * 2) / 2,
(model.rowCount() * itemSize.height() + view->frameWidth() * 2) / 2);
QTRY_VERIFY(view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(view->verticalScrollBar()->isVisible());
// make it double as large, no scroll bars should be visible
view->resize((itemSize.width() + view->frameWidth() * 2) * 2,
(model.rowCount() * itemSize.height() + view->frameWidth() * 2) * 2);
QTRY_VERIFY(!view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(!view->verticalScrollBar()->isVisible());
// now, coming from the double size, resize it to the exactly matching size, still no scroll bars should be visible again
view->resize(itemSize.width() + view->frameWidth() * 2,
model.rowCount() * itemSize.height() + view->frameWidth() * 2);
QTRY_VERIFY(!view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(!view->verticalScrollBar()->isVisible());
// now remove just one single pixel in height -> both scroll bars will show up since they depend on each other
view->resize(itemSize.width() + view->frameWidth() * 2,
model.rowCount() * itemSize.height() + view->frameWidth() * 2 - 1);
QTRY_VERIFY(view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(view->verticalScrollBar()->isVisible());
// now remove just one single pixel in width -> both scroll bars will show up since they depend on each other
view->resize(itemSize.width() + view->frameWidth() * 2 - 1,
model.rowCount() * itemSize.height() + view->frameWidth() * 2);
QTRY_VERIFY(view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(view->verticalScrollBar()->isVisible());
// finally, coming from a size being to small, resize back to the exactly matching size -> both scroll bars should disappear again
view->resize(itemSize.width() + view->frameWidth() * 2,
model.rowCount() * itemSize.height() + view->frameWidth() * 2);
QTRY_VERIFY(!view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(!view->verticalScrollBar()->isVisible());
// now remove just one single pixel in height -> both scroll bars will show up since they depend on each other
view->resize(itemSize.width() + view->frameWidth() * 2,
model.rowCount() * itemSize.height() + view->frameWidth() * 2 - 1);
QTRY_VERIFY(view->horizontalScrollBar()->isVisible());
QTRY_VERIFY(view->verticalScrollBar()->isVisible());
}
void tst_QListView::horizontalScrollingByVerticalWheelEvents()
{
#if QT_CONFIG(wheelevent)
QListView lv;
lv.setWrapping(true);
lv.setItemDelegate(new TestDelegate(&lv, QSize(100, 100)));
QtTestModel model(100, 1);
lv.setModel(&model);
lv.resize(300, 300);
lv.show();
QVERIFY(QTest::qWaitForWindowExposed(&lv));
QPoint globalPos = lv.geometry().center();
QPoint pos = lv.viewport()->geometry().center();
QWheelEvent wheelDownEvent(pos, globalPos, QPoint(0, 0), QPoint(0, -120), Qt::NoButton, Qt::NoModifier, Qt::NoScrollPhase, false);
QWheelEvent wheelUpEvent(pos, globalPos, QPoint(0, 0), QPoint(0, 120), Qt::NoButton, Qt::NoModifier, Qt::NoScrollPhase, false);
QWheelEvent wheelLeftDownEvent(pos, globalPos, QPoint(0, 0), QPoint(120, -120), Qt::NoButton, Qt::NoModifier, Qt::NoScrollPhase, false);
int hValue = lv.horizontalScrollBar()->value();
QCoreApplication::sendEvent(lv.viewport(), &wheelDownEvent);
QVERIFY(lv.horizontalScrollBar()->value() > hValue);
QCoreApplication::sendEvent(lv.viewport(), &wheelUpEvent);
QCOMPARE(lv.horizontalScrollBar()->value(), hValue);
QCoreApplication::sendEvent(lv.viewport(), &wheelLeftDownEvent);
QCOMPARE(lv.horizontalScrollBar()->value(), hValue);
// ensure that vertical wheel events are not converted when vertical
// scroll bar is not visible but vertical scrolling is possible
lv.setWrapping(false);
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QCoreApplication::processEvents();
int vValue = lv.verticalScrollBar()->value();
QCoreApplication::sendEvent(lv.viewport(), &wheelDownEvent);
QVERIFY(lv.verticalScrollBar()->value() > vValue);
#else
QSKIP("Built with --no-feature-wheelevent");
#endif
}
void tst_QListView::taskQTBUG_7232_AllowUserToControlSingleStep()
{
// When we set the scrollMode to ScrollPerPixel it will adjust the scrollbars singleStep automatically
// Setting a singlestep on a scrollbar should however imply that the user takes control.
// Setting a singlestep to -1 return to an automatic control of the singleStep.
QListView lv;
lv.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
lv.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
QStandardItemModel model(1000, 100);
QString str = QString::fromLatin1("This is a long string made to ensure that we get some horizontal scroll (and we want scroll)");
model.setData(model.index(0, 0), str);
lv.setModel(&model);
lv.setGeometry(150, 150, 150, 150);
lv.show();
lv.setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
lv.setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
QVERIFY(QTest::qWaitForWindowExposed(&lv));
int vStep1 = lv.verticalScrollBar()->singleStep();
int hStep1 = lv.horizontalScrollBar()->singleStep();
QVERIFY(lv.verticalScrollBar()->singleStep() > 1);
QVERIFY(lv.horizontalScrollBar()->singleStep() > 1);
lv.verticalScrollBar()->setSingleStep(1);
lv.setGeometry(200, 200, 200, 200);
QCOMPARE(lv.verticalScrollBar()->singleStep(), 1);
lv.horizontalScrollBar()->setSingleStep(1);
lv.setGeometry(150, 150, 150, 150);
QCOMPARE(lv.horizontalScrollBar()->singleStep(), 1);
lv.verticalScrollBar()->setSingleStep(-1);
lv.horizontalScrollBar()->setSingleStep(-1);
QCOMPARE(vStep1, lv.verticalScrollBar()->singleStep());
QCOMPARE(hStep1, lv.horizontalScrollBar()->singleStep());
}
void tst_QListView::taskQTBUG_51086_skippingIndexesInSelectedIndexes()
{
QStandardItemModel data(10, 1);
QItemSelectionModel selections(&data);
PublicListView list;
list.setModel(&data);
list.setSelectionModel(&selections);
list.setRowHidden(7, true);
list.setRowHidden(8, true);
for (int i = 0, count = data.rowCount(); i < count; ++i)
selections.select(data.index(i, 0), QItemSelectionModel::Select);
const QModelIndexList indexes = list.selectedIndexes();
QVERIFY(!indexes.contains(data.index(7, 0)));
QVERIFY(!indexes.contains(data.index(8, 0)));
}
void tst_QListView::taskQTBUG_47694_indexOutOfBoundBatchLayout()
{
QListView view;
view.setLayoutMode(QListView::Batched);
int batchSize = view.batchSize();
QStandardItemModel model(batchSize + 1, 1);
view.setModel(&model);
view.scrollTo(model.index(batchSize - 1, 0));
}
void tst_QListView::itemAlignment()
{
auto item1 = new QStandardItem("111");
auto item2 = new QStandardItem("111111");
QStandardItemModel model;
model.appendRow(item1);
model.appendRow(item2);
QListView w;
w.setModel(&model);
w.setWrapping(true);
w.show();
QVERIFY(QTest::qWaitForWindowExposed(&w));
QVERIFY(w.visualRect(item1->index()).width() > 0);
QVERIFY(w.visualRect(item1->index()).width() == w.visualRect(item2->index()).width());
w.setItemAlignment(Qt::AlignLeft);
QCoreApplication::processEvents();
QVERIFY(w.visualRect(item1->index()).width() < w.visualRect(item2->index()).width());
}
void tst_QListView::internalDragDropMove_data()
{
QTest::addColumn<QListView::ViewMode>("viewMode");
QTest::addColumn<QAbstractItemView::DragDropMode>("dragDropMode");
QTest::addColumn<Qt::DropActions>("supportedDropActions");
QTest::addColumn<Qt::DropAction>("defaultDropAction");
QTest::addColumn<Qt::ItemFlags>("itemFlags");
QTest::addColumn<bool>("modelMoves");
QTest::addColumn<QStringList>("expectedData");
const Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable
| Qt::ItemIsEnabled
| Qt::ItemIsEditable
| Qt::ItemIsDragEnabled;
const QStringList unchanged = QStringList{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
const QStringList reordered = QStringList{"0", "2", "3", "4", "5", "6", "7", "8", "9", "1"};
const QStringList replaced = QStringList{"0", "2", "3", "4", "1", "6", "7", "8", "9"};
for (auto viewMode : { QListView::IconMode, QListView::ListMode }) {
for (auto modelMoves : { true, false } ) {
QByteArray rowName = viewMode == QListView::IconMode ? "icon" : "list" ;
rowName += modelMoves ? ", model moves" : ", model doesn't move";
QTest::newRow((rowName + ", copy&move").constData())
<< viewMode
<< QAbstractItemView::InternalMove
<< (Qt::CopyAction|Qt::MoveAction)
<< Qt::MoveAction
<< defaultFlags
<< modelMoves
// listview in IconMode doesn't change the model
<< ((viewMode == QListView::IconMode && !modelMoves) ? unchanged : reordered);
QTest::newRow((rowName + ", only move").constData())
<< viewMode
<< QAbstractItemView::InternalMove
<< (Qt::IgnoreAction|Qt::MoveAction)
<< Qt::MoveAction
<< defaultFlags
<< modelMoves
// listview in IconMode doesn't change the model
<< ((viewMode == QListView::IconMode && !modelMoves) ? unchanged : reordered);
QTest::newRow((rowName + ", replace item").constData())
<< viewMode
<< QAbstractItemView::InternalMove
<< (Qt::IgnoreAction|Qt::MoveAction)
<< Qt::MoveAction
<< (defaultFlags | Qt::ItemIsDropEnabled)
<< modelMoves
<< replaced;
}
}
}
/*
Test moving of items items via drag'n'drop.
This should reorder items when an item is dropped in between two items,
or - if items can be dropped on - replace the content of the drop target.
Test QListView in both icon and list view modes.
See QTBUG-67440, QTBUG-83084, QTBUG-87057
*/
void tst_QListView::internalDragDropMove()
{
const QString platform(QGuiApplication::platformName().toLower());
if (platform != QLatin1String("xcb"))
QSKIP("Need a window system with proper DnD support via injected mouse events");
QFETCH(QListView::ViewMode, viewMode);
QFETCH(QAbstractItemView::DragDropMode, dragDropMode);
QFETCH(Qt::DropActions, supportedDropActions);
QFETCH(Qt::DropAction, defaultDropAction);
QFETCH(Qt::ItemFlags, itemFlags);
QFETCH(bool, modelMoves);
QFETCH(QStringList, expectedData);
class ItemModel : public QStringListModel
{
public:
ItemModel()
{
QStringList list;
for (int i = 0; i < 10; ++i) {
list << QString::number(i);
}
setStringList(list);
}
Qt::DropActions supportedDropActions() const override { return m_supportedDropActions; }
Qt::ItemFlags flags(const QModelIndex &index) const override
{
if (!index.isValid())
return QStringListModel::flags(index);
return m_itemFlags;
}
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
const QModelIndex &destinationParent, int destinationChild) override
{
if (!m_modelMoves) // many models don't implement moveRows
return false;
return QStringListModel::moveRows(sourceParent, sourceRow, count,
destinationParent, destinationChild);
}
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &values) override
{
return QStringListModel::setData(index, values.value(Qt::DisplayRole), Qt::DisplayRole);
}
QVariant data(const QModelIndex &index, int role) const override
{
if (role == Qt::DecorationRole)
return QColor(Qt::GlobalColor(index.row() + 1));
return QStringListModel::data(index, role);
}
QMap<int, QVariant> itemData(const QModelIndex &index) const override
{
auto item = QStringListModel::itemData(index);
item[Qt::DecorationRole] = data(index, Qt::DecorationRole);
return item;
}
Qt::DropActions m_supportedDropActions;
Qt::ItemFlags m_itemFlags;
bool m_modelMoves;
};
ItemModel data;
data.m_supportedDropActions = supportedDropActions;
data.m_itemFlags = itemFlags;
data.m_modelMoves = modelMoves;
QItemSelectionModel selections(&data);
PublicListView list;
list.setWindowTitle(QTest::currentTestFunction());
list.setViewMode(viewMode);
list.setDragDropMode(dragDropMode);
list.setDefaultDropAction(defaultDropAction);
list.setModel(&data);
list.setSelectionModel(&selections);
int itemHeight = list.sizeHintForIndex(data.index(1, 0)).height();
list.resize(300, 15 * itemHeight);
list.show();
selections.select(data.index(1, 0), QItemSelectionModel::Select);
auto getSelectedTexts = [&]() -> QStringList {
QStringList selectedTexts;
for (auto index : selections.selectedIndexes())
selectedTexts << data.itemData(index).value(Qt::DisplayRole).toString();
return selectedTexts;
};
// The test relies on the global position of mouse events; make sure
// the window is properly mapped on X11.
QVERIFY(QTest::qWaitForWindowActive(&list));
// execute as soon as the eventloop is running again
// which is the case inside list.startDrag()
QTimer::singleShot(0, [&]()
{
QPoint droppos;
// take into account subtle differences between icon and list mode in QListView's drop placement
if (itemFlags & Qt::ItemIsDropEnabled)
droppos = list.rectForIndex(data.index(5, 0)).center();
else if (viewMode == QListView::IconMode)
droppos = list.rectForIndex(data.index(9, 0)).bottomRight() + QPoint(30, 30);
else
droppos = list.rectForIndex(data.index(9, 0)).bottomRight();
QMouseEvent mouseMove(QEvent::MouseMove, droppos, list.mapToGlobal(droppos), Qt::NoButton, {}, {});
QCoreApplication::sendEvent(&list, &mouseMove);
QMouseEvent mouseRelease(QEvent::MouseButtonRelease, droppos, list.mapToGlobal(droppos), Qt::LeftButton, {}, {});
QCoreApplication::sendEvent(&list, &mouseRelease);
});
const QStringList expectedSelected = getSelectedTexts();
list.startDrag(Qt::MoveAction);
QTRY_COMPARE(data.stringList(), expectedData);
// if the model doesn't implement moveRows, or if items are replaced, then selection is lost
if (modelMoves && !(itemFlags & Qt::ItemIsDropEnabled)) {
const QStringList actualSelected = getSelectedTexts();
QTRY_COMPARE(actualSelected, expectedSelected);
}
}
void tst_QListView::scrollOnRemove_data()
{
QTest::addColumn<QListView::ViewMode>("viewMode");
QTest::addColumn<QAbstractItemView::SelectionMode>("selectionMode");
const QMetaObject &mo = QListView::staticMetaObject;
const auto viewModeEnum = mo.enumerator(mo.indexOfEnumerator("ViewMode"));
const auto selectionModeEnum = mo.enumerator(mo.indexOfEnumerator("SelectionMode"));
for (auto viewMode : { QListView::ListMode, QListView::IconMode }) {
const char *viewModeName = viewModeEnum.valueToKey(viewMode);
for (int index = 0; index < selectionModeEnum.keyCount(); ++index) {
const auto selectionMode = QAbstractItemView::SelectionMode(selectionModeEnum.value(index));
const char *selectionModeName = selectionModeEnum.valueToKey(selectionMode);
QTest::addRow("%s, %s", viewModeName, selectionModeName) << viewMode << selectionMode;
}
}
}
void tst_QListView::scrollOnRemove()
{
QFETCH(QListView::ViewMode, viewMode);
QFETCH(QAbstractItemView::SelectionMode, selectionMode);
QPixmap pixmap;
if (viewMode == QListView::IconMode) {
pixmap = QPixmap(25, 25);
pixmap.fill(Qt::red);
}
QStandardItemModel model;
for (int i = 0; i < 50; ++i) {
QStandardItem *item = new QStandardItem(QString::number(i));
item->setIcon(pixmap);
model.appendRow(item);
}
QWidget widget;
QListView view(&widget);
view.setFixedSize(100, 100);
view.setAutoScroll(true);
if (viewMode == QListView::IconMode)
view.setWrapping(true);
view.setModel(&model);
view.setSelectionMode(selectionMode);
view.setViewMode(viewMode);
widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget));
QCOMPARE(view.verticalScrollBar()->value(), 0);
const QModelIndex item25 = model.index(25, 0);
view.scrollTo(item25);
QTRY_VERIFY(view.verticalScrollBar()->value() > 0); // layout and scrolling are delayed
const int item25Position = view.verticalScrollBar()->value();
// selecting a fully visible item shouldn't scroll
view.selectionModel()->setCurrentIndex(item25, QItemSelectionModel::SelectCurrent);
QTRY_COMPARE(view.verticalScrollBar()->value(), item25Position);
// removing the selected item might scroll if another item is selected
model.removeRow(25);
// if nothing is selected now, then the view should not have scrolled
if (!view.selectionModel()->selectedIndexes().count())
QTRY_COMPARE(view.verticalScrollBar()->value(), item25Position);
}
QTEST_MAIN(tst_QListView)
#include "tst_qlistview.moc"
|