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