Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrect.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qrect.h"
6#include "qdatastream.h"
7#include "qmath.h"
8
9#include <private/qdebug_p.h>
10
12
13/*!
14 \class QRect
15 \inmodule QtCore
16 \ingroup painting
17 \reentrant
18
19 \compares equality
20 \compareswith equality QRectF
21 \endcompareswith
22
23 \brief The QRect class defines a rectangle in the plane using
24 integer precision.
25
26 A rectangle is normally expressed as a top-left corner and a
27 size. The size (width and height) of a QRect is always equivalent
28 to the mathematical rectangle that forms the basis for its
29 rendering.
30
31 A QRect can be constructed with a set of left, top, width and
32 height integers, or from a QPoint and a QSize. The following code
33 creates two identical rectangles.
34
35 \snippet code/src_corelib_tools_qrect.cpp 0
36
37 There is a third constructor that creates a QRect using the
38 top-left and bottom-right coordinates, but we recommend that you
39 avoid using it. The rationale is that for historical reasons the
40 values returned by the bottom() and right() functions deviate from
41 the true bottom-right corner of the rectangle.
42
43 The QRect class provides a collection of functions that return the
44 various rectangle coordinates, and enable manipulation of
45 these. QRect also provides functions to move the rectangle relative
46 to the various coordinates. In addition there is a moveTo()
47 function that moves the rectangle, leaving its top left corner at
48 the given coordinates. Alternatively, the translate() function
49 moves the rectangle the given offset relative to the current
50 position, and the translated() function returns a translated copy
51 of this rectangle.
52
53 The size() function returns the rectangle's dimensions as a
54 QSize. The dimensions can also be retrieved separately using the
55 width() and height() functions. To manipulate the dimensions use
56 the setSize(), setWidth() or setHeight() functions. Alternatively,
57 the size can be changed by applying either of the functions
58 setting the rectangle coordinates, for example, setBottom() or
59 setRight().
60
61 The contains() function tells whether a given point is inside the
62 rectangle or not, and the intersects() function returns \c true if
63 this rectangle intersects with a given rectangle. The QRect class
64 also provides the intersected() function which returns the
65 intersection rectangle, and the united() function which returns the
66 rectangle that encloses the given rectangle and this:
67
68 \table
69 \row
70 \li \inlineimage qrect-intersect.png {Diagram showing intersection
71 of two rectangles r and s with the overlapping area
72 highlighted. It's also showing how the width and
73 height of the highlighted area is determined with
74 r.intersect(s).width() and r.intersect(s).height().}
75 \li \inlineimage qrect-unite.webp {Diagram showing two overlapped
76 rectangles r and s and their united size which is
77 determined with r.united(s).width() and
78 r.united(s).height().}
79 \row
80 \li intersected()
81 \li united()
82 \endtable
83
84 The isEmpty() function returns \c true if left() > right() or top() >
85 bottom(). Note that an empty rectangle is not valid: The isValid()
86 function returns \c true if left() <= right() \e and top() <=
87 bottom(). A null rectangle (isNull() == true) on the other hand,
88 has both width and height set to 0.
89
90 Note that due to the way QRect and QRectF are defined, an
91 empty QRect is defined in essentially the same way as QRectF.
92
93 Finally, QRect objects can be streamed as well as compared.
94
95 \section1 Rendering
96
97 When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
98 the boundary line of a QRect will be rendered symmetrically on
99 both sides of the mathematical rectangle's boundary line. But when
100 using an aliased painter (the default) other rules apply.
101
102 Then, when rendering with a one pixel wide pen the QRect's boundary
103 line will be rendered to the right and below the mathematical
104 rectangle's boundary line.
105
106 When rendering with a two pixels wide pen the boundary line will
107 be split in the middle by the mathematical rectangle. This will be
108 the case whenever the pen is set to an even number of pixels,
109 while rendering with a pen with an odd number of pixels, the spare
110 pixel will be rendered to the right and below the mathematical
111 rectangle as in the one pixel case.
112
113 \table
114 \row
115 \li \inlineimage qrect-diagram-zero.png
116 \li \inlineimage qrect-diagram-one.png
117 \row
118 \li Logical representation
119 \li One pixel wide pen
120 \row
121 \li \inlineimage qrect-diagram-two.png
122 \li \inlineimage qrect-diagram-three.png
123 \row
124 \li Two pixel wide pen
125 \li Three pixel wide pen
126 \endtable
127
128 \section1 Coordinates
129
130 The QRect class provides a collection of functions that return the
131 various rectangle coordinates, and enable manipulation of
132 these. QRect also provides functions to move the rectangle relative
133 to the various coordinates.
134
135 For example the left(), setLeft() and moveLeft() functions as an
136 example: left() returns the x-coordinate of the rectangle's left
137 edge, setLeft() sets the left edge of the rectangle to the given x
138 coordinate (it may change the width, but will never change the
139 rectangle's right edge) and moveLeft() moves the entire rectangle
140 horizontally, leaving the rectangle's left edge at the given x
141 coordinate and its size unchanged.
142
143 \image qrect-coordinates.png {Diagram of a rectangle showing QRect
144 methods for positions: x(), y(), center(), corners (topLeft(),
145 topRight(), bottomLeft(), bottomRight()), and edges (top(),
146 bottom(), left(), right()).}
147
148 Note that for historical reasons the values returned by the
149 bottom() and right() functions deviate from the true bottom-right
150 corner of the rectangle: The right() function returns \e { left()
151 + width() - 1} and the bottom() function returns \e {top() +
152 height() - 1}. The same is the case for the point returned by the
153 bottomRight() convenience function. In addition, the x and y
154 coordinate of the topRight() and bottomLeft() functions,
155 respectively, contain the same deviation from the true right and
156 bottom edges.
157
158 We recommend that you use x() + width() and y() + height() to find
159 the true bottom-right corner, and avoid right() and
160 bottom(). Another solution is to use QRectF: The QRectF class
161 defines a rectangle in the plane using floating point accuracy for
162 coordinates, and the QRectF::right() and QRectF::bottom()
163 functions \e do return the right and bottom coordinates.
164
165 It is also possible to add offsets to this rectangle's coordinates
166 using the adjust() function, as well as retrieve a new rectangle
167 based on adjustments of the original one using the adjusted()
168 function. If either of the width and height is negative, use the
169 normalized() function to retrieve a rectangle where the corners
170 are swapped.
171
172 In addition, QRect provides the getCoords() function which extracts
173 the position of the rectangle's top-left and bottom-right corner,
174 and the getRect() function which extracts the rectangle's top-left
175 corner, width and height. Use the setCoords() and setRect()
176 function to manipulate the rectangle's coordinates and dimensions
177 in one go.
178
179 \section1 Constraints
180
181 QRect is limited to the minimum and maximum values for the \c int type.
182 Operations on a QRect that could potentially result in values outside this
183 range will result in undefined behavior.
184
185 \sa QRectF, QRegion
186*/
187
188/*****************************************************************************
189 QRect member functions
190 *****************************************************************************/
191
192/*!
193 \fn QRect::QRect()
194
195 Constructs a null rectangle.
196
197 \sa isNull()
198*/
199
200/*!
201 \fn QRect::QRect(const QPoint &topLeft, const QPoint &bottomRight)
202
203 Constructs a rectangle with the given \a topLeft and \a bottomRight corners, both included.
204
205 If \a bottomRight is to higher and to the left of \a topLeft, the rectangle defined
206 is instead non-inclusive of the corners.
207
208 \note To ensure both points are included regardless of relative order, use span().
209
210 \sa setTopLeft(), setBottomRight(), span()
211*/
212
213
214/*!
215 \fn QRect::QRect(const QPoint &topLeft, const QSize &size)
216
217 Constructs a rectangle with the given \a topLeft corner and the
218 given \a size.
219
220 \sa setTopLeft(), setSize()
221*/
222
223
224/*!
225 \fn QRect::QRect(int x, int y, int width, int height)
226
227 Constructs a rectangle with (\a x, \a y) as its top-left corner
228 and the given \a width and \a height.
229
230 \sa setRect()
231*/
232
233
234/*!
235 \fn bool QRect::isNull() const
236
237 Returns \c true if the rectangle is a null rectangle, otherwise
238 returns \c false.
239
240 A null rectangle has both the width and the height set to 0 (i.e.,
241 right() == left() - 1 and bottom() == top() - 1). A null rectangle
242 is also empty, and hence is not valid.
243
244 \sa isEmpty(), isValid()
245*/
246
247/*!
248 \fn bool QRect::isEmpty() const
249
250 Returns \c true if the rectangle is empty, otherwise returns \c false.
251
252 An empty rectangle has a left() > right() or top() > bottom(). An
253 empty rectangle is not valid (i.e., isEmpty() == !isValid()).
254
255 Use the normalized() function to retrieve a rectangle where the
256 corners are swapped.
257
258 \sa isNull(), isValid(), normalized()
259*/
260
261/*!
262 \fn bool QRect::isValid() const
263
264 Returns \c true if the rectangle is valid, otherwise returns \c false.
265
266 A valid rectangle has a left() <= right() and top() <=
267 bottom(). Note that non-trivial operations like intersections are
268 not defined for invalid rectangles. A valid rectangle is not empty
269 (i.e., isValid() == !isEmpty()).
270
271 \sa isNull(), isEmpty(), normalized()
272*/
273
274
275/*!
276 Returns a normalized rectangle; i.e., a rectangle that has a
277 non-negative width and height.
278
279 If width() < 0 the function swaps the left and right corners, and
280 it swaps the top and bottom corners if height() < 0. The corners
281 are at the same time changed from being non-inclusive to inclusive.
282
283 \sa isValid(), isEmpty()
284*/
285
286QRect QRect::normalized() const noexcept
287{
288 QRect r(*this);
289 if (x2 < x1) { // swap bad x values
290 r.x1 = x2 + 1;
291 r.x2 = x1 - 1;
292 }
293 if (y2 < y1) { // swap bad y values
294 r.y1 = y2 + 1;
295 r.y2 = y1 - 1;
296 }
297 return r;
298}
299
300
301/*!
302 \fn int QRect::left() const
303
304 Returns the x-coordinate of the rectangle's left edge. Equivalent
305 to x().
306
307 \sa setLeft(), topLeft(), bottomLeft()
308*/
309
310/*!
311 \fn int QRect::top() const
312
313 Returns the y-coordinate of the rectangle's top edge.
314 Equivalent to y().
315
316 \sa setTop(), topLeft(), topRight()
317*/
318
319/*!
320 \fn int QRect::right() const
321
322 Returns the x-coordinate of the rectangle's right edge.
323
324 Note that for historical reasons this function returns left() +
325 width() - 1; use x() + width() to retrieve the true x-coordinate.
326
327 \sa setRight(), topRight(), bottomRight()
328*/
329
330/*!
331 \fn int QRect::bottom() const
332
333 Returns the y-coordinate of the rectangle's bottom edge.
334
335 Note that for historical reasons this function returns top() +
336 height() - 1; use y() + height() to retrieve the true y-coordinate.
337
338 \sa setBottom(), bottomLeft(), bottomRight()
339*/
340
341/*!
342 \fn int QRect::x() const
343
344 Returns the x-coordinate of the rectangle's left edge. Equivalent to left().
345
346 \sa setX(), y(), topLeft()
347*/
348
349/*!
350 \fn int QRect::y() const
351
352 Returns the y-coordinate of the rectangle's top edge. Equivalent to top().
353
354 \sa setY(), x(), topLeft()
355*/
356
357/*!
358 \fn void QRect::setLeft(int x)
359
360 Sets the left edge of the rectangle to the given \a x
361 coordinate. May change the width, but will never change the right
362 edge of the rectangle.
363
364 Equivalent to setX().
365
366 \sa left(), moveLeft()
367*/
368
369/*!
370 \fn void QRect::setTop(int y)
371
372 Sets the top edge of the rectangle to the given \a y
373 coordinate. May change the height, but will never change the
374 bottom edge of the rectangle.
375
376 Equivalent to setY().
377
378 \sa top(), moveTop()
379*/
380
381/*!
382 \fn void QRect::setRight(int x)
383
384 Sets the right edge of the rectangle to the given \a x
385 coordinate. May change the width, but will never change the left
386 edge of the rectangle.
387
388 \sa right(), moveRight()
389*/
390
391/*!
392 \fn void QRect::setBottom(int y)
393
394 Sets the bottom edge of the rectangle to the given \a y
395 coordinate. May change the height, but will never change the top
396 edge of the rectangle.
397
398 \sa bottom(), moveBottom(),
399*/
400
401/*!
402 \fn void QRect::setX(int x)
403
404 Sets the left edge of the rectangle to the given \a x
405 coordinate. May change the width, but will never change the right
406 edge of the rectangle.
407
408 Equivalent to setLeft().
409
410 \sa x(), setY(), setTopLeft()
411*/
412
413/*!
414 \fn void QRect::setY(int y)
415
416 Sets the top edge of the rectangle to the given \a y
417 coordinate. May change the height, but will never change the
418 bottom edge of the rectangle.
419
420 Equivalent to setTop().
421
422 \sa y(), setX(), setTopLeft()
423*/
424
425/*!
426 \fn void QRect::setTopLeft(const QPoint &position)
427
428 Set the top-left corner of the rectangle to the given \a
429 position. May change the size, but will never change the
430 bottom-right corner of the rectangle.
431
432 \sa topLeft(), moveTopLeft()
433*/
434
435/*!
436 \fn void QRect::setBottomRight(const QPoint &position)
437
438 Set the bottom-right corner of the rectangle to the given \a
439 position. May change the size, but will never change the
440 top-left corner of the rectangle.
441
442 \sa bottomRight(), moveBottomRight()
443*/
444
445/*!
446 \fn void QRect::setTopRight(const QPoint &position)
447
448 Set the top-right corner of the rectangle to the given \a
449 position. May change the size, but will never change the
450 bottom-left corner of the rectangle.
451
452 \sa topRight(), moveTopRight()
453*/
454
455/*!
456 \fn void QRect::setBottomLeft(const QPoint &position)
457
458 Set the bottom-left corner of the rectangle to the given \a
459 position. May change the size, but will never change the
460 top-right corner of the rectangle.
461
462 \sa bottomLeft(), moveBottomLeft()
463*/
464
465/*!
466 \fn QPoint QRect::topLeft() const
467
468 Returns the position of the rectangle's top-left corner.
469
470 \sa setTopLeft(), top(), left()
471*/
472
473/*!
474 \fn QPoint QRect::bottomRight() const
475
476 Returns the position of the rectangle's bottom-right corner.
477
478 Note that for historical reasons this function returns
479 QPoint(left() + width() -1, top() + height() - 1).
480
481 \sa setBottomRight(), bottom(), right()
482*/
483
484/*!
485 \fn QPoint QRect::topRight() const
486
487 Returns the position of the rectangle's top-right corner.
488
489 Note that for historical reasons this function returns
490 QPoint(left() + width() -1, top()).
491
492 \sa setTopRight(), top(), right()
493*/
494
495/*!
496 \fn QPoint QRect::bottomLeft() const
497
498 Returns the position of the rectangle's bottom-left corner. Note
499 that for historical reasons this function returns QPoint(left(),
500 top() + height() - 1).
501
502 \sa setBottomLeft(), bottom(), left()
503*/
504
505/*!
506 \fn QPoint QRect::center() const
507
508 Returns the center point of the rectangle.
509
510 \sa moveCenter()
511*/
512
513
514/*!
515 \fn void QRect::getRect(int *x, int *y, int *width, int *height) const
516
517 Extracts the position of the rectangle's top-left corner to *\a x
518 and *\a y, and its dimensions to *\a width and *\a height.
519
520 \sa setRect(), getCoords()
521*/
522
523
524/*!
525 \fn void QRect::getCoords(int *x1, int *y1, int *x2, int *y2) const
526
527 Extracts the position of the rectangle's top-left corner to *\a x1
528 and *\a y1, and the position of the bottom-right corner to *\a x2
529 and *\a y2.
530
531 \sa setCoords(), getRect()
532*/
533
534/*!
535 \fn void QRect::moveLeft(int x)
536
537 Moves the rectangle horizontally, leaving the rectangle's left
538 edge at the given \a x coordinate. The rectangle's size is
539 unchanged.
540
541 \sa left(), setLeft(), moveRight()
542*/
543
544/*!
545 \fn void QRect::moveTop(int y)
546
547 Moves the rectangle vertically, leaving the rectangle's top edge
548 at the given \a y coordinate. The rectangle's size is unchanged.
549
550 \sa top(), setTop(), moveBottom()
551*/
552
553
554/*!
555 \fn void QRect::moveRight(int x)
556
557 Moves the rectangle horizontally, leaving the rectangle's right
558 edge at the given \a x coordinate. The rectangle's size is
559 unchanged.
560
561 \sa right(), setRight(), moveLeft()
562*/
563
564
565/*!
566 \fn void QRect::moveBottom(int y)
567
568 Moves the rectangle vertically, leaving the rectangle's bottom
569 edge at the given \a y coordinate. The rectangle's size is
570 unchanged.
571
572 \sa bottom(), setBottom(), moveTop()
573*/
574
575
576/*!
577 \fn void QRect::moveTopLeft(const QPoint &position)
578
579 Moves the rectangle, leaving the top-left corner at the given \a
580 position. The rectangle's size is unchanged.
581
582 \sa setTopLeft(), moveTop(), moveLeft()
583*/
584
585
586/*!
587 \fn void QRect::moveBottomRight(const QPoint &position)
588
589 Moves the rectangle, leaving the bottom-right corner at the given
590 \a position. The rectangle's size is unchanged.
591
592 \sa setBottomRight(), moveRight(), moveBottom()
593*/
594
595
596/*!
597 \fn void QRect::moveTopRight(const QPoint &position)
598
599 Moves the rectangle, leaving the top-right corner at the given \a
600 position. The rectangle's size is unchanged.
601
602 \sa setTopRight(), moveTop(), moveRight()
603*/
604
605
606/*!
607 \fn void QRect::moveBottomLeft(const QPoint &position)
608
609 Moves the rectangle, leaving the bottom-left corner at the given
610 \a position. The rectangle's size is unchanged.
611
612 \sa setBottomLeft(), moveBottom(), moveLeft()
613*/
614
615
616/*!
617 \fn void QRect::moveCenter(const QPoint &position)
618
619 Moves the rectangle, leaving the center point at the given \a
620 position. The rectangle's size is unchanged.
621
622 \sa center()
623*/
624
625/*!
626 \fn void QRect::moveTo(int x, int y)
627
628 Moves the rectangle, leaving the top-left corner at the given
629 position (\a x, \a y). The rectangle's size is unchanged.
630
631 \sa translate(), moveTopLeft()
632*/
633
634/*!
635 \fn void QRect::moveTo(const QPoint &position)
636
637 Moves the rectangle, leaving the top-left corner at the given \a
638 position.
639*/
640
641/*!
642 \fn void QRect::translate(int dx, int dy)
643
644 Moves the rectangle \a dx along the x axis and \a dy along the y
645 axis, relative to the current position. Positive values move the
646 rectangle to the right and down.
647
648 \sa moveTopLeft(), moveTo(), translated()
649*/
650
651
652/*!
653 \fn void QRect::translate(const QPoint &offset)
654 \overload
655
656 Moves the rectangle \a{offset}.\l{QPoint::x()}{x()} along the x
657 axis and \a{offset}.\l{QPoint::y()}{y()} along the y axis,
658 relative to the current position.
659*/
660
661
662/*!
663 \fn QRect QRect::translated(int dx, int dy) const
664
665 Returns a copy of the rectangle that is translated \a dx along the
666 x axis and \a dy along the y axis, relative to the current
667 position. Positive values move the rectangle to the right and
668 down.
669
670 \sa translate()
671
672*/
673
674
675/*!
676 \fn QRect QRect::translated(const QPoint &offset) const
677
678 \overload
679
680 Returns a copy of the rectangle that is translated
681 \a{offset}.\l{QPoint::x()}{x()} along the x axis and
682 \a{offset}.\l{QPoint::y()}{y()} along the y axis, relative to the
683 current position.
684*/
685
686/*!
687 \fn QRect QRect::transposed() const
688 \since 5.7
689
690 Returns a copy of the rectangle that has its width and height
691 exchanged:
692
693 \snippet code/src_corelib_tools_qrect.cpp 2
694
695 \sa QSize::transposed()
696*/
697
698/*!
699 \fn void QRect::setRect(int x, int y, int width, int height)
700
701 Sets the coordinates of the rectangle's top-left corner to (\a{x},
702 \a{y}), and its size to the given \a width and \a height.
703
704 \sa getRect(), setCoords()
705*/
706
707
708/*!
709 \fn void QRect::setCoords(int x1, int y1, int x2, int y2)
710
711 Sets the coordinates of the rectangle's top-left corner to (\a x1,
712 \a y1), and the coordinates of its bottom-right corner to (\a x2,
713 \a y2).
714
715 \sa getCoords(), setRect()
716*/
717
718
719/*! \fn QRect QRect::adjusted(int dx1, int dy1, int dx2, int dy2) const
720
721 Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
722 added respectively to the existing coordinates of this rectangle.
723
724 \sa adjust()
725*/
726
727/*! \fn void QRect::adjust(int dx1, int dy1, int dx2, int dy2)
728
729 Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
730 existing coordinates of the rectangle.
731
732 \sa adjusted(), setRect()
733*/
734
735/*!
736 \fn QSize QRect::size() const
737
738 Returns the size of the rectangle.
739
740 \sa setSize(), width(), height()
741*/
742
743/*!
744 \fn int QRect::width() const
745
746 Returns the width of the rectangle.
747
748 \sa setWidth(), height(), size()
749*/
750
751/*!
752 \fn int QRect::height() const
753
754 Returns the height of the rectangle.
755
756 \sa setHeight(), width(), size()
757*/
758
759/*!
760 \fn void QRect::setWidth(int width)
761
762 Sets the width of the rectangle to the given \a width. The right
763 edge is changed, but not the left one.
764
765 \sa width(), setSize()
766*/
767
768
769/*!
770 \fn void QRect::setHeight(int height)
771
772 Sets the height of the rectangle to the given \a height. The bottom
773 edge is changed, but not the top one.
774
775 \sa height(), setSize()
776*/
777
778
779/*!
780 \fn void QRect::setSize(const QSize &size)
781
782 Sets the size of the rectangle to the given \a size. The top-left
783 corner is not moved.
784
785 \sa size(), setWidth(), setHeight()
786*/
787
788
789/*!
790 \fn bool QRect::contains(const QPoint &point, bool proper) const
791
792 Returns \c true if the given \a point is inside or on the edge of
793 the rectangle, otherwise returns \c false. If \a proper is true, this
794 function only returns \c true if the given \a point is \e inside the
795 rectangle (i.e., not on the edge).
796
797 \sa intersects()
798*/
799
800bool QRect::contains(const QPoint &p, bool proper) const noexcept
801{
802 Representation l, r;
803 if (x2 < x1 - 1) {
804 l = x2 + 1;
805 r = x1 - 1;
806 } else {
807 l = x1;
808 r = x2;
809 }
810 if (proper) {
811 if (p.x() <= l || p.x() >= r)
812 return false;
813 } else {
814 if (p.x() < l || p.x() > r)
815 return false;
816 }
817 Representation t, b;
818 if (y2 < y1 - 1) {
819 t = y2 + 1;
820 b = y1 - 1;
821 } else {
822 t = y1;
823 b = y2;
824 }
825 if (proper) {
826 if (p.y() <= t || p.y() >= b)
827 return false;
828 } else {
829 if (p.y() < t || p.y() > b)
830 return false;
831 }
832 return true;
833}
834
835
836/*!
837 \fn bool QRect::contains(int x, int y, bool proper) const
838 \overload
839
840 Returns \c true if the point (\a x, \a y) is inside or on the edge of
841 the rectangle, otherwise returns \c false. If \a proper is true, this
842 function only returns \c true if the point is entirely inside the
843 rectangle(not on the edge).
844*/
845
846/*!
847 \fn bool QRect::contains(int x, int y) const
848 \overload
849
850 Returns \c true if the point (\a x, \a y) is inside this rectangle,
851 otherwise returns \c false.
852*/
853
854/*!
855 \fn bool QRect::contains(const QRect &rectangle, bool proper) const
856 \overload
857
858 Returns \c true if the given \a rectangle is inside this rectangle.
859 otherwise returns \c false. If \a proper is true, this function only
860 returns \c true if the \a rectangle is entirely inside this
861 rectangle (not on the edge).
862*/
863
864bool QRect::contains(const QRect &r, bool proper) const noexcept
865{
866 if (isNull() || r.isNull())
867 return false;
868
869 Representation l1 = x1;
870 Representation r1 = x1 - 1;
871 if (x2 < x1 - 1)
872 l1 = x2 + 1;
873 else
874 r1 = x2;
875
876 Representation l2 = r.x1;
877 Representation r2 = r.x1 - 1;
878 if (r.x2 < r.x1 - 1)
879 l2 = r.x2 + 1;
880 else
881 r2 = r.x2;
882
883 if (proper) {
884 if (l2 <= l1 || r2 >= r1)
885 return false;
886 } else {
887 if (l2 < l1 || r2 > r1)
888 return false;
889 }
890
891 Representation t1 = y1;
892 Representation b1 = y1 - 1;
893 if (y2 < y1 - 1)
894 t1 = y2 + 1;
895 else
896 b1 = y2;
897
898 Representation t2 = r.y1;
899 Representation b2 = r.y1 - 1;
900 if (r.y2 < r.y1 - 1)
901 t2 = r.y2 + 1;
902 else
903 b2 = r.y2;
904
905 if (proper) {
906 if (t2 <= t1 || b2 >= b1)
907 return false;
908 } else {
909 if (t2 < t1 || b2 > b1)
910 return false;
911 }
912
913 return true;
914}
915
916/*!
917 \fn QRect& QRect::operator|=(const QRect &rectangle)
918
919 Unites this rectangle with the given \a rectangle.
920
921 \sa united(), operator|()
922*/
923
924/*!
925 \fn QRect& QRect::operator&=(const QRect &rectangle)
926
927 Intersects this rectangle with the given \a rectangle.
928
929 \sa intersected(), operator&()
930*/
931
932
933/*!
934 \fn QRect QRect::operator|(const QRect &rectangle) const
935
936 Returns the bounding rectangle of this rectangle and the given \a
937 rectangle.
938
939 \sa operator|=(), united()
940*/
941
942QRect QRect::operator|(const QRect &r) const noexcept
943{
944 if (isNull())
945 return r;
946 if (r.isNull())
947 return *this;
948
949 Representation l1 = x1;
950 Representation r1 = x1 - 1;
951 if (x2 < x1 - 1)
952 l1 = x2 + 1;
953 else
954 r1 = x2;
955
956 Representation l2 = r.x1;
957 Representation r2 = r.x1 - 1;
958 if (r.x2 < r.x1 - 1)
959 l2 = r.x2 + 1;
960 else
961 r2 = r.x2;
962
963 Representation t1 = y1;
964 Representation b1 = y1 - 1;
965 if (y2 < y1 - 1)
966 t1 = y2 + 1;
967 else
968 b1 = y2;
969
970 Representation t2 = r.y1;
971 Representation b2 = r.y1 - 1;
972 if (r.y2 < r.y1 - 1)
973 t2 = r.y2 + 1;
974 else
975 b2 = r.y2;
976
977 QRect tmp;
978 tmp.x1 = qMin(l1, l2);
979 tmp.x2 = qMax(r1, r2);
980 tmp.y1 = qMin(t1, t2);
981 tmp.y2 = qMax(b1, b2);
982 return tmp;
983}
984
985/*!
986 \fn QRect QRect::united(const QRect &rectangle) const
987 \since 4.2
988
989 Returns the bounding rectangle of this rectangle and the given \a rectangle.
990
991 \image qrect-unite.webp {Diagram showing two overlapped rectangles r and s
992 and their united size which is determined with r.united(s).width and
993 r.united(s).height().}
994
995 \sa intersected()
996*/
997
998
999/*!
1000 \fn QRect QRect::operator&(const QRect &rectangle) const
1001
1002 Returns the intersection of this rectangle and the given \a
1003 rectangle. Returns an empty rectangle if there is no intersection.
1004
1005 \sa operator&=(), intersected()
1006*/
1007
1008QRect QRect::operator&(const QRect &r) const noexcept
1009{
1010 if (isNull() || r.isNull())
1011 return QRect();
1012
1013 Representation l1 = x1;
1014 Representation r1 = x2;
1015 if (x2 < x1 - 1) {
1016 l1 = x2 + 1;
1017 r1 = x1 - 1;
1018 }
1019
1020 Representation l2 = r.x1;
1021 Representation r2 = r.x2;
1022 if (r.x2 < r.x1 - 1) {
1023 l2 = r.x2 + 1;
1024 r2 = r.x1 - 1;
1025 }
1026
1027 if (l1 > r2 || l2 > r1)
1028 return QRect();
1029
1030 Representation t1 = y1;
1031 Representation b1 = y2;
1032 if (y2 < y1 - 1) {
1033 t1 = y2 + 1;
1034 b1 = y1 - 1;
1035 }
1036
1037 Representation t2 = r.y1;
1038 Representation b2 = r.y2;
1039 if (r.y2 < r.y1 - 1) {
1040 t2 = r.y2 + 1;
1041 b2 = r.y1 - 1;
1042 }
1043
1044 if (t1 > b2 || t2 > b1)
1045 return QRect();
1046
1047 QRect tmp;
1048 tmp.x1 = qMax(l1, l2);
1049 tmp.x2 = qMin(r1, r2);
1050 tmp.y1 = qMax(t1, t2);
1051 tmp.y2 = qMin(b1, b2);
1052 return tmp;
1053}
1054
1055/*!
1056 \fn QRect QRect::intersected(const QRect &rectangle) const
1057 \since 4.2
1058
1059 Returns the intersection of this rectangle and the given \a
1060 rectangle. Note that \c{r.intersected(s)} is equivalent to \c{r & s}.
1061
1062 \image qrect-intersect.png {Diagram showing intersection of two
1063 rectangles r and s with the overlapping area highlighted.
1064 It also shows how the width and height of the highlighted area
1065 is determined with r.intersect(s).width and
1066 r.intersect(s).height().}
1067
1068 \sa intersects(), united(), operator&=()
1069*/
1070
1071/*!
1072 \fn bool QRect::intersects(const QRect &rectangle) const
1073
1074 Returns \c true if this rectangle intersects with the given \a
1075 rectangle (i.e., there is at least one pixel that is within both
1076 rectangles), otherwise returns \c false.
1077
1078 The intersection rectangle can be retrieved using the intersected()
1079 function.
1080
1081 \sa contains()
1082*/
1083
1084bool QRect::intersects(const QRect &r) const noexcept
1085{
1086 if (isNull() || r.isNull())
1087 return false;
1088
1089 Representation l1 = x1;
1090 Representation r1 = x2;
1091 if (x2 < x1 - 1) {
1092 l1 = x2 + 1;
1093 r1 = x1 - 1;
1094 }
1095
1096 Representation l2 = r.x1;
1097 Representation r2 = r.x2;
1098 if (r.x2 < r.x1 - 1) {
1099 l2 = r.x2 + 1;
1100 r2 = r.x1 - 1;
1101 }
1102
1103 if (l1 > r2 || l2 > r1)
1104 return false;
1105
1106 Representation t1 = y1;
1107 Representation b1 = y2;
1108 if (y2 < y1 - 1) {
1109 t1 = y2 + 1;
1110 b1 = y1 - 1;
1111 }
1112
1113 Representation t2 = r.y1;
1114 Representation b2 = r.y2;
1115 if (r.y2 < r.y1 - 1) {
1116 t2 = r.y2 + 1;
1117 b2 = r.y1 - 1;
1118 }
1119
1120 if (t1 > b2 || t2 > b1)
1121 return false;
1122
1123 return true;
1124}
1125
1126/*!
1127 \fn bool QRect::operator==(const QRect &lhs, const QRect &rhs)
1128
1129 Returns \c true if the rectangles \a lhs and \a rhs are equal,
1130 otherwise returns \c false.
1131*/
1132
1133
1134/*!
1135 \fn bool QRect::operator!=(const QRect &lhs, const QRect &rhs)
1136
1137 Returns \c true if the rectangles \a lhs and \a rhs are different,
1138 otherwise returns \c false.
1139*/
1140
1141/*!
1142 \fn QRect operator+(const QRect &rectangle, const QMargins &margins)
1143 \relates QRect
1144
1145 Returns the \a rectangle grown by the \a margins.
1146
1147 \since 5.1
1148*/
1149
1150/*!
1151 \fn QRect operator+(const QMargins &margins, const QRect &rectangle)
1152 \relates QRect
1153 \overload
1154
1155 Returns the \a rectangle grown by the \a margins.
1156
1157 \since 5.1
1158*/
1159
1160/*!
1161 \fn QRect operator-(const QRect &lhs, const QMargins &rhs)
1162 \relates QRect
1163
1164 Returns the \a lhs rectangle shrunk by the \a rhs margins.
1165
1166 \since 5.3
1167*/
1168
1169/*!
1170 \fn QRect QRect::marginsAdded(const QMargins &margins) const
1171
1172 Returns a rectangle grown by the \a margins.
1173
1174 \sa operator+=(), marginsRemoved(), operator-=()
1175
1176 \since 5.1
1177*/
1178
1179/*!
1180 \fn QRect QRect::operator+=(const QMargins &margins)
1181
1182 Adds the \a margins to the rectangle, growing it.
1183
1184 \sa marginsAdded(), marginsRemoved(), operator-=()
1185
1186 \since 5.1
1187*/
1188
1189/*!
1190 \fn QRect QRect::marginsRemoved(const QMargins &margins) const
1191
1192 Removes the \a margins from the rectangle, shrinking it.
1193
1194 \sa marginsAdded(), operator+=(), operator-=()
1195
1196 \since 5.1
1197*/
1198
1199/*!
1200 \fn QRect QRect::operator -=(const QMargins &margins)
1201
1202 Returns a rectangle shrunk by the \a margins.
1203
1204 \sa marginsRemoved(), operator+=(), marginsAdded()
1205
1206 \since 5.1
1207*/
1208
1209/*!
1210 \fn static QRect QRect::span(const QPoint &p1, const QPoint &p2)
1211
1212 Returns a rectangle spanning the two points \a p1 and \a p2, including both and
1213 everything in between.
1214
1215 \since 6.0
1216*/
1217
1218/*!
1219 \fn QRect::toRectF() const
1220 \since 6.4
1221
1222 Returns this rectangle as a rectangle with floating point accuracy.
1223
1224 \note This function, like the QRectF(QRect) constructor, preserves the
1225 size() of the rectangle, not its bottomRight() corner.
1226
1227 \sa QRectF::toRect()
1228*/
1229
1230/*****************************************************************************
1231 QRect stream functions
1232 *****************************************************************************/
1233#ifndef QT_NO_DATASTREAM
1234/*!
1235 \fn QDataStream &operator<<(QDataStream &stream, const QRect &rectangle)
1236 \relates QRect
1237
1238 Writes the given \a rectangle to the given \a stream, and returns
1239 a reference to the stream.
1240
1241 \sa {Serializing Qt Data Types}
1242*/
1243
1244QDataStream &operator<<(QDataStream &s, const QRect &r)
1245{
1246 if (s.version() == 1)
1247 s << (qint16)r.left() << (qint16)r.top()
1248 << (qint16)r.right() << (qint16)r.bottom();
1249 else
1250 s << (qint32)r.left() << (qint32)r.top()
1251 << (qint32)r.right() << (qint32)r.bottom();
1252 return s;
1253}
1254
1255/*!
1256 \fn QDataStream &operator>>(QDataStream &stream, QRect &rectangle)
1257 \relates QRect
1258
1259 Reads a rectangle from the given \a stream into the given \a
1260 rectangle, and returns a reference to the stream.
1261
1262 \sa {Serializing Qt Data Types}
1263*/
1264
1265QDataStream &operator>>(QDataStream &s, QRect &r)
1266{
1267 if (s.version() == 1) {
1268 qint16 x1, y1, x2, y2;
1269 s >> x1; s >> y1; s >> x2; s >> y2;
1270 r.setCoords(x1, y1, x2, y2);
1271 }
1272 else {
1273 qint32 x1, y1, x2, y2;
1274 s >> x1; s >> y1; s >> x2; s >> y2;
1275 r.setCoords(x1, y1, x2, y2);
1276 }
1277 return s;
1278}
1279
1280#endif // QT_NO_DATASTREAM
1281
1282
1283#ifndef QT_NO_DEBUG_STREAM
1284QDebug operator<<(QDebug dbg, const QRect &r)
1285{
1286 QDebugStateSaver saver(dbg);
1287 dbg.nospace();
1288 dbg << "QRect" << '(';
1289 QtDebugUtils::formatQRect(dbg, r);
1290 dbg << ')';
1291 return dbg;
1292}
1293#endif
1294
1295/*!
1296 \class QRectF
1297 \inmodule QtCore
1298 \ingroup painting
1299 \reentrant
1300
1301 \compares equality
1302 \compareswith equality QRect
1303 \endcompareswith
1304
1305 \brief The QRectF class defines a finite rectangle in the plane using
1306 floating point precision.
1307
1308 A rectangle is normally expressed as a top-left corner and a
1309 size. The size (width and height) of a QRectF is always equivalent
1310 to the mathematical rectangle that forms the basis for its
1311 rendering.
1312
1313 A QRectF can be constructed with a set of left, top, width and
1314 height coordinates, or from a QPointF and a QSizeF. The following
1315 code creates two identical rectangles.
1316
1317 \snippet code/src_corelib_tools_qrect.cpp 1
1318
1319 There is also a third constructor creating a QRectF from a QRect,
1320 and a corresponding toRect() function that returns a QRect object
1321 based on the values of this rectangle (note that the coordinates
1322 in the returned rectangle are rounded to the nearest integer).
1323
1324 The QRectF class provides a collection of functions that return
1325 the various rectangle coordinates, and enable manipulation of
1326 these. QRectF also provides functions to move the rectangle
1327 relative to the various coordinates. In addition there is a
1328 moveTo() function that moves the rectangle, leaving its top left
1329 corner at the given coordinates. Alternatively, the translate()
1330 function moves the rectangle the given offset relative to the
1331 current position, and the translated() function returns a
1332 translated copy of this rectangle.
1333
1334 The size() function returns the rectangle's dimensions as a
1335 QSizeF. The dimensions can also be retrieved separately using the
1336 width() and height() functions. To manipulate the dimensions use
1337 the setSize(), setWidth() or setHeight() functions. Alternatively,
1338 the size can be changed by applying either of the functions
1339 setting the rectangle coordinates, for example, setBottom() or
1340 setRight().
1341
1342 The contains() function tells whether a given point is inside the
1343 rectangle or not, and the intersects() function returns \c true if
1344 this rectangle intersects with a given rectangle (otherwise
1345 false). The QRectF class also provides the intersected() function
1346 which returns the intersection rectangle, and the united() function
1347 which returns the rectangle that encloses the given rectangle and
1348 this:
1349
1350 \table
1351 \row
1352 \li \inlineimage qrect-intersect.png {Diagram showing intersection
1353 of two rectangles r and s with the overlapping area
1354 highlighted. It's also showing how the width and
1355 height of the highlighted area is determined with
1356 r.intersect(s).width and r.intersect(s).height().}
1357 \li \inlineimage qrect-unite.webp {Diagram showing two overlapped
1358 rectangles r and s and their united size which is
1359 determined with r.united(s).width and
1360 r.united(s).height()}
1361 \row
1362 \li intersected()
1363 \li united()
1364 \endtable
1365
1366 The isEmpty() function returns \c true if the rectangle's width or
1367 height is less than, or equal to, 0. Note that an empty rectangle
1368 is not valid: The isValid() function returns \c true if both width
1369 and height is larger than 0. A null rectangle (isNull() == true)
1370 on the other hand, has both width and height set to 0.
1371
1372 Note that due to the way QRect and QRectF are defined, an
1373 empty QRectF is defined in essentially the same way as QRect.
1374
1375 Finally, QRectF objects can be streamed as well as compared.
1376
1377 \section1 Rendering
1378
1379 When using an \l {QPainter::Antialiasing}{anti-aliased} painter,
1380 the boundary line of a QRectF will be rendered symmetrically on both
1381 sides of the mathematical rectangle's boundary line. But when
1382 using an aliased painter (the default) other rules apply.
1383
1384 Then, when rendering with a one pixel wide pen the QRectF's boundary
1385 line will be rendered to the right and below the mathematical
1386 rectangle's boundary line.
1387
1388 When rendering with a two pixels wide pen the boundary line will
1389 be split in the middle by the mathematical rectangle. This will be
1390 the case whenever the pen is set to an even number of pixels,
1391 while rendering with a pen with an odd number of pixels, the spare
1392 pixel will be rendered to the right and below the mathematical
1393 rectangle as in the one pixel case.
1394
1395 \table
1396 \row
1397 \li \inlineimage qrect-diagram-zero.png
1398 \li \inlineimage qrectf-diagram-one.png
1399 \row
1400 \li Logical representation
1401 \li One pixel wide pen
1402 \row
1403 \li \inlineimage qrectf-diagram-two.png
1404 \li \inlineimage qrectf-diagram-three.png
1405 \row
1406 \li Two pixel wide pen
1407 \li Three pixel wide pen
1408 \endtable
1409
1410 \section1 Coordinates
1411
1412 The QRectF class provides a collection of functions that return
1413 the various rectangle coordinates, and enable manipulation of
1414 these. QRectF also provides functions to move the rectangle
1415 relative to the various coordinates.
1416
1417 For example: the bottom(), setBottom() and moveBottom() functions:
1418 bottom() returns the y-coordinate of the rectangle's bottom edge,
1419 setBottom() sets the bottom edge of the rectangle to the given y
1420 coordinate (it may change the height, but will never change the
1421 rectangle's top edge) and moveBottom() moves the entire rectangle
1422 vertically, leaving the rectangle's bottom edge at the given y
1423 coordinate and its size unchanged.
1424
1425 \image qrectf-coordinates.png {Diagram of a rectangle showing QRect
1426 methods for positions: x(), y(), center(), corners (topLeft(),
1427 topRight(), bottomLeft(), bottomRight()), and edges (top(),
1428 bottom(), left(), right()).}
1429
1430 It is also possible to add offsets to this rectangle's coordinates
1431 using the adjust() function, as well as retrieve a new rectangle
1432 based on adjustments of the original one using the adjusted()
1433 function. If either of the width and height is negative, use the
1434 normalized() function to retrieve a rectangle where the corners
1435 are swapped.
1436
1437 In addition, QRectF provides the getCoords() function which extracts
1438 the position of the rectangle's top-left and bottom-right corner,
1439 and the getRect() function which extracts the rectangle's top-left
1440 corner, width and height. Use the setCoords() and setRect()
1441 function to manipulate the rectangle's coordinates and dimensions
1442 in one go.
1443
1444 \sa QRect, QRegion
1445*/
1446
1447/*****************************************************************************
1448 QRectF member functions
1449 *****************************************************************************/
1450
1451/*!
1452 \fn QRectF::QRectF()
1453
1454 Constructs a null rectangle.
1455
1456 \sa isNull()
1457*/
1458
1459/*!
1460 \fn QRectF::QRectF(const QPointF &topLeft, const QSizeF &size)
1461
1462 Constructs a rectangle with the given \a topLeft corner and the given \a size.
1463
1464 \sa setTopLeft(), setSize()
1465*/
1466
1467/*!
1468 \fn QRectF::QRectF(const QPointF &topLeft, const QPointF &bottomRight)
1469 \since 4.3
1470
1471 Constructs a rectangle with the given \a topLeft and \a bottomRight corners.
1472
1473 \sa setTopLeft(), setBottomRight()
1474*/
1475
1476/*!
1477 \fn QRectF::QRectF(qreal x, qreal y, qreal width, qreal height)
1478
1479 Constructs a rectangle with (\a x, \a y) as its top-left corner and the
1480 given \a width and \a height. All parameters must be finite.
1481
1482 \sa setRect()
1483*/
1484
1485/*!
1486 \fn QRectF::QRectF(const QRect &rectangle)
1487
1488 Constructs a QRectF rectangle from the given QRect \a rectangle.
1489
1490 \note This function, like QRect::toRectF(), preserves the size() of
1491 \a rectangle, not its bottomRight() corner.
1492
1493 \sa toRect(), QRect::toRectF()
1494*/
1495
1496/*!
1497 \fn bool QRectF::isNull() const
1498
1499 Returns \c true if the rectangle is a null rectangle, otherwise returns \c false.
1500
1501 A null rectangle has both the width and the height set to 0. A
1502 null rectangle is also empty, and hence not valid.
1503
1504 \sa isEmpty(), isValid()
1505*/
1506
1507/*!
1508 \fn bool QRectF::isEmpty() const
1509
1510 Returns \c true if the rectangle is empty, otherwise returns \c false.
1511
1512 An empty rectangle has width() <= 0 or height() <= 0. An empty
1513 rectangle is not valid (i.e., isEmpty() == !isValid()).
1514
1515 Use the normalized() function to retrieve a rectangle where the
1516 corners are swapped.
1517
1518 \sa isNull(), isValid(), normalized()
1519*/
1520
1521/*!
1522 \fn bool QRectF::isValid() const
1523
1524 Returns \c true if the rectangle is valid, otherwise returns \c false.
1525
1526 A valid rectangle has a width() > 0 and height() > 0. Note that
1527 non-trivial operations like intersections are not defined for
1528 invalid rectangles. A valid rectangle is not empty (i.e., isValid()
1529 == !isEmpty()).
1530
1531 \sa isNull(), isEmpty(), normalized()
1532*/
1533
1534
1535/*!
1536 Returns a normalized rectangle; i.e., a rectangle that has a
1537 non-negative width and height.
1538
1539 If width() < 0 the function swaps the left and right corners, and
1540 it swaps the top and bottom corners if height() < 0.
1541
1542 \sa isValid(), isEmpty()
1543*/
1544
1545QRectF QRectF::normalized() const noexcept
1546{
1547 QRectF r = *this;
1548 if (r.w < 0) {
1549 r.xp += r.w;
1550 r.w = -r.w;
1551 }
1552 if (r.h < 0) {
1553 r.yp += r.h;
1554 r.h = -r.h;
1555 }
1556 return r;
1557}
1558
1559/*!
1560 \fn qreal QRectF::x() const
1561
1562 Returns the x-coordinate of the rectangle's left edge. Equivalent
1563 to left().
1564
1565
1566 \sa setX(), y(), topLeft()
1567*/
1568
1569/*!
1570 \fn qreal QRectF::y() const
1571
1572 Returns the y-coordinate of the rectangle's top edge. Equivalent
1573 to top().
1574
1575 \sa setY(), x(), topLeft()
1576*/
1577
1578
1579/*!
1580 \fn void QRectF::setLeft(qreal x)
1581
1582 Sets the left edge of the rectangle to the given finite \a x
1583 coordinate. May change the width, but will never change the right
1584 edge of the rectangle.
1585
1586 Equivalent to setX().
1587
1588 \sa left(), moveLeft()
1589*/
1590
1591/*!
1592 \fn void QRectF::setTop(qreal y)
1593
1594 Sets the top edge of the rectangle to the given finite \a y coordinate. May
1595 change the height, but will never change the bottom edge of the
1596 rectangle.
1597
1598 Equivalent to setY().
1599
1600 \sa top(), moveTop()
1601*/
1602
1603/*!
1604 \fn void QRectF::setRight(qreal x)
1605
1606 Sets the right edge of the rectangle to the given finite \a x
1607 coordinate. May change the width, but will never change the left
1608 edge of the rectangle.
1609
1610 \sa right(), moveRight()
1611*/
1612
1613/*!
1614 \fn void QRectF::setBottom(qreal y)
1615
1616 Sets the bottom edge of the rectangle to the given finite \a y
1617 coordinate. May change the height, but will never change the top
1618 edge of the rectangle.
1619
1620 \sa bottom(), moveBottom()
1621*/
1622
1623/*!
1624 \fn void QRectF::setX(qreal x)
1625
1626 Sets the left edge of the rectangle to the given finite \a x
1627 coordinate. May change the width, but will never change the right
1628 edge of the rectangle.
1629
1630 Equivalent to setLeft().
1631
1632 \sa x(), setY(), setTopLeft()
1633*/
1634
1635/*!
1636 \fn void QRectF::setY(qreal y)
1637
1638 Sets the top edge of the rectangle to the given finite \a y
1639 coordinate. May change the height, but will never change the
1640 bottom edge of the rectangle.
1641
1642 Equivalent to setTop().
1643
1644 \sa y(), setX(), setTopLeft()
1645*/
1646
1647/*!
1648 \fn void QRectF::setTopLeft(const QPointF &position)
1649
1650 Set the top-left corner of the rectangle to the given \a
1651 position. May change the size, but will never change the
1652 bottom-right corner of the rectangle.
1653
1654 \sa topLeft(), moveTopLeft()
1655*/
1656
1657/*!
1658 \fn void QRectF::setBottomRight(const QPointF &position)
1659
1660 Set the bottom-right corner of the rectangle to the given \a
1661 position. May change the size, but will never change the
1662 top-left corner of the rectangle.
1663
1664 \sa bottomRight(), moveBottomRight()
1665*/
1666
1667/*!
1668 \fn void QRectF::setTopRight(const QPointF &position)
1669
1670 Set the top-right corner of the rectangle to the given \a
1671 position. May change the size, but will never change the
1672 bottom-left corner of the rectangle.
1673
1674 \sa topRight(), moveTopRight()
1675*/
1676
1677/*!
1678 \fn void QRectF::setBottomLeft(const QPointF &position)
1679
1680 Set the bottom-left corner of the rectangle to the given \a
1681 position. May change the size, but will never change the
1682 top-right corner of the rectangle.
1683
1684 \sa bottomLeft(), moveBottomLeft()
1685*/
1686
1687/*!
1688 \fn QPointF QRectF::center() const
1689
1690 Returns the center point of the rectangle.
1691
1692 \sa moveCenter()
1693*/
1694
1695
1696/*!
1697 \fn void QRectF::getRect(qreal *x, qreal *y, qreal *width, qreal *height) const
1698
1699 Extracts the position of the rectangle's top-left corner to *\a x and
1700 *\a y, and its dimensions to *\a width and *\a height.
1701
1702 \sa setRect(), getCoords()
1703*/
1704
1705
1706/*!
1707 \fn void QRectF::getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const
1708
1709 Extracts the position of the rectangle's top-left corner to *\a x1
1710 and *\a y1, and the position of the bottom-right corner to *\a x2 and
1711 *\a y2.
1712
1713 \sa setCoords(), getRect()
1714*/
1715
1716/*!
1717 \fn void QRectF::moveLeft(qreal x)
1718
1719 Moves the rectangle horizontally, leaving the rectangle's left
1720 edge at the given finite \a x coordinate. The rectangle's size is
1721 unchanged.
1722
1723 \sa left(), setLeft(), moveRight()
1724*/
1725
1726/*!
1727 \fn void QRectF::moveTop(qreal y)
1728
1729 Moves the rectangle vertically, leaving the rectangle's top line
1730 at the given finite \a y coordinate. The rectangle's size is unchanged.
1731
1732 \sa top(), setTop(), moveBottom()
1733*/
1734
1735
1736/*!
1737 \fn void QRectF::moveRight(qreal x)
1738
1739 Moves the rectangle horizontally, leaving the rectangle's right
1740 edge at the given finite \a x coordinate. The rectangle's size is
1741 unchanged.
1742
1743 \sa right(), setRight(), moveLeft()
1744*/
1745
1746
1747/*!
1748 \fn void QRectF::moveBottom(qreal y)
1749
1750 Moves the rectangle vertically, leaving the rectangle's bottom
1751 edge at the given finite \a y coordinate. The rectangle's size is
1752 unchanged.
1753
1754 \sa bottom(), setBottom(), moveTop()
1755*/
1756
1757
1758/*!
1759 \fn void QRectF::moveTopLeft(const QPointF &position)
1760
1761 Moves the rectangle, leaving the top-left corner at the given \a
1762 position. The rectangle's size is unchanged.
1763
1764 \sa setTopLeft(), moveTop(), moveLeft()
1765*/
1766
1767
1768/*!
1769 \fn void QRectF::moveBottomRight(const QPointF &position)
1770
1771 Moves the rectangle, leaving the bottom-right corner at the given
1772 \a position. The rectangle's size is unchanged.
1773
1774 \sa setBottomRight(), moveBottom(), moveRight()
1775*/
1776
1777
1778/*!
1779 \fn void QRectF::moveTopRight(const QPointF &position)
1780
1781 Moves the rectangle, leaving the top-right corner at the given
1782 \a position. The rectangle's size is unchanged.
1783
1784 \sa setTopRight(), moveTop(), moveRight()
1785*/
1786
1787
1788/*!
1789 \fn void QRectF::moveBottomLeft(const QPointF &position)
1790
1791 Moves the rectangle, leaving the bottom-left corner at the given
1792 \a position. The rectangle's size is unchanged.
1793
1794 \sa setBottomLeft(), moveBottom(), moveLeft()
1795*/
1796
1797
1798/*!
1799 \fn void QRectF::moveTo(qreal x, qreal y)
1800
1801 Moves the rectangle, leaving the top-left corner at the given position (\a
1802 x, \a y). The rectangle's size is unchanged. Both parameters must be finite.
1803
1804 \sa translate(), moveTopLeft()
1805*/
1806
1807/*!
1808 \fn void QRectF::moveTo(const QPointF &position)
1809 \overload
1810
1811 Moves the rectangle, leaving the top-left corner at the given \a
1812 position.
1813*/
1814
1815/*!
1816 \fn void QRectF::translate(qreal dx, qreal dy)
1817
1818 Moves the rectangle \a dx along the x-axis and \a dy along the y-axis,
1819 relative to the current position. Positive values move the rectangle to the
1820 right and downwards. Both parameters must be finite.
1821
1822 \sa moveTopLeft(), moveTo(), translated()
1823*/
1824
1825
1826/*!
1827 \fn void QRectF::translate(const QPointF &offset)
1828 \overload
1829
1830 Moves the rectangle \a{offset}.\l{QPointF::x()}{x()} along the x
1831 axis and \a{offset}.\l{QPointF::y()}{y()} along the y axis,
1832 relative to the current position.
1833*/
1834
1835
1836/*!
1837 \fn QRectF QRectF::translated(qreal dx, qreal dy) const
1838
1839 Returns a copy of the rectangle that is translated \a dx along the
1840 x axis and \a dy along the y axis, relative to the current
1841 position. Positive values move the rectangle to the right and
1842 down. Both parameters must be finite.
1843
1844 \sa translate()
1845*/
1846
1847
1848/*!
1849 \fn QRectF QRectF::translated(const QPointF &offset) const
1850 \overload
1851
1852 Returns a copy of the rectangle that is translated
1853 \a{offset}.\l{QPointF::x()}{x()} along the x axis and
1854 \a{offset}.\l{QPointF::y()}{y()} along the y axis, relative to the
1855 current position.
1856*/
1857
1858/*!
1859 \fn QRectF QRectF::transposed() const
1860 \since 5.7
1861
1862 Returns a copy of the rectangle that has its width and height
1863 exchanged:
1864
1865 \snippet code/src_corelib_tools_qrect.cpp 3
1866
1867 \sa QSizeF::transposed()
1868*/
1869
1870/*!
1871 \fn void QRectF::setRect(qreal x, qreal y, qreal width, qreal height)
1872
1873 Sets the coordinates of the rectangle's top-left corner to (\a x, \a y), and
1874 its size to the given \a width and \a height. All parameters must be finite.
1875
1876 \sa getRect(), setCoords()
1877*/
1878
1879
1880/*!
1881 \fn void QRectF::setCoords(qreal x1, qreal y1, qreal x2, qreal y2)
1882
1883 Sets the coordinates of the rectangle's top-left corner to (\a x1,
1884 \a y1), and the coordinates of its bottom-right corner to (\a x2,
1885 \a y2). All parameters must be finite.
1886
1887 \sa getCoords(), setRect()
1888*/
1889
1890/*!
1891 \fn QRectF QRectF::adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
1892
1893 Returns a new rectangle with \a dx1, \a dy1, \a dx2 and \a dy2
1894 added respectively to the existing coordinates of this rectangle.
1895 All parameters must be finite.
1896
1897 \sa adjust()
1898*/
1899
1900/*! \fn void QRectF::adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
1901
1902 Adds \a dx1, \a dy1, \a dx2 and \a dy2 respectively to the
1903 existing coordinates of the rectangle. All parameters must be finite.
1904
1905 \sa adjusted(), setRect()
1906*/
1907/*!
1908 \fn QSizeF QRectF::size() const
1909
1910 Returns the size of the rectangle.
1911
1912 \sa setSize(), width(), height()
1913*/
1914
1915/*!
1916 \fn qreal QRectF::width() const
1917
1918 Returns the width of the rectangle.
1919
1920 \sa setWidth(), height(), size()
1921*/
1922
1923/*!
1924 \fn qreal QRectF::height() const
1925
1926 Returns the height of the rectangle.
1927
1928 \sa setHeight(), width(), size()
1929*/
1930
1931/*!
1932 \fn void QRectF::setWidth(qreal width)
1933
1934 Sets the width of the rectangle to the given finite \a width. The right
1935 edge is changed, but not the left one.
1936
1937 \sa width(), setSize()
1938*/
1939
1940
1941/*!
1942 \fn void QRectF::setHeight(qreal height)
1943
1944 Sets the height of the rectangle to the given finite \a height. The bottom
1945 edge is changed, but not the top one.
1946
1947 \sa height(), setSize()
1948*/
1949
1950
1951/*!
1952 \fn void QRectF::setSize(const QSizeF &size)
1953
1954 Sets the size of the rectangle to the given finite \a size. The top-left
1955 corner is not moved.
1956
1957 \sa size(), setWidth(), setHeight()
1958*/
1959
1960
1961/*!
1962 \fn bool QRectF::contains(const QPointF &point) const
1963
1964 Returns \c true if the given \a point is inside or on the edge of the
1965 rectangle; otherwise returns \c false.
1966
1967 \sa intersects()
1968*/
1969
1970bool QRectF::contains(const QPointF &p) const noexcept
1971{
1972 qreal l = xp;
1973 qreal r = xp;
1974 if (w < 0)
1975 l += w;
1976 else
1977 r += w;
1978 if (l == r) // null rect
1979 return false;
1980
1981 if (p.x() < l || p.x() > r)
1982 return false;
1983
1984 qreal t = yp;
1985 qreal b = yp;
1986 if (h < 0)
1987 t += h;
1988 else
1989 b += h;
1990 if (t == b) // null rect
1991 return false;
1992
1993 if (p.y() < t || p.y() > b)
1994 return false;
1995
1996 return true;
1997}
1998
1999
2000/*!
2001 \fn bool QRectF::contains(qreal x, qreal y) const
2002 \overload
2003
2004 Returns \c true if the point (\a x, \a y) is inside or on the edge of
2005 the rectangle; otherwise returns \c false.
2006*/
2007
2008/*!
2009 \fn bool QRectF::contains(const QRectF &rectangle) const
2010 \overload
2011
2012 Returns \c true if the given \a rectangle is inside this rectangle;
2013 otherwise returns \c false.
2014*/
2015
2016bool QRectF::contains(const QRectF &r) const noexcept
2017{
2018 qreal l1 = xp;
2019 qreal r1 = xp;
2020 if (w < 0)
2021 l1 += w;
2022 else
2023 r1 += w;
2024 if (l1 == r1) // null rect
2025 return false;
2026
2027 qreal l2 = r.xp;
2028 qreal r2 = r.xp;
2029 if (r.w < 0)
2030 l2 += r.w;
2031 else
2032 r2 += r.w;
2033 if (l2 == r2) // null rect
2034 return false;
2035
2036 if (l2 < l1 || r2 > r1)
2037 return false;
2038
2039 qreal t1 = yp;
2040 qreal b1 = yp;
2041 if (h < 0)
2042 t1 += h;
2043 else
2044 b1 += h;
2045 if (t1 == b1) // null rect
2046 return false;
2047
2048 qreal t2 = r.yp;
2049 qreal b2 = r.yp;
2050 if (r.h < 0)
2051 t2 += r.h;
2052 else
2053 b2 += r.h;
2054 if (t2 == b2) // null rect
2055 return false;
2056
2057 if (t2 < t1 || b2 > b1)
2058 return false;
2059
2060 return true;
2061}
2062
2063/*!
2064 \fn qreal QRectF::left() const
2065
2066 Returns the x-coordinate of the rectangle's left edge. Equivalent
2067 to x().
2068
2069 \sa setLeft(), topLeft(), bottomLeft()
2070*/
2071
2072/*!
2073 \fn qreal QRectF::top() const
2074
2075 Returns the y-coordinate of the rectangle's top edge. Equivalent
2076 to y().
2077
2078 \sa setTop(), topLeft(), topRight()
2079*/
2080
2081/*!
2082 \fn qreal QRectF::right() const
2083
2084 Returns the x-coordinate of the rectangle's right edge.
2085
2086 \sa setRight(), topRight(), bottomRight()
2087*/
2088
2089/*!
2090 \fn qreal QRectF::bottom() const
2091
2092 Returns the y-coordinate of the rectangle's bottom edge.
2093
2094 \sa setBottom(), bottomLeft(), bottomRight()
2095*/
2096
2097/*!
2098 \fn QPointF QRectF::topLeft() const
2099
2100 Returns the position of the rectangle's top-left corner.
2101
2102 \sa setTopLeft(), top(), left()
2103*/
2104
2105/*!
2106 \fn QPointF QRectF::bottomRight() const
2107
2108 Returns the position of the rectangle's bottom-right corner.
2109
2110 \sa setBottomRight(), bottom(), right()
2111*/
2112
2113/*!
2114 \fn QPointF QRectF::topRight() const
2115
2116 Returns the position of the rectangle's top-right corner.
2117
2118 \sa setTopRight(), top(), right()
2119*/
2120
2121/*!
2122 \fn QPointF QRectF::bottomLeft() const
2123
2124 Returns the position of the rectangle's bottom-left corner.
2125
2126 \sa setBottomLeft(), bottom(), left()
2127*/
2128
2129/*!
2130 \fn QRectF& QRectF::operator|=(const QRectF &rectangle)
2131
2132 Unites this rectangle with the given \a rectangle.
2133
2134 \sa united(), operator|()
2135*/
2136
2137/*!
2138 \fn QRectF& QRectF::operator&=(const QRectF &rectangle)
2139
2140 Intersects this rectangle with the given \a rectangle.
2141
2142 \sa intersected(), operator&()
2143*/
2144
2145
2146/*!
2147 \fn QRectF QRectF::operator|(const QRectF &rectangle) const
2148
2149 Returns the bounding rectangle of this rectangle and the given \a rectangle.
2150
2151 \sa united(), operator|=()
2152*/
2153
2154QRectF QRectF::operator|(const QRectF &r) const noexcept
2155{
2156 if (isNull())
2157 return r;
2158 if (r.isNull())
2159 return *this;
2160
2161 qreal left = xp;
2162 qreal right = xp;
2163 if (w < 0)
2164 left += w;
2165 else
2166 right += w;
2167
2168 if (r.w < 0) {
2169 left = qMin(left, r.xp + r.w);
2170 right = qMax(right, r.xp);
2171 } else {
2172 left = qMin(left, r.xp);
2173 right = qMax(right, r.xp + r.w);
2174 }
2175
2176 qreal top = yp;
2177 qreal bottom = yp;
2178 if (h < 0)
2179 top += h;
2180 else
2181 bottom += h;
2182
2183 if (r.h < 0) {
2184 top = qMin(top, r.yp + r.h);
2185 bottom = qMax(bottom, r.yp);
2186 } else {
2187 top = qMin(top, r.yp);
2188 bottom = qMax(bottom, r.yp + r.h);
2189 }
2190
2191 return QRectF(left, top, right - left, bottom - top);
2192}
2193
2194/*!
2195 \fn QRectF QRectF::united(const QRectF &rectangle) const
2196 \since 4.2
2197
2198 Returns the bounding rectangle of this rectangle and the given \a
2199 rectangle.
2200
2201 \image qrect-unite.webp {Diagram showing two overlapped rectangles
2202 r and s and their united size which is determined with
2203 r.united(s).width and r.united(s).height()}
2204
2205 \sa intersected()
2206*/
2207
2208
2209/*!
2210 \fn QRectF QRectF::operator &(const QRectF &rectangle) const
2211
2212 Returns the intersection of this rectangle and the given \a
2213 rectangle. Returns an empty rectangle if there is no intersection.
2214
2215 \sa operator&=(), intersected()
2216*/
2217
2218QRectF QRectF::operator&(const QRectF &r) const noexcept
2219{
2220 qreal l1 = xp;
2221 qreal r1 = xp;
2222 if (w < 0)
2223 l1 += w;
2224 else
2225 r1 += w;
2226 if (l1 == r1) // null rect
2227 return QRectF();
2228
2229 qreal l2 = r.xp;
2230 qreal r2 = r.xp;
2231 if (r.w < 0)
2232 l2 += r.w;
2233 else
2234 r2 += r.w;
2235 if (l2 == r2) // null rect
2236 return QRectF();
2237
2238 if (l1 >= r2 || l2 >= r1)
2239 return QRectF();
2240
2241 qreal t1 = yp;
2242 qreal b1 = yp;
2243 if (h < 0)
2244 t1 += h;
2245 else
2246 b1 += h;
2247 if (t1 == b1) // null rect
2248 return QRectF();
2249
2250 qreal t2 = r.yp;
2251 qreal b2 = r.yp;
2252 if (r.h < 0)
2253 t2 += r.h;
2254 else
2255 b2 += r.h;
2256 if (t2 == b2) // null rect
2257 return QRectF();
2258
2259 if (t1 >= b2 || t2 >= b1)
2260 return QRectF();
2261
2262 QRectF tmp;
2263 tmp.xp = qMax(l1, l2);
2264 tmp.yp = qMax(t1, t2);
2265 tmp.w = qMin(r1, r2) - tmp.xp;
2266 tmp.h = qMin(b1, b2) - tmp.yp;
2267 return tmp;
2268}
2269
2270/*!
2271 \fn QRectF QRectF::intersected(const QRectF &rectangle) const
2272 \since 4.2
2273
2274 Returns the intersection of this rectangle and the given \a
2275 rectangle. Note that \c {r.intersected(s)} is equivalent to \c
2276 {r & s}.
2277
2278 \image qrect-intersect.png {Diagram showing intersection of two
2279 rectangles r and s with the overlapping area highlighted.
2280 It's also showing how the width and height of the
2281 highlighted area is determined with r.intersect(s).width
2282 and r.intersect(s).height().}
2283
2284 \sa intersects(), united(), operator&=()
2285*/
2286
2287/*!
2288 \fn bool QRectF::intersects(const QRectF &rectangle) const
2289
2290 Returns \c true if this rectangle intersects with the given \a
2291 rectangle (i.e. there is a non-empty area of overlap between
2292 them), otherwise returns \c false.
2293
2294 The intersection rectangle can be retrieved using the intersected()
2295 function.
2296
2297 \sa contains()
2298*/
2299
2300bool QRectF::intersects(const QRectF &r) const noexcept
2301{
2302 qreal l1 = xp;
2303 qreal r1 = xp;
2304 if (w < 0)
2305 l1 += w;
2306 else
2307 r1 += w;
2308 if (l1 == r1) // null rect
2309 return false;
2310
2311 qreal l2 = r.xp;
2312 qreal r2 = r.xp;
2313 if (r.w < 0)
2314 l2 += r.w;
2315 else
2316 r2 += r.w;
2317 if (l2 == r2) // null rect
2318 return false;
2319
2320 if (l1 >= r2 || l2 >= r1)
2321 return false;
2322
2323 qreal t1 = yp;
2324 qreal b1 = yp;
2325 if (h < 0)
2326 t1 += h;
2327 else
2328 b1 += h;
2329 if (t1 == b1) // null rect
2330 return false;
2331
2332 qreal t2 = r.yp;
2333 qreal b2 = r.yp;
2334 if (r.h < 0)
2335 t2 += r.h;
2336 else
2337 b2 += r.h;
2338 if (t2 == b2) // null rect
2339 return false;
2340
2341 if (t1 >= b2 || t2 >= b1)
2342 return false;
2343
2344 return true;
2345}
2346
2347/*!
2348 \fn QRect QRectF::toRect() const
2349
2350 Returns a QRect based on the values of this rectangle. Note that the
2351 coordinates in the returned rectangle are rounded to the nearest integer.
2352
2353 \sa QRectF(), toAlignedRect(), QRect::toRectF()
2354*/
2355
2356/*!
2357 \fn QRect QRectF::toAlignedRect() const
2358 \since 4.3
2359
2360 Returns a QRect based on the values of this rectangle that is the
2361 smallest possible integer rectangle that completely contains this
2362 rectangle.
2363
2364 \sa toRect()
2365*/
2366
2367QRect QRectF::toAlignedRect() const noexcept
2368{
2369 int xmin = qFloor(xp);
2370 int xmax = qCeil(xp + w);
2371 int ymin = qFloor(yp);
2372 int ymax = qCeil(yp + h);
2373 return QRect(xmin, ymin, xmax - xmin, ymax - ymin);
2374}
2375
2376/*!
2377 \fn void QRectF::moveCenter(const QPointF &position)
2378
2379 Moves the rectangle, leaving the center point at the given \a
2380 position. The rectangle's size is unchanged.
2381
2382 \sa center()
2383*/
2384
2385/*!
2386 \fn bool QRectF::operator==(const QRectF &lhs, const QRectF &rhs)
2387
2388 Returns \c true if the rectangles \a lhs and \a rhs are \b approximately
2389 equal, otherwise returns \c false.
2390
2391 \warning This function does not check for strict equality; instead,
2392 it uses a fuzzy comparison to compare the rectangles' coordinates.
2393
2394 \sa qFuzzyCompare
2395*/
2396
2397
2398/*!
2399 \fn bool QRectF::operator!=(const QRectF &lhs, const QRectF &rhs)
2400
2401 Returns \c true if the rectangles \a lhs and \a rhs are sufficiently
2402 different, otherwise returns \c false.
2403
2404 \warning This function does not check for strict inequality; instead,
2405 it uses a fuzzy comparison to compare the rectangles' coordinates.
2406*/
2407
2408/*!
2409 \fn QRectF operator+(const QRectF &lhs, const QMarginsF &rhs)
2410 \relates QRectF
2411 \since 5.3
2412
2413 Returns the \a lhs rectangle grown by the \a rhs margins.
2414*/
2415
2416/*!
2417 \fn QRectF operator-(const QRectF &lhs, const QMarginsF &rhs)
2418 \relates QRectF
2419 \since 5.3
2420
2421 Returns the \a lhs rectangle shrunk by the \a rhs margins.
2422*/
2423
2424/*!
2425 \fn QRectF operator+(const QMarginsF &lhs, const QRectF &rhs)
2426 \relates QRectF
2427 \overload
2428 \since 5.3
2429
2430 Returns the \a lhs rectangle grown by the \a rhs margins.
2431*/
2432
2433/*!
2434 \fn QRectF QRectF::marginsAdded(const QMarginsF &margins) const
2435 \since 5.3
2436
2437 Returns a rectangle grown by the \a margins.
2438
2439 \sa operator+=(), marginsRemoved(), operator-=()
2440*/
2441
2442/*!
2443 \fn QRectF QRectF::marginsRemoved(const QMarginsF &margins) const
2444 \since 5.3
2445
2446 Removes the \a margins from the rectangle, shrinking it.
2447
2448 \sa marginsAdded(), operator+=(), operator-=()
2449*/
2450
2451/*!
2452 \fn QRectF QRectF::operator+=(const QMarginsF &margins)
2453 \since 5.3
2454
2455 Adds the \a margins to the rectangle, growing it.
2456
2457 \sa marginsAdded(), marginsRemoved(), operator-=()
2458*/
2459
2460/*!
2461 \fn QRectF QRectF::operator-=(const QMarginsF &margins)
2462 \since 5.3
2463
2464 Returns a rectangle shrunk by the \a margins.
2465
2466 \sa marginsRemoved(), operator+=(), marginsAdded()
2467*/
2468
2469/*!
2470 \fn bool QRectF::qFuzzyCompare(const QRectF &lhs, const QRectF &rhs)
2471 \since 6.8
2472
2473 Returns \c true if the rectangle \a lhs is approximately equal to the
2474 rectangle \a rhs; otherwise returns \c false.
2475*/
2476
2477/*!
2478 \fn bool QRectF::qFuzzyIsNull(const QRectF &rect)
2479 \since 6.8
2480
2481 Returns \c true if both width and height of the rectangle \a rect are
2482 approximately equal to zero; otherwise returns \c false.
2483*/
2484
2485/*****************************************************************************
2486 QRectF stream functions
2487 *****************************************************************************/
2488#ifndef QT_NO_DATASTREAM
2489/*!
2490 \fn QDataStream &operator<<(QDataStream &stream, const QRectF &rectangle)
2491
2492 \relates QRectF
2493
2494 Writes the \a rectangle to the \a stream, and returns a reference to the
2495 stream.
2496
2497 \sa {Serializing Qt Data Types}
2498*/
2499
2500QDataStream &operator<<(QDataStream &s, const QRectF &r)
2501{
2502 s << double(r.x()) << double(r.y()) << double(r.width()) << double(r.height());
2503 return s;
2504}
2505
2506/*!
2507 \fn QDataStream &operator>>(QDataStream &stream, QRectF &rectangle)
2508
2509 \relates QRectF
2510
2511 Reads a \a rectangle from the \a stream, and returns a reference to the
2512 stream.
2513
2514 \sa {Serializing Qt Data Types}
2515*/
2516
2517QDataStream &operator>>(QDataStream &s, QRectF &r)
2518{
2519 double x, y, w, h;
2520 s >> x;
2521 s >> y;
2522 s >> w;
2523 s >> h;
2524 r.setRect(qreal(x), qreal(y), qreal(w), qreal(h));
2525 return s;
2526}
2527
2528#endif // QT_NO_DATASTREAM
2529
2530
2531#ifndef QT_NO_DEBUG_STREAM
2532QDebug operator<<(QDebug dbg, const QRectF &r)
2533{
2534 QDebugStateSaver saver(dbg);
2535 dbg.nospace();
2536 dbg << "QRectF" << '(';
2537 QtDebugUtils::formatQRect(dbg, r);
2538 dbg << ')';
2539 return dbg;
2540}
2541#endif
2542
2543QT_END_NAMESPACE
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDataStream & operator>>(QDataStream &s, QRectF &r)
Definition qrect.cpp:2517
QDebug operator<<(QDebug dbg, const QRectF &r)
Definition qrect.cpp:2532