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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qrect.h"
#include "qdatastream.h"
#include "qdebug.h"
#include "qmath.h"
#include <math.h>
QT_BEGIN_NAMESPACE
/*!
\class QRect
\ingroup multimedia
\brief The QRect class defines a rectangle in the plane using
integer precision.
A rectangle is normally expressed as an upper-left corner and a
size. The size (width and height) of a QRect is always equivalent
to the mathematical rectangle that forms the basis for its
rendering.
A QRect can be constructed with a set of left, top, width and
height integers, or from a QPoint and a QSize. The following code
creates two identical rectangles.
\snippet doc/src/snippets/code/src_corelib_tools_qrect.cpp 0
There is a third constructor that creates a QRect using the
top-left and bottom-right coordinates, but we recommend that you
avoid using it. The rationale is that for historical reasons the
values returned by the bottom() and right() functions deviate from
the true bottom-right corner of the rectangle.
The QRect class provides a collection of functions that return the
various rectangle coordinates, and enable manipulation of
these. QRect also provide functions to move the rectangle relative
to the various coordinates. In addition there is a moveTo()
function that moves the rectangle, leaving its top left corner at
the given coordinates. Alternatively, the translate() function
moves the rectangle the given offset relative to the current
position, and the translated() function returns a translated copy
of this rectangle.
The size() function returns the rectange's dimensions as a
QSize. The dimensions can also be retrieved separately using the
width() and height() functions. To manipulate the dimensions use
the setSize(), setWidth() or setHeight() functions. Alternatively,
the size can be changed by applying either of the functions
setting the rectangle coordinates, for example, setBottom() or
setRight().
The contains() function tells whether a given point is inside the
rectangle or not, and the intersects() function returns true if
this rectangle intersects with a given rectangle. The QRect class
also provides the intersected() function which returns the
intersection rectangle, and the united() function which returns the
rectangle that encloses the given rectangle and this:
\table
\row
\o \inlineimage qrect-intersect.png
\o \inlineimage qrect-unite.png
\row
\o intersected()
\o united()
\endtable
The isEmpty() function returns true if left() > right() or top() >
bottom(). Note that an empty rectangle is not valid: The isValid()
function returns true if left() <= right() \e and top() <=
bottom(). A null rectangle (isNull() == true) on the other hand,
has both width and height set to 0.
Note that due to the way QRect and QRectF are defined, an
empty QRect is defined in essentially the same way as QRectF.
Finally, QRect objects can be streamed as well as compared.
\tableofcontents
\section1 Rendering
When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
the boundary line of a QRect will be rendered symmetrically on
both sides of the mathematical rectangle's boundary line. But when
using an aliased painter (the default) other rules apply.
Then, when rendering with a one pixel wide pen the QRect's boundary
line will be rendered to the right and below the mathematical
rectangle's boundary line.
When rendering with a two pixels wide pen the boundary line will
be split in the middle by the mathematical rectangle. This will be
the case whenever the pen is set to an even number of pixels,
while rendering with a pen with an odd number of pixels, the spare
pixel will be rendered to the right and below the mathematical
rectangle as in the one pixel case.
\table
\row
\o \inlineimage qrect-diagram-zero.png
\o \inlineimage qrect-diagram-one.png
\row
\o Logical representation
\o One pixel wide pen
\row
\o \inlineimage qrect-diagram-two.png
\o \inlineimage qrect-diagram-three.png
\row
\o Two pixel wide pen
\o Three pixel wide pen
\endtable
\section1 Coordinates
The QRect class provides a collection of functions that return the
various rectangle coordinates, and enable manipulation of
these. QRect also provide functions to move the rectangle relative
to the various coordinates.
For example the left(), setLeft() and moveLeft() functions as an
example: left() returns the x-coordinate of the rectangle's left
edge, setLeft() sets the left edge of the rectangle to the given x
coordinate (it may change the width, but will never change the
rectangle's right edge) and moveLeft() moves the entire rectangle
horizontally, leaving the rectangle's left edge at the given x
coordinate and its size unchanged.
\image qrect-coordinates.png
Note that for historical reasons the values returned by the
bottom() and right() functions deviate from the true bottom-right
corner of the rectangle: The right() function returns \e { left()
+ width() - 1} and the bottom() function returns \e {top() +
height() - 1}. The same is the case for the point returned by the
bottomRight() convenience function. In addition, the x and y
coordinate of the topRight() and bottomLeft() functions,
respectively, contain the same deviation from the true right and
bottom edges.
We recommend that you use x() + width() and y() + height() to find
the true bottom-right corner, and avoid right() and
bottom(). Another solution is to use QRectF: The QRectF class
defines a rectangle in the plane using floating point accuracy for
coordinates, and the QRectF::right() and QRectF::bottom()
functions \e do return the right and bottom coordinates.
It is also possible to add offsets to this rectangle's coordinates
using the adjust() function, as well as retrieve a new rectangle
based on adjustments of the original one using the adjusted()
function. If either of the width and height is negative, use the
normalized() function to retrieve a rectangle where the corners
are swapped.
In addition, QRect provides the getCoords() function which extracts
the position of the rectangle's top-left and bottom-right corner,
and the getRect() function which extracts the rectangle's top-left
corner, width and height. Use the setCoords() and setRect()
function to manipulate the rectangle's coordinates and dimensions
in one go.
\sa QRectF, QRegion
*/
/*****************************************************************************
QRect member functions
*****************************************************************************/
/*!
\fn QRect::QRect()
Constructs a null rectangle.
\sa isNull()
*/
/*!
\fn QRect::QRect(const QPoint &topLeft, const QPoint &bottomRight)
Constructs a rectangle with the given \a topLeft and \a bottomRight corners.
\sa setTopLeft(), setBottomRight()
*/
/*!
\fn QRect::QRect(const QPoint &topLeft, const QSize &size)
Constructs a rectangle with the given \a topLeft corner and the
given \a size.
\sa setTopLeft(), setSize()
*/
/*!
\fn QRect::QRect(int x, int y, int width, int height)
Constructs a rectangle with (\a x, \a y) as its top-left corner
and the given \a width and \a height.
\sa setRect()
*/
/*!
\fn bool QRect::isNull() const
Returns true if the rectangle is a null rectangle, otherwise
returns false.
A null rectangle has both the width and the height set to 0 (i.e.,
right() == left() - 1 and bottom() == top() - 1). A null rectangle
is also empty, and hence is not valid.
\sa isEmpty(), isValid()
*/
/*!
\fn bool QRect::isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
An empty rectangle has a left() > right() or top() > bottom(). An
empty rectangle is not valid (i.e., isEmpty() == !isValid()).
Use the normalized() function to retrieve a rectangle where the
corners are swapped.
\sa isNull(), isValid(), normalized()
*/
/*!
\fn bool QRect::isValid() const
Returns true if the rectangle is valid, otherwise returns false.
A valid rectangle has a left() < right() and top() <
bottom(). Note that non-trivial operations like intersections are
not defined for invalid rectangles. A valid rectangle is not empty
(i.e., isValid() == !isEmpty()).
\sa isNull(), isEmpty(), normalized()
*/
/*!
Returns a normalized rectangle; i.e., a rectangle that has a
non-negative width and height.
If width() < 0 the function swaps the left and right corners, and
it swaps the top and bottom corners if height() < 0.
\sa isValid(), isEmpty()
*/
QRect QRect::normalized() const
{
QRect r;
if (x2 < x1 - 1) { // swap bad x values
r.x1 = x2;
r.x2 = x1;
} else {
r.x1 = x1;
r.x2 = x2;
}
if (y2 < y1 - 1) { // swap bad y values
r.y1 = y2;
r.y2 = y1;
} else {
r.y1 = y1;
r.y2 = y2;
}
return r;
}
/*!
\fn QRect QRect::normalize() const
\compat
Returns a normalized rectangle; i.e., a rectangle that has a
non-negative width and height.
Use the normalized() function instead
*/
/*!
\fn int QRect::left() const
Returns the x-coordinate of the rectangle's left edge. Equivalent
to x().
\sa setLeft(), topLeft(), bottomLeft()
*/
/*!
\fn int QRect::top() const
Returns the y-coordinate of the rectangle's top edge.
Equivalent to y().
\sa setTop(), topLeft(), topRight()
*/
/*!
\fn int QRect::right() const
Returns the x-coordinate of the rectangle's right edge.
Note that for historical reasons this function returns left() +
width() - 1; use x() + width() to retrieve the true x-coordinate.
\sa setRight(), topRight(), bottomRight()
*/
/*!
\fn int QRect::bottom() const
Returns the y-coordinate of the rectangle's bottom edge.
Note that for historical reasons this function returns top() +
height() - 1; use y() + height() to retrieve the true y-coordinate.
\sa setBottom(), bottomLeft(), bottomRight()
*/
/*!
\fn int &QRect::rLeft()
\compat
Returns a reference to the left coordinate of the rectangle.
Use the left() function instead.
*/
/*!
\fn int &QRect::rTop()
\compat
Returns a reference to the top coordinate of the rectangle.
Use the top() function instead.
*/
/*!
\fn int &QRect::rRight()
\compat
Returns a reference to the right coordinate of the rectangle.
Use the right() function instead.
*/
/*!
\fn int &QRect::rBottom()
\compat
Returns a reference to the bottom coordinate of the rectangle.
Use the bottom() function instead.
*/
/*!
\fn int QRect::x() const
Returns the x-coordinate of the rectangle's left edge. Equivalent to left().
\sa setX(), y(), topLeft()
*/
/*!
\fn int QRect::y() const
Returns the y-coordinate of the rectangle's top edge. Equivalent to top().
\sa setY(), x(), topLeft()
*/
/*!
\fn void QRect::setLeft(int x)
Sets the left edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the right
edge of the rectangle.
Equivalent to setX().
\sa left(), moveLeft()
*/
/*!
\fn void QRect::setTop(int y)
Sets the top edge of the rectangle to the given \a y
coordinate. May change the height, but will never change the
bottom edge of the rectangle.
Equivalent to setY().
\sa top(), moveTop()
*/
/*!
\fn void QRect::setRight(int x)
Sets the right edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the left
edge of the rectangle.
\sa right(), moveRight()
*/
/*!
\fn void QRect::setBottom(int y)
Sets the bottom edge of the rectangle to the given \a y
coordinate. May change the height, but will never change the top
edge of the rectangle.
\sa bottom(), moveBottom(),
*/
/*!
\fn void QRect::setX(int x)
Sets the left edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the right
edge of the rectangle.
Equivalent to setLeft().
\sa x(), setY(), setTopLeft()
*/
/*!
\fn void QRect::setY(int y)
Sets the top edge of the rectangle to the given \a y
coordinate. May change the height, but will never change the
bottom edge of the rectangle.
Equivalent to setTop().
\sa y(), setX(), setTopLeft()
*/
/*!
\fn void QRect::setTopLeft(const QPoint &position)
Set the top-left corner of the rectangle to the given \a
position. May change the size, but will never change the
bottom-right corner of the rectangle.
\sa topLeft(), moveTopLeft()
*/
/*!
\fn void QRect::setBottomRight(const QPoint &position)
Set the bottom-right corner of the rectangle to the given \a
position. May change the size, but will never change the
top-left corner of the rectangle.
\sa bottomRight(), moveBottomRight()
*/
/*!
\fn void QRect::setTopRight(const QPoint &position)
Set the top-right corner of the rectangle to the given \a
position. May change the size, but will never change the
bottom-left corner of the rectangle.
\sa topRight(), moveTopRight()
*/
/*!
\fn void QRect::setBottomLeft(const QPoint &position)
Set the bottom-left corner of the rectangle to the given \a
position. May change the size, but will never change the
top-right corner of the rectangle.
\sa bottomLeft(), moveBottomLeft()
*/
/*!
\fn QPoint QRect::topLeft() const
Returns the position of the rectangle's top-left corner.
\sa setTopLeft(), top(), left()
*/
/*!
\fn QPoint QRect::bottomRight() const
Returns the position of the rectangle's bottom-right corner.
Note that for historical reasons this function returns
QPoint(left() + width() -1, top() + height() - 1).
\sa setBottomRight(), bottom(), right()
*/
/*!
\fn QPoint QRect::topRight() const
Returns the position of the rectangle's top-right corner.
Note that for historical reasons this function returns
QPoint(left() + width() -1, top()).
\sa setTopRight(), top(), right()
*/
/*!
\fn QPoint QRect::bottomLeft() const
Returns the position of the rectangle's bottom-left corner. Note
that for historical reasons this function returns QPoint(left(),
top() + height() - 1).
\sa setBottomLeft(), bottom(), left()
*/
/*!
\fn QPoint QRect::center() const
Returns the center point of the rectangle.
\sa moveCenter()
*/
/*!
\fn void QRect::getRect(int *x, int *y, int *width, int *height) const
Extracts the position of the rectangle's top-left corner to *\a x
and *\a y, and its dimensions to *\a width and *\a height.
\sa setRect(), getCoords()
*/
/*!
\fn void QRect::getCoords(int *x1, int *y1, int *x2, int *y2) const
Extracts the position of the rectangle's top-left corner to *\a x1
and *\a y1, and the position of the bottom-right corner to *\a x2
and *\a y2.
\sa setCoords(), getRect()
*/
/*!
\fn void QRect::rect(int *x, int *y, int *width, int *height) const
\compat
Extracts the position of the rectangle's top-left corner to *\a x and
*\a y, and its dimensions to *\a width and * \a height.
Use the getRect() function instead.
*/
/*!
\fn void QRect::coords(int *x1, int *y1, int *x2, int *y2) const
\compat
Extracts the position of the rectangle's top-left corner to *\a x1
and *\a y1, and the position of the bottom-right corner to *\a x2
and *\a y2.
Use the getCoords() function instead.
*/
/*!
\fn void QRect::moveLeft(int x)
Moves the rectangle horizontally, leaving the rectangle's left
edge at the given \a x coordinate. The rectangle's size is
unchanged.
\sa left(), setLeft(), moveRight()
*/
/*!
\fn void QRect::moveTop(int y)
Moves the rectangle vertically, leaving the rectangle's top edge
at the given \a y coordinate. The rectangle's size is unchanged.
\sa top(), setTop(), moveBottom()
*/
/*!
\fn void QRect::moveRight(int x)
Moves the rectangle horizontally, leaving the rectangle's right
edge at the given \a x coordinate. The rectangle's size is
unchanged.
\sa right(), setRight(), moveLeft()
*/
/*!
\fn void QRect::moveBottom(int y)
Moves the rectangle vertically, leaving the rectangle's bottom
edge at the given \a y coordinate. The rectangle's size is
unchanged.
\sa bottom(), setBottom(), moveTop()
*/
/*!
\fn void QRect::moveTopLeft(const QPoint &position)
Moves the rectangle, leaving the top-left corner at the given \a
position. The rectangle's size is unchanged.
\sa setTopLeft(), moveTop(), moveLeft()
*/
/*!
\fn void QRect::moveBottomRight(const QPoint &position)
Moves the rectangle, leaving the bottom-right corner at the given
\a position. The rectangle's size is unchanged.
\sa setBottomRight(), moveRight(), moveBottom()
*/
/*!
\fn void QRect::moveTopRight(const QPoint &position)
Moves the rectangle, leaving the top-right corner at the given \a
position. The rectangle's size is unchanged.
\sa setTopRight(), moveTop(), moveRight()
*/
/*!
\fn void QRect::moveBottomLeft(const QPoint &position)
Moves the rectangle, leaving the bottom-left corner at the given
\a position. The rectangle's size is unchanged.
\sa setBottomLeft(), moveBottom(), moveLeft()
*/
/*!
\fn void QRect::moveCenter(const QPoint &position)
Moves the rectangle, leaving the center point at the given \a
position. The rectangle's size is unchanged.
\sa center()
*/
void QRect::moveCenter(const QPoint &p)
{
int w = x2 - x1;
int h = y2 - y1;
x1 = p.x() - w/2;
y1 = p.y() - h/2;
x2 = x1 + w;
y2 = y1 + h;
}
/*!
\fn void QRect::moveBy(int dx, int dy)
\compat
Moves the rectangle \a dx along the x axis and \a dy along the y
axis, relative to the current position.
Use the translate() function instead.
*/
/*!
\fn void QRect::moveBy(const QPoint &)
\compat
Use the translate() function instead.
*/
/*!
\fn void QRect::moveTo(int x, int y)
Moves the rectangle, leaving the top-left corner at the given
position (\a x, \a y). The rectangle's size is unchanged.
\sa translate(), moveTopLeft()
*/
/*!
\fn void QRect::moveTo(const QPoint &position)
Moves the rectangle, leaving the top-left corner at the given \a
position.
*/
/*!
\fn void QRect::translate(int dx, int dy)
Moves the rectangle \a dx along the x axis and \a dy along the y
axis, relative to the current position. Positive values move the
rectangle to the right and down.
\sa moveTopLeft(), moveTo(), translated()
*/
/*!
\fn void QRect::translate(const QPoint &offset)
\overload
Moves the rectangle \a{offset}.\l{QPoint::x()}{x()} along the x
axis and \a{offset}.\l{QPoint::y()}{y()} along the y axis,
relative to the current position.
*/
/*!
\fn QRect QRect::translated(int dx, int dy) const
Returns a copy of the rectangle that is translated \a dx along the
x axis and \a dy along the y axis, relative to the current
position. Positive values move the rectangle to the right and
down.
\sa translate()
*/
/*!
\fn QRect QRect::translated(const QPoint &offset) const
\overload
Returns a copy of the rectangle that is translated
\a{offset}.\l{QPoint::x()}{x()} along the x axis and
\a{offset}.\l{QPoint::y()}{y()} along the y axis, relative to the
current position.
*/
/*!
\fn void QRect::setRect(int x, int y, int width, int height)
Sets the coordinates of the rectangle's top-left corner to (\a{x},
\a{y}), and its size to the given \a width and \a height.
\sa getRect(), setCoords()
*/
/*!
\fn void QRect::setCoords(int x1, int y1, int x2, int y2)
Sets the coordinates of the rectangle's top-left corner to (\a x1,
\a y1), and the coordinates of its bottom-right corner to (\a x2,
\a y2).
\sa getCoords(), setRect()
*/
/*!
\fn void QRect::addCoords(int dx1, int dy1, int dx2, int dy2)
\compat
Adds \a dx1, \a dy1, \a dx2 and \a dy2 to the existing coordinates
of the rectangle respectively.
Use the adjust() function instead.
*/
/*! \fn QRect QRect::adjusted(int dx1, int dy1, int dx2, int dy2) const
Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
added respectively to the existing coordinates of this rectangle.
\sa adjust()
*/
/*! \fn void QRect::adjust(int dx1, int dy1, int dx2, int dy2)
Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
existing coordinates of the rectangle.
\sa adjusted(), setRect()
*/
/*!
\fn QSize QRect::size() const
Returns the size of the rectangle.
\sa setSize(), width(), height()
*/
/*!
\fn int QRect::width() const
Returns the width of the rectangle.
\sa setWidth(), height(), size()
*/
/*!
\fn int QRect::height() const
Returns the height of the rectangle.
\sa setHeight(), width(), size()
*/
/*!
\fn void QRect::setWidth(int width)
Sets the width of the rectangle to the given \a width. The right
edge is changed, but not the left one.
\sa width(), setSize()
*/
/*!
\fn void QRect::setHeight(int height)
Sets the height of the rectangle to the given \a height. The bottom
edge is changed, but not the top one.
\sa height(), setSize()
*/
/*!
\fn void QRect::setSize(const QSize &size)
Sets the size of the rectangle to the given \a size. The top-left
corner is not moved.
\sa size(), setWidth(), setHeight()
*/
/*!
\fn bool QRect::contains(const QPoint &point, bool proper) const
Returns true if the the given \a point is inside or on the edge of
the rectangle, otherwise returns false. If \a proper is true, this
function only returns true if the given \a point is \e inside the
rectangle (i.e., not on the edge).
\sa intersects()
*/
bool QRect::contains(const QPoint &p, bool proper) const
{
int l, r;
if (x2 < x1 - 1) {
l = x2;
r = x1;
} else {
l = x1;
r = x2;
}
if (proper) {
if (p.x() <= l || p.x() >= r)
return false;
} else {
if (p.x() < l || p.x() > r)
return false;
}
int t, b;
if (y2 < y1 - 1) {
t = y2;
b = y1;
} else {
t = y1;
b = y2;
}
if (proper) {
if (p.y() <= t || p.y() >= b)
return false;
} else {
if (p.y() < t || p.y() > b)
return false;
}
return true;
}
/*!
\fn bool QRect::contains(int x, int y, bool proper) const
\overload
Returns true if the point (\a x, \a y) is inside or on the edge of
the rectangle, otherwise returns false. If \a proper is true, this
function only returns true if the point is entirely inside the
rectangle(not on the edge).
*/
/*!
\fn bool QRect::contains(int x, int y) const
\overload
Returns true if the point (\a x, \a y) is inside this rectangle,
otherwise returns false.
*/
/*!
\fn bool QRect::contains(const QRect &rectangle, bool proper) const
\overload
Returns true if the given \a rectangle is inside this rectangle.
otherwise returns false. If \a proper is true, this function only
returns true if the \a rectangle is entirely inside this
rectangle (not on the edge).
*/
bool QRect::contains(const QRect &r, bool proper) const
{
if (isNull() || r.isNull())
return false;
int l1 = x1;
int r1 = x1;
if (x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if (r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if (proper) {
if (l2 <= l1 || r2 >= r1)
return false;
} else {
if (l2 < l1 || r2 > r1)
return false;
}
int t1 = y1;
int b1 = y1;
if (y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if (proper) {
if (t2 <= t1 || b2 >= b1)
return false;
} else {
if (t2 < t1 || b2 > b1)
return false;
}
return true;
}
/*!
\fn QRect& QRect::operator|=(const QRect &rectangle)
Unites this rectangle with the given \a rectangle.
\sa united(), operator|()
*/
/*!
\fn QRect& QRect::operator&=(const QRect &rectangle)
Intersects this rectangle with the given \a rectangle.
\sa intersected(), operator&()
*/
/*!
\fn QRect QRect::operator|(const QRect &rectangle) const
Returns the bounding rectangle of this rectangle and the given \a
rectangle.
\sa operator|=(), united()
*/
QRect QRect::operator|(const QRect &r) const
{
if (isNull())
return r;
if (r.isNull())
return *this;
int l1 = x1;
int r1 = x1;
if (x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if (r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
int t1 = y1;
int b1 = y1;
if (y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
QRect tmp;
tmp.x1 = qMin(l1, l2);
tmp.x2 = qMax(r1, r2);
tmp.y1 = qMin(t1, t2);
tmp.y2 = qMax(b1, b2);
return tmp;
}
/*!
\fn QRect QRect::unite(const QRect &rectangle) const
\obsolete
Use united(\a rectangle) instead.
*/
/*!
\fn QRect QRect::united(const QRect &rectangle) const
\since 4.2
Returns the bounding rectangle of this rectangle and the given \a rectangle.
\image qrect-unite.png
\sa intersected()
*/
/*!
\fn QRect QRect::operator&(const QRect &rectangle) const
Returns the intersection of this rectangle and the given \a
rectangle. Returns an empty rectangle if there is no intersection.
\sa operator&=(), intersected()
*/
QRect QRect::operator&(const QRect &r) const
{
if (isNull() || r.isNull())
return QRect();
int l1 = x1;
int r1 = x1;
if (x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if (r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if (l1 > r2 || l2 > r1)
return QRect();
int t1 = y1;
int b1 = y1;
if (y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if (t1 > b2 || t2 > b1)
return QRect();
QRect tmp;
tmp.x1 = qMax(l1, l2);
tmp.x2 = qMin(r1, r2);
tmp.y1 = qMax(t1, t2);
tmp.y2 = qMin(b1, b2);
return tmp;
}
/*!
\fn QRect QRect::intersect(const QRect &rectangle) const
\obsolete
Use intersected(\a rectangle) instead.
*/
/*!
\fn QRect QRect::intersected(const QRect &rectangle) const
\since 4.2
Returns the intersection of this rectangle and the given \a
rectangle. Note that \c{r.intersected(s)} is equivalent to \c{r & s}.
\image qrect-intersect.png
\sa intersects(), united(), operator&=()
*/
/*!
\fn bool QRect::intersects(const QRect &rectangle) const
Returns true if this rectangle intersects with the given \a
rectangle (i.e., there is at least one pixel that is within both
rectangles), otherwise returns false.
The intersection rectangle can be retrieved using the intersected()
function.
\sa contains()
*/
bool QRect::intersects(const QRect &r) const
{
if (isNull() || r.isNull())
return false;
int l1 = x1;
int r1 = x1;
if (x2 - x1 + 1 < 0)
l1 = x2;
else
r1 = x2;
int l2 = r.x1;
int r2 = r.x1;
if (r.x2 - r.x1 + 1 < 0)
l2 = r.x2;
else
r2 = r.x2;
if (l1 > r2 || l2 > r1)
return false;
int t1 = y1;
int b1 = y1;
if (y2 - y1 + 1 < 0)
t1 = y2;
else
b1 = y2;
int t2 = r.y1;
int b2 = r.y1;
if (r.y2 - r.y1 + 1 < 0)
t2 = r.y2;
else
b2 = r.y2;
if (t1 > b2 || t2 > b1)
return false;
return true;
}
/*!
\fn bool operator==(const QRect &r1, const QRect &r2)
\relates QRect
Returns true if the rectangles \a r1 and \a r2 are equal,
otherwise returns false.
*/
/*!
\fn bool operator!=(const QRect &r1, const QRect &r2)
\relates QRect
Returns true if the rectangles \a r1 and \a r2 are different, otherwise
returns false.
*/
/*****************************************************************************
QRect stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\fn QDataStream &operator<<(QDataStream &stream, const QRect &rectangle)
\relates QRect
Writes the given \a rectangle to the given \a stream, and returns
a reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator<<(QDataStream &s, const QRect &r)
{
if (s.version() == 1)
s << (qint16)r.left() << (qint16)r.top()
<< (qint16)r.right() << (qint16)r.bottom();
else
s << (qint32)r.left() << (qint32)r.top()
<< (qint32)r.right() << (qint32)r.bottom();
return s;
}
/*!
\fn QDataStream &operator>>(QDataStream &stream, QRect &rectangle)
\relates QRect
Reads a rectangle from the given \a stream into the given \a
rectangle, and returns a reference to the stream.
\sa {Format of the QDataStream Operators}
*/
QDataStream &operator>>(QDataStream &s, QRect &r)
{
if (s.version() == 1) {
qint16 x1, y1, x2, y2;
s >> x1; s >> y1; s >> x2; s >> y2;
r.setCoords(x1, y1, x2, y2);
}
else {
qint32 x1, y1, x2, y2;
s >> x1; s >> y1; s >> x2; s >> y2;
r.setCoords(x1, y1, x2, y2);
}
return s;
}
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRect &r) {
dbg.nospace() << "QRect(" << r.x() << ',' << r.y() << ' '
<< r.width() << 'x' << r.height() << ')';
return dbg.space();
}
#endif
/*!
\class QRectF
\ingroup multimedia
\brief The QRectF class defines a rectangle in the plane using floating
point precision.
A rectangle is normally expressed as an upper-left corner and a
size. The size (width and height) of a QRectF is always equivalent
to the mathematical rectangle that forms the basis for its
rendering.
A QRectF can be constructed with a set of left, top, width and
height integers, or from a QPoint and a QSize. The following code
creates two identical rectangles.
\snippet doc/src/snippets/code/src_corelib_tools_qrect.cpp 1
There is also a third constructor creating a QRectF from a QRect,
and a corresponding toRect() function that returns a QRect object
based on the values of this rectangle (note that the coordinates
in the returned rectangle are rounded to the nearest integer).
The QRectF class provides a collection of functions that return
the various rectangle coordinates, and enable manipulation of
these. QRectF also provide functions to move the rectangle
relative to the various coordinates. In addition there is a
moveTo() function that moves the rectangle, leaving its top left
corner at the given coordinates. Alternatively, the translate()
function moves the rectangle the given offset relative to the
current position, and the translated() function returns a
translated copy of this rectangle.
The size() function returns the rectange's dimensions as a
QSize. The dimensions can also be retrieved separately using the
width() and height() functions. To manipulate the dimensions use
the setSize(), setWidth() or setHeight() functions. Alternatively,
the size can be changed by applying either of the functions
setting the rectangle coordinates, for example, setBottom() or
setRight().
The contains() function tells whether a given point is inside the
rectangle or not, and the intersects() function returns true if
this rectangle intersects with a given rectangle (otherwise
false). The QRectF class also provides the intersected() function
which returns the intersection rectangle, and the united() function
which returns the rectangle that encloses the given rectangle and
this:
\table
\row
\o \inlineimage qrect-intersect.png
\o \inlineimage qrect-unite.png
\row
\o intersected()
\o united()
\endtable
The isEmpty() function returns true if the rectangle's width or
height is less than, or equal to, 0. Note that an empty rectangle
is not valid: The isValid() function returns true if both width
and height is larger than 0. A null rectangle (isNull() == true)
on the other hand, has both width and height set to 0.
Note that due to the way QRect and QRectF are defined, an
empty QRectF is defined in essentially the same way as QRect.
Finally, QRectF objects can be streamed as well as compared.
\tableofcontents
\section1 Rendering
When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
the boundary line of a QRectF will be rendered symmetrically on both
sides of the mathematical rectangle's boundary line. But when
using an aliased painter (the default) other rules apply.
Then, when rendering with a one pixel wide pen the QRectF's boundary
line will be rendered to the right and below the mathematical
rectangle's boundary line.
When rendering with a two pixels wide pen the boundary line will
be split in the middle by the mathematical rectangle. This will be
the case whenever the pen is set to an even number of pixels,
while rendering with a pen with an odd number of pixels, the spare
pixel will be rendered to the right and below the mathematical
rectangle as in the one pixel case.
\table
\row
\o \inlineimage qrect-diagram-zero.png
\o \inlineimage qrectf-diagram-one.png
\row
\o Logical representation
\o One pixel wide pen
\row
\o \inlineimage qrectf-diagram-two.png
\o \inlineimage qrectf-diagram-three.png
\row
\o Two pixel wide pen
\o Three pixel wide pen
\endtable
\section1 Coordinates
The QRectF class provides a collection of functions that return
the various rectangle coordinates, and enable manipulation of
these. QRectF also provide functions to move the rectangle
relative to the various coordinates.
For example: the bottom(), setBottom() and moveBottom() functions:
bottom() returns the y-coordinate of the rectangle's bottom edge,
setBottom() sets the bottom edge of the rectangle to the given y
coordinate (it may change the height, but will never change the
rectangle's top edge) and moveBottom() moves the entire rectangle
vertically, leaving the rectangle's bottom edge at the given y
coordinate and its size unchanged.
\image qrectf-coordinates.png
It is also possible to add offsets to this rectangle's coordinates
using the adjust() function, as well as retrieve a new rectangle
based on adjustments of the original one using the adjusted()
function. If either of the width and height is negative, use the
normalized() function to retrieve a rectangle where the corners
are swapped.
In addition, QRectF provides the getCoords() function which extracts
the position of the rectangle's top-left and bottom-right corner,
and the getRect() function which extracts the rectangle's top-left
corner, width and height. Use the setCoords() and setRect()
function to manipulate the rectangle's coordinates and dimensions
in one go.
\sa QRect, QRegion
*/
/*****************************************************************************
QRectF member functions
*****************************************************************************/
/*!
\fn QRectF::QRectF()
Constructs a null rectangle.
\sa isNull()
*/
/*!
\fn QRectF::QRectF(const QPointF &topLeft, const QSizeF &size)
Constructs a rectangle with the given \a topLeft corner and the given \a size.
\sa setTopLeft(), setSize()
*/
/*!
\fn QRectF::QRectF(const QPointF &topLeft, const QPointF &bottomRight)
\since 4.3
Constructs a rectangle with the given \a topLeft and \a bottomRight corners.
\sa setTopLeft(), setBottomRight()
*/
/*!
\fn QRectF::QRectF(qreal x, qreal y, qreal width, qreal height)
Constructs a rectangle with (\a x, \a y) as its top-left corner
and the given \a width and \a height.
\sa setRect()
*/
/*!
\fn QRectF::QRectF(const QRect &rectangle)
Constructs a QRectF rectangle from the given QRect \a rectangle.
\sa toRect()
*/
/*!
\fn bool QRectF::isNull() const
Returns true if the rectangle is a null rectangle, otherwise returns false.
A null rectangle has both the width and the height set to 0. A
null rectangle is also empty, and hence not valid.
\sa isEmpty(), isValid()
*/
/*!
\fn bool QRectF::isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
An empty rectangle has width() <= 0 or height() <= 0. An empty
rectangle is not valid (i.e., isEmpty() == !isValid()).
Use the normalized() function to retrieve a rectangle where the
corners are swapped.
\sa isNull(), isValid(), normalized()
*/
/*!
\fn bool QRectF::isValid() const
Returns true if the rectangle is valid, otherwise returns false.
A valid rectangle has a width() > 0 and height() > 0. Note that
non-trivial operations like intersections are not defined for
invalid rectangles. A valid rectangle is not empty (i.e., isValid()
== !isEmpty()).
\sa isNull(), isEmpty(), normalized()
*/
/*!
Returns a normalized rectangle; i.e., a rectangle that has a
non-negative width and height.
If width() < 0 the function swaps the left and right corners, and
it swaps the top and bottom corners if height() < 0.
\sa isValid(), isEmpty()
*/
QRectF QRectF::normalized() const
{
QRectF r = *this;
if (r.w < 0) {
r.xp += r.w;
r.w = -r.w;
}
if (r.h < 0) {
r.yp += r.h;
r.h = -r.h;
}
return r;
}
/*!
\fn qreal QRectF::x() const
Returns the x-coordinate of the rectangle's left edge. Equivalent
to left().
\sa setX(), y(), topLeft()
*/
/*!
\fn qreal QRectF::y() const
Returns the y-coordinate of the rectangle's top edge. Equivalent
to top().
\sa setY(), x(), topLeft()
*/
/*!
\fn void QRectF::setLeft(qreal x)
Sets the left edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the right
edge of the rectangle.
Equivalent to setX().
\sa left(), moveLeft()
*/
/*!
\fn void QRectF::setTop(qreal y)
Sets the top edge of the rectangle to the given \a y coordinate. May
change the height, but will never change the bottom edge of the
rectangle.
Equivalent to setY().
\sa top(), moveTop()
*/
/*!
\fn void QRectF::setRight(qreal x)
Sets the right edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the left
edge of the rectangle.
\sa right(), moveRight()
*/
/*!
\fn void QRectF::setBottom(qreal y)
Sets the bottom edge of the rectangle to the given \a y
coordinate. May change the height, but will never change the top
edge of the rectangle.
\sa bottom(), moveBottom()
*/
/*!
\fn void QRectF::setX(qreal x)
Sets the left edge of the rectangle to the given \a x
coordinate. May change the width, but will never change the right
edge of the rectangle.
Equivalent to setLeft().
\sa x(), setY(), setTopLeft()
*/
/*!
\fn void QRectF::setY(qreal y)
Sets the top edge of the rectangle to the given \a y
coordinate. May change the height, but will never change the
bottom edge of the rectangle.
Equivalent to setTop().
\sa y(), setX(), setTopLeft()
*/
/*!
\fn void QRectF::setTopLeft(const QPointF &position)
Set the top-left corner of the rectangle to the given \a
position. May change the size, but will never change the
bottom-right corner of the rectangle.
\sa topLeft(), moveTopLeft()
*/
/*!
\fn void QRectF::setBottomRight(const QPointF &position)
Set the bottom-right corner of the rectangle to the given \a
position. May change the size, but will never change the
top-left corner of the rectangle.
\sa bottomRight(), moveBottomRight()
*/
/*!
\fn void QRectF::setTopRight(const QPointF &position)
Set the top-right corner of the rectangle to the given \a
position. May change the size, but will never change the
bottom-left corner of the rectangle.
\sa topRight(), moveTopRight()
*/
/*!
\fn void QRectF::setBottomLeft(const QPointF &position)
Set the bottom-left corner of the rectangle to the given \a
position. May change the size, but will never change the
top-right corner of the rectangle.
\sa bottomLeft(), moveBottomLeft()
*/
/*!
\fn QPointF QRectF::center() const
Returns the center point of the rectangle.
\sa moveCenter()
*/
/*!
\fn void QRectF::getRect(qreal *x, qreal *y, qreal *width, qreal *height) const
Extracts the position of the rectangle's top-left corner to *\a x and
*\a y, and its dimensions to *\a width and *\a height.
\sa setRect(), getCoords()
*/
/*!
\fn void QRectF::getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const
Extracts the position of the rectangle's top-left corner to *\a x1
and *\a y1, and the position of the bottom-right corner to *\a x2 and
*\a y2.
\sa setCoords(), getRect()
*/
/*!
\fn void QRectF::moveLeft(qreal x)
Moves the rectangle horizontally, leaving the rectangle's left
edge at the given \a x coordinate. The rectangle's size is
unchanged.
\sa left(), setLeft(), moveRight()
*/
/*!
\fn void QRectF::moveTop(qreal y)
Moves the rectangle vertically, leaving the rectangle's top line
at the given \a y coordinate. The rectangle's size is unchanged.
\sa top(), setTop(), moveBottom()
*/
/*!
\fn void QRectF::moveRight(qreal x)
Moves the rectangle horizontally, leaving the rectangle's right
edge at the given \a x coordinate. The rectangle's size is
unchanged.
\sa right(), setRight(), moveLeft()
*/
/*!
\fn void QRectF::moveBottom(qreal y)
Moves the rectangle vertically, leaving the rectangle's bottom
edge at the given \a y coordinate. The rectangle's size is
unchanged.
\sa bottom(), setBottom(), moveTop()
*/
/*!
\fn void QRectF::moveTopLeft(const QPointF &position)
Moves the rectangle, leaving the top-left corner at the given \a
position. The rectangle's size is unchanged.
\sa setTopLeft(), moveTop(), moveLeft()
*/
/*!
\fn void QRectF::moveBottomRight(const QPointF &position)
Moves the rectangle, leaving the bottom-right corner at the given
\a position. The rectangle's size is unchanged.
\sa setBottomRight(), moveBottom(), moveRight()
*/
/*!
\fn void QRectF::moveTopRight(const QPointF &position)
Moves the rectangle, leaving the top-right corner at the given
\a position. The rectangle's size is unchanged.
\sa setTopRight(), moveTop(), moveRight()
*/
/*!
\fn void QRectF::moveBottomLeft(const QPointF &position)
Moves the rectangle, leaving the bottom-left corner at the given
\a position. The rectangle's size is unchanged.
\sa setBottomLeft(), moveBottom(), moveLeft()
*/
/*!
\fn void QRectF::moveTo(qreal x, qreal y)
Moves the rectangle, leaving the top-left corner at the given
position (\a x, \a y). The rectangle's size is unchanged.
\sa translate(), moveTopLeft()
*/
/*!
\fn void QRectF::moveTo(const QPointF &position)
\overload
Moves the rectangle, leaving the top-left corner at the given \a
position.
*/
/*!
\fn void QRectF::translate(qreal dx, qreal dy)
Moves the rectangle \a dx along the x-axis and \a dy along the y-axis,
relative to the current position. Positive values move the rectangle to the
right and downwards.
\sa moveTopLeft(), moveTo(), translated()
*/
/*!
\fn void QRectF::translate(const QPointF &offset)
\overload
Moves the rectangle \a{offset}.\l{QPointF::x()}{x()} along the x
axis and \a{offset}.\l{QPointF::y()}{y()} along the y axis,
relative to the current position.
*/
/*!
\fn QRectF QRectF::translated(qreal dx, qreal dy) const
Returns a copy of the rectangle that is translated \a dx along the
x axis and \a dy along the y axis, relative to the current
position. Positive values move the rectangle to the right and
down.
\sa translate()
*/
/*!
\fn QRectF QRectF::translated(const QPointF &offset) const
\overload
Returns a copy of the rectangle that is translated
\a{offset}.\l{QPointF::x()}{x()} along the x axis and
\a{offset}.\l{QPointF::y()}{y()} along the y axis, relative to the
current position.
*/
/*!
\fn void QRectF::setRect(qreal x, qreal y, qreal width, qreal height)
Sets the coordinates of the rectangle's top-left corner to (\a x,
\a y), and its size to the given \a width and \a height.
\sa getRect(), setCoords()
*/
/*!
\fn void QRectF::setCoords(qreal x1, qreal y1, qreal x2, qreal y2)
Sets the coordinates of the rectangle's top-left corner to (\a x1,
\a y1), and the coordinates of its bottom-right corner to (\a x2,
\a y2).
\sa getCoords() setRect()
*/
/*!
\fn QRectF QRectF::adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
added respectively to the existing coordinates of this rectangle.
\sa adjust()
*/
/*! \fn void QRectF::adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
existing coordinates of the rectangle.
\sa adjusted(), setRect()
*/
/*!
\fn QSizeF QRectF::size() const
Returns the size of the rectangle.
\sa setSize(), width(), height()
*/
/*!
\fn qreal QRectF::width() const
Returns the width of the rectangle.
\sa setWidth(), height(), size()
*/
/*!
\fn qreal QRectF::height() const
Returns the height of the rectangle.
\sa setHeight(), width(), size()
*/
/*!
\fn void QRectF::setWidth(qreal width)
Sets the width of the rectangle to the given \a width. The right
edge is changed, but not the left one.
\sa width(), setSize()
*/
/*!
\fn void QRectF::setHeight(qreal height)
Sets the height of the rectangle to the given \a height. The bottom
edge is changed, but not the top one.
\sa height(), setSize()
*/
/*!
\fn void QRectF::setSize(const QSizeF &size)
Sets the size of the rectangle to the given \a size. The top-left
corner is not moved.
\sa size(), setWidth(), setHeight()
*/
/*!
\fn bool QRectF::contains(const QPointF &point) const
Returns true if the given \a point is inside or on the edge of the
rectangle; otherwise returns false.
\sa intersects()
*/
bool QRectF::contains(const QPointF &p) const
{
qreal l = xp;
qreal r = xp;
if (w < 0)
l += w;
else
r += w;
if (l == r) // null rect
return false;
if (p.x() < l || p.x() > r)
return false;
qreal t = yp;
qreal b = yp;
if (h < 0)
t += h;
else
b += h;
if (t == b) // null rect
return false;
if (p.y() < t || p.y() > b)
return false;
return true;
}
/*!
\fn bool QRectF::contains(qreal x, qreal y) const
\overload
Returns true if the point (\a x, \a y) is inside or on the edge of
the rectangle; otherwise returns false.
*/
/*!
\fn bool QRectF::contains(const QRectF &rectangle) const
\overload
Returns true if the given \a rectangle is inside this rectangle;
otherwise returns false.
*/
bool QRectF::contains(const QRectF &r) const
{
qreal l1 = xp;
qreal r1 = xp;
if (w < 0)
l1 += w;
else
r1 += w;
if (l1 == r1) // null rect
return false;
qreal l2 = r.xp;
qreal r2 = r.xp;
if (r.w < 0)
l2 += r.w;
else
r2 += r.w;
if (l2 == r2) // null rect
return false;
if (l2 < l1 || r2 > r1)
return false;
qreal t1 = yp;
qreal b1 = yp;
if (h < 0)
t1 += h;
else
b1 += h;
if (t1 == b1) // null rect
return false;
qreal t2 = r.yp;
qreal b2 = r.yp;
if (r.h < 0)
t2 += r.h;
else
b2 += r.h;
if (t2 == b2) // null rect
return false;
if (t2 < t1 || b2 > b1)
return false;
return true;
}
/*!
\fn qreal QRectF::left() const
Returns the x-coordinate of the rectangle's left edge. Equivalent
to x().
\sa setLeft(), topLeft(), bottomLeft()
*/
/*!
\fn qreal QRectF::top() const
Returns the y-coordinate of the rectangle's top edge. Equivalent
to y().
\sa setTop(), topLeft(), topRight()
*/
/*!
\fn qreal QRectF::right() const
Returns the x-coordinate of the rectangle's right edge.
\sa setRight(), topRight(), bottomRight()
*/
/*!
\fn qreal QRectF::bottom() const
Returns the y-coordinate of the rectangle's bottom edge.
\sa setBottom(), bottomLeft(), bottomRight()
*/
/*!
\fn QPointF QRectF::topLeft() const
Returns the position of the rectangle's top-left corner.
\sa setTopLeft(), top(), left()
*/
/*!
\fn QPointF QRectF::bottomRight() const
Returns the position of the rectangle's bottom-right corner.
\sa setBottomRight(), bottom(), right()
*/
/*!
\fn QPointF QRectF::topRight() const
Returns the position of the rectangle's top-right corner.
\sa setTopRight(), top(), right()
*/
/*!
\fn QPointF QRectF::bottomLeft() const
Returns the position of the rectangle's bottom-left corner.
\sa setBottomLeft(), bottom(), left()
*/
/*!
\fn QRectF& QRectF::operator|=(const QRectF &rectangle)
Unites this rectangle with the given \a rectangle.
\sa united(), operator|()
*/
/*!
\fn QRectF& QRectF::operator&=(const QRectF &rectangle)
Intersects this rectangle with the given \a rectangle.
\sa intersected(), operator|=()
*/
/*!
\fn QRectF QRectF::operator|(const QRectF &rectangle) const
Returns the bounding rectangle of this rectangle and the given \a rectangle.
\sa united(), operator|=()
*/
QRectF QRectF::operator|(const QRectF &r) const
{
qreal l1 = xp;
qreal r1 = xp;
if (w < 0)
l1 += w;
else
r1 += w;
if (l1 == r1) // null rect
return r;
qreal l2 = r.xp;
qreal r2 = r.xp;
if (r.w < 0)
l2 += r.w;
else
r2 += r.w;
if (l2 == r2) // null rect
return *this;
qreal t1 = yp;
qreal b1 = yp;
if (h < 0)
t1 += h;
else
b1 += h;
if (t1 == b1) // null rect
return r;
qreal t2 = r.yp;
qreal b2 = r.yp;
if (r.h < 0)
t2 += r.h;
else
b2 += r.h;
if (t2 == b2) // null rect
return *this;
QRectF tmp;
tmp.xp = qMin(l1, l2);
tmp.yp = qMin(t1, t2);
tmp.w = qMax(r1, r2) - tmp.xp;
tmp.h = qMax(b1, b2) - tmp.yp;
return tmp;
}
/*!
\fn QRectF QRectF::unite(const QRectF &rectangle) const
\obsolete
Use united(\a rectangle) instead.
*/
/*!
\fn QRectF QRectF::united(const QRectF &rectangle) const
\since 4.2
Returns the bounding rectangle of this rectangle and the given \a
rectangle.
\image qrect-unite.png
\sa intersected()
*/
/*!
\fn QRectF QRectF::operator &(const QRectF &rectangle) const
Returns the intersection of this rectangle and the given \a
rectangle. Returns an empty rectangle if there is no intersection.
\sa operator&=(), intersected()
*/
QRectF QRectF::operator&(const QRectF &r) const
{
qreal l1 = xp;
qreal r1 = xp;
if (w < 0)
l1 += w;
else
r1 += w;
if (l1 == r1) // null rect
return QRectF();
qreal l2 = r.xp;
qreal r2 = r.xp;
if (r.w < 0)
l2 += r.w;
else
r2 += r.w;
if (l2 == r2) // null rect
return QRectF();
if (l1 >= r2 || l2 >= r1)
return QRectF();
qreal t1 = yp;
qreal b1 = yp;
if (h < 0)
t1 += h;
else
b1 += h;
if (t1 == b1) // null rect
return QRectF();
qreal t2 = r.yp;
qreal b2 = r.yp;
if (r.h < 0)
t2 += r.h;
else
b2 += r.h;
if (t2 == b2) // null rect
return QRectF();
if (t1 >= b2 || t2 >= b1)
return QRectF();
QRectF tmp;
tmp.xp = qMax(l1, l2);
tmp.yp = qMax(t1, t2);
tmp.w = qMin(r1, r2) - tmp.xp;
tmp.h = qMin(b1, b2) - tmp.yp;
return tmp;
}
/*!
\fn QRectF QRectF::intersect(const QRectF &rectangle) const
\obsolete
Use intersected(\a rectangle) instead.
*/
/*!
\fn QRectF QRectF::intersected(const QRectF &rectangle) const
\since 4.2
Returns the intersection of this rectangle and the given \a
rectangle. Note that \c {r.intersected(s)} is equivalent to \c
{r & s}.
\image qrect-intersect.png
\sa intersects(), united(), operator&=()
*/
/*!
\fn bool QRectF::intersects(const QRectF &rectangle) const
Returns true if this rectangle intersects with the given \a
rectangle (i.e. there is a non-empty area of overlap between
them), otherwise returns false.
The intersection rectangle can be retrieved using the intersected()
function.
\sa contains()
*/
bool QRectF::intersects(const QRectF &r) const
{
qreal l1 = xp;
qreal r1 = xp;
if (w < 0)
l1 += w;
else
r1 += w;
if (l1 == r1) // null rect
return false;
qreal l2 = r.xp;
qreal r2 = r.xp;
if (r.w < 0)
l2 += r.w;
else
r2 += r.w;
if (l2 == r2) // null rect
return false;
if (l1 >= r2 || l2 >= r1)
return false;
qreal t1 = yp;
qreal b1 = yp;
if (h < 0)
t1 += h;
else
b1 += h;
if (t1 == b1) // null rect
return false;
qreal t2 = r.yp;
qreal b2 = r.yp;
if (r.h < 0)
t2 += r.h;
else
b2 += r.h;
if (t2 == b2) // null rect
return false;
if (t1 >= b2 || t2 >= b1)
return false;
return true;
}
/*!
\fn QRect QRectF::toRect() const
Returns a QRect based on the values of this rectangle. Note that the
coordinates in the returned rectangle are rounded to the nearest integer.
\sa QRectF(), toAlignedRect()
*/
/*!
\fn QRect QRectF::toAlignedRect() const
\since 4.3
Returns a QRect based on the values of this rectangle that is the
smallest possible integer rectangle that completely contains this
rectangle.
\sa toRect()
*/
QRect QRectF::toAlignedRect() const
{
int xmin = int(qFloor(xp));
int xmax = int(qCeil(xp + w));
int ymin = int(qFloor(yp));
int ymax = int(qCeil(yp + h));
return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
}
/*!
\fn void QRectF::moveCenter(const QPointF &position)
Moves the rectangle, leaving the center point at the given \a
position. The rectangle's size is unchanged.
\sa center()
*/
/*!
\fn bool operator==(const QRectF &r1, const QRectF &r2)
\relates QRectF
Returns true if the rectangles \a r1 and \a r2 are equal,
otherwise returns false.
*/
/*!
\fn bool operator!=(const QRectF &r1, const QRectF &r2)
\relates QRectF
Returns true if the rectangles \a r1 and \a r2 are different, otherwise
returns false.
*/
/*****************************************************************************
QRectF stream functions
*****************************************************************************/
#ifndef QT_NO_DATASTREAM
/*!
\fn QDataStream &operator<<(QDataStream &stream, const QRectF &rectangle)
\relates QRectF
Writes the \a rectangle to the \a stream, and returns a reference to the
stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator<<(QDataStream &s, const QRectF &r)
{
s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
return s;
}
/*!
\fn QDataStream &operator>>(QDataStream &stream, QRectF &rectangle)
\relates QRectF
Reads a \a rectangle from the \a stream, and returns a reference to the
stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator>>(QDataStream &s, QRectF &r)
{
double x, y, w, h;
s >> x;
s >> y;
s >> w;
s >> h;
r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
return s;
}
#endif // QT_NO_DATASTREAM
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QRectF &r) {
dbg.nospace() << "QRectF(" << r.x() << ',' << r.y() << ' '
<< r.width() << 'x' << r.height() << ')';
return dbg.space();
}
#endif
QT_END_NAMESPACE
|