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
qsize.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 "qsize.h"
6#include "qdatastream.h"
7
8#include <private/qdebug_p.h>
9
11
12/*!
13 \class QSize
14 \inmodule QtCore
15 \ingroup painting
16
17 \brief The QSize class defines the size of a two-dimensional
18 object using integer point precision.
19
20 A size is specified by a width() and a height(). It can be set in
21 the constructor and changed using the setWidth(), setHeight(), or
22 scale() functions, or using arithmetic operators. A size can also
23 be manipulated directly by retrieving references to the width and
24 height using the rwidth() and rheight() functions. Finally, the
25 width and height can be swapped using the transpose() function.
26
27 The isValid() function determines if a size is valid (a valid size
28 has both width and height greater than or equal to zero). The isEmpty()
29 function returns \c true if either of the width and height is less
30 than, or equal to, zero, while the isNull() function returns \c true
31 only if both the width and the height is zero.
32
33 Use the expandedTo() function to retrieve a size which holds the
34 maximum height and width of \e this size and a given
35 size. Similarly, the boundedTo() function returns a size which
36 holds the minimum height and width of \e this size and a given
37 size.
38
39 QSize objects can be streamed as well as compared.
40
41 \sa QSizeF, QPoint, QRect
42*/
43
44
45/*****************************************************************************
46 QSize member functions
47 *****************************************************************************/
48
49/*!
50 \fn QSize::QSize()
51
52 Constructs a size with an invalid width and height (i.e., isValid()
53 returns \c false).
54
55 \sa isValid()
56*/
57
58/*!
59 \fn QSize::QSize(int width, int height)
60
61 Constructs a size with the given \a width and \a height.
62
63 \sa setWidth(), setHeight()
64*/
65
66/*!
67 \fn bool QSize::isNull() const
68
69 Returns \c true if both the width and height is 0; otherwise returns
70 false.
71
72 \sa isValid(), isEmpty()
73*/
74
75/*!
76 \fn bool QSize::isEmpty() const
77
78 Returns \c true if either of the width and height is less than or
79 equal to 0; otherwise returns \c false.
80
81 \sa isNull(), isValid()
82*/
83
84/*!
85 \fn bool QSize::isValid() const
86
87 Returns \c true if both the width and height is equal to or greater
88 than 0; otherwise returns \c false.
89
90 \sa isNull(), isEmpty()
91*/
92
93/*!
94 \fn int QSize::width() const
95
96 Returns the width.
97
98 \sa height(), setWidth()
99*/
100
101/*!
102 \fn int QSize::height() const
103
104 Returns the height.
105
106 \sa width(), setHeight()
107*/
108
109/*!
110 \fn void QSize::setWidth(int width)
111
112 Sets the width to the given \a width.
113
114 \sa rwidth(), width(), setHeight()
115*/
116
117/*!
118 \fn void QSize::setHeight(int height)
119
120 Sets the height to the given \a height.
121
122 \sa rheight(), height(), setWidth()
123*/
124
125/*!
126 Swaps the width and height values.
127
128 \sa setWidth(), setHeight(), transposed()
129*/
130
131void QSize::transpose() noexcept
132{
133 qSwap(wd, ht);
134}
135
136/*!
137 \fn QSize QSize::transposed() const
138 \since 5.0
139
140 Returns a QSize with width and height swapped.
141
142 \sa transpose()
143*/
144
145/*!
146 \fn void QSize::scale(int width, int height, Qt::AspectRatioMode mode)
147
148 Scales the size to a rectangle with the given \a width and \a
149 height, according to the specified \a mode:
150
151 \list
152 \li If \a mode is Qt::IgnoreAspectRatio, the size is set to (\a width, \a height).
153 \li If \a mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle
154 as large as possible inside (\a width, \a height), preserving the aspect ratio.
155 \li If \a mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
156 as small as possible outside (\a width, \a height), preserving the aspect ratio.
157 \endlist
158
159 Example:
160 \snippet code/src_corelib_tools_qsize.cpp 0
161
162 \sa setWidth(), setHeight(), scaled()
163*/
164
165/*!
166 \fn void QSize::scale(const QSize &size, Qt::AspectRatioMode mode)
167 \overload
168
169 Scales the size to a rectangle with the given \a size, according to
170 the specified \a mode.
171*/
172
173/*!
174 \fn QSize QSize::scaled(int width, int height, Qt::AspectRatioMode mode) const
175 \since 5.0
176
177 Return a size scaled to a rectangle with the given \a width and \a
178 height, according to the specified \a mode.
179
180 \sa scale()
181*/
182
183/*!
184 \overload
185 \since 5.0
186
187 Return a size scaled to a rectangle with the given size \a s,
188 according to the specified \a mode.
189*/
190QSize QSize::scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept
191{
192 if (mode == Qt::IgnoreAspectRatio || wd == 0 || ht == 0) {
193 return s;
194 } else {
195 bool useHeight;
196 qint64 rw = qint64(s.height()) * qint64(width()) / qint64(height());
197
198 if (mode == Qt::KeepAspectRatio) {
199 useHeight = (rw <= s.width());
200 } else { // mode == Qt::KeepAspectRatioByExpanding
201 useHeight = (rw >= s.width());
202 }
203
204 if (useHeight) {
205 return QSize(int(rw), s.height());
206 } else {
207 return QSize(s.width(),
208 qint32(qint64(s.width()) * qint64(height()) / qint64(width())));
209 }
210 }
211}
212
213/*!
214 \fn int &QSize::rwidth()
215
216 Returns a reference to the width.
217
218 Using a reference makes it possible to manipulate the width
219 directly. For example:
220
221 \snippet code/src_corelib_tools_qsize.cpp 1
222
223 \sa rheight(), setWidth()
224*/
225
226/*!
227 \fn int &QSize::rheight()
228
229 Returns a reference to the height.
230
231 Using a reference makes it possible to manipulate the height
232 directly. For example:
233
234 \snippet code/src_corelib_tools_qsize.cpp 2
235
236 \sa rwidth(), setHeight()
237*/
238
239/*!
240 \fn QSize &QSize::operator+=(const QSize &size)
241
242 Adds the given \a size to \e this size, and returns a reference to
243 this size. For example:
244
245 \snippet code/src_corelib_tools_qsize.cpp 3
246*/
247
248/*!
249 \fn QSize &QSize::operator-=(const QSize &size)
250
251 Subtracts the given \a size from \e this size, and returns a
252 reference to this size. For example:
253
254 \snippet code/src_corelib_tools_qsize.cpp 4
255*/
256
257/*!
258 \fn QSize &QSize::operator*=(qreal factor)
259 \overload
260
261 Multiplies both the width and height by the given \a factor, and
262 returns a reference to the size.
263
264 Note that the result is rounded to the nearest integer.
265
266 \sa scale()
267*/
268
269/*!
270 \fn bool QSize::operator==(const QSize &lhs, const QSize &rhs)
271
272 Returns \c true if \a lhs and \a rhs are equal; otherwise returns \c false.
273*/
274
275/*!
276 \fn bool QSize::operator!=(const QSize &lhs, const QSize &rhs)
277
278 Returns \c true if \a lhs and \a rhs are different; otherwise returns \c false.
279*/
280
281/*!
282 \fn QSize QSize::operator+(const QSize &s1, const QSize &s2)
283
284 Returns the sum of \a s1 and \a s2; each component is added separately.
285*/
286
287/*!
288 \fn QSize QSize::operator-(const QSize &s1, const QSize &s2)
289
290 Returns \a s2 subtracted from \a s1; each component is subtracted
291 separately.
292*/
293
294/*!
295 \fn QSize QSize::operator*(const QSize &size, qreal factor)
296
297 Multiplies the given \a size by the given \a factor, and returns
298 the result rounded to the nearest integer.
299
300 \sa QSize::scale()
301*/
302
303/*!
304 \fn QSize QSize::operator*(qreal factor, const QSize &size)
305 \overload
306
307 Multiplies the given \a size by the given \a factor, and returns
308 the result rounded to the nearest integer.
309*/
310
311/*!
312 \fn QSize &QSize::operator/=(qreal divisor)
313 \overload
314
315 Divides both the width and height by the given \a divisor, and
316 returns a reference to the size.
317
318 Note that the result is rounded to the nearest integer.
319
320 \sa QSize::scale()
321*/
322
323/*!
324 \fn QSize QSize::operator/(const QSize &size, qreal divisor)
325 \overload
326
327 Divides the given \a size by the given \a divisor, and returns the
328 result rounded to the nearest integer.
329
330 \sa QSize::scale()
331*/
332
333/*!
334 \fn QSize QSize::expandedTo(const QSize & otherSize) const
335
336 Returns a size holding the maximum width and height of this size
337 and the given \a otherSize.
338
339 \sa boundedTo(), scale()
340*/
341
342/*!
343 \fn QSize QSize::boundedTo(const QSize & otherSize) const
344
345 Returns a size holding the minimum width and height of this size
346 and the given \a otherSize.
347
348 \sa expandedTo(), scale()
349*/
350
351/*!
352 \fn QSize QSize::grownBy(QMargins margins) const
353 \fn QSizeF QSizeF::grownBy(QMarginsF margins) const
354 \since 5.14
355
356 Returns the size that results from growing this size by \a margins.
357
358 \sa shrunkBy()
359*/
360
361/*!
362 \fn QSize QSize::shrunkBy(QMargins margins) const
363 \fn QSizeF QSizeF::shrunkBy(QMarginsF margins) const
364 \since 5.14
365
366 Returns the size that results from shrinking this size by \a margins.
367
368 \sa grownBy()
369*/
370
371/*!
372 \fn QSize::toSizeF() const
373 \since 6.4
374
375 Returns this size as a size with floating point accuracy.
376
377 \sa QSizeF::toSize()
378*/
379
380/*****************************************************************************
381 QSize stream functions
382 *****************************************************************************/
383#ifndef QT_NO_DATASTREAM
384/*!
385 \fn QDataStream &operator<<(QDataStream &stream, const QSize &size)
386 \relates QSize
387
388 Writes the given \a size to the given \a stream, and returns a
389 reference to the stream.
390
391 \sa {Serializing Qt Data Types}
392*/
393
394QDataStream &operator<<(QDataStream &s, const QSize &sz)
395{
396 if (s.version() == 1)
397 s << (qint16)sz.width() << (qint16)sz.height();
398 else
399 s << (qint32)sz.width() << (qint32)sz.height();
400 return s;
401}
402
403/*!
404 \fn QDataStream &operator>>(QDataStream &stream, QSize &size)
405 \relates QSize
406
407 Reads a size from the given \a stream into the given \a size, and
408 returns a reference to the stream.
409
410 \sa {Serializing Qt Data Types}
411*/
412
413QDataStream &operator>>(QDataStream &s, QSize &sz)
414{
415 if (s.version() == 1) {
416 qint16 w, h;
417 s >> w; sz.rwidth() = w;
418 s >> h; sz.rheight() = h;
419 }
420 else {
421 qint32 w, h;
422 s >> w; sz.rwidth() = w;
423 s >> h; sz.rheight() = h;
424 }
425 return s;
426}
427#endif // QT_NO_DATASTREAM
428
429#ifndef QT_NO_DEBUG_STREAM
430QDebug operator<<(QDebug dbg, const QSize &s)
431{
432 QDebugStateSaver saver(dbg);
433 dbg.nospace();
434 dbg << "QSize(";
435 QtDebugUtils::formatQSize(dbg, s);
436 dbg << ')';
437 return dbg;
438}
439#endif
440
441
442
443/*!
444 \class QSizeF
445 \inmodule QtCore
446 \brief The QSizeF class defines the size of a two-dimensional object
447 using floating point precision.
448
449 \ingroup painting
450
451 A size is specified by a width() and a height(). It can be set in
452 the constructor and changed using the setWidth(), setHeight(), or
453 scale() functions, or using arithmetic operators. A size can also
454 be manipulated directly by retrieving references to the width and
455 height using the rwidth() and rheight() functions. Finally, the
456 width and height can be swapped using the transpose() function.
457
458 The isValid() function determines if a size is valid. A valid size
459 has both width and height greater than or equal to zero. The
460 isEmpty() function returns \c true if either of the width and height
461 is \e less than (or equal to) zero, while the isNull() function
462 returns \c true only if both the width and the height is zero.
463
464 Use the expandedTo() function to retrieve a size which holds the
465 maximum height and width of this size and a given
466 size. Similarly, the boundedTo() function returns a size which
467 holds the minimum height and width of this size and a given size.
468
469 The QSizeF class also provides the toSize() function returning a
470 QSize copy of this size, constructed by rounding the width and
471 height to the nearest integers.
472
473 QSizeF objects can be streamed as well as compared.
474
475 \sa QSize, QPointF, QRectF
476*/
477
478
479/*****************************************************************************
480 QSizeF member functions
481 *****************************************************************************/
482
483/*!
484 \fn QSizeF::QSizeF()
485
486 Constructs an invalid size.
487
488 \sa isValid()
489*/
490
491/*!
492 \fn QSizeF::QSizeF(const QSize &size)
493
494 Constructs a size with floating point accuracy from the given \a
495 size.
496
497 \sa toSize(), QSize::toSizeF()
498*/
499
500/*!
501 \fn QSizeF::QSizeF(qreal width, qreal height)
502
503 Constructs a size with the given finite \a width and \a height.
504*/
505
506/*!
507 \fn bool QSizeF::isNull() const
508
509 Returns \c true if both the width and height are 0.0 (ignoring the sign);
510 otherwise returns \c false.
511
512 \sa isValid(), isEmpty()
513*/
514
515/*!
516 \fn bool QSizeF::isEmpty() const
517
518 Returns \c true if either of the width and height is less than or
519 equal to 0; otherwise returns \c false.
520
521 \sa isNull(), isValid()
522*/
523
524/*!
525 \fn bool QSizeF::isValid() const
526
527 Returns \c true if both the width and height are equal to or greater
528 than 0; otherwise returns \c false.
529
530 \sa isNull(), isEmpty()
531*/
532
533/*!
534 \fn int QSizeF::width() const
535
536 Returns the width.
537
538 \sa height(), setWidth()
539*/
540
541/*!
542 \fn int QSizeF::height() const
543
544 Returns the height.
545
546 \sa width(), setHeight()
547*/
548
549/*!
550 \fn void QSizeF::setWidth(qreal width)
551
552 Sets the width to the given finite \a width.
553
554 \sa width(), rwidth(), setHeight()
555*/
556
557/*!
558 \fn void QSizeF::setHeight(qreal height)
559
560 Sets the height to the given finite \a height.
561
562 \sa height(), rheight(), setWidth()
563*/
564
565/*!
566 \fn QSize QSizeF::toSize() const
567
568 Returns an integer based copy of this size.
569
570 Note that the coordinates in the returned size will be rounded to
571 the nearest integer.
572
573 \sa QSizeF(), QSize::toSizeF()
574*/
575
576/*!
577 Swaps the width and height values.
578
579 \sa setWidth(), setHeight(), transposed()
580*/
581
582void QSizeF::transpose() noexcept
583{
584 qSwap(wd, ht);
585}
586
587/*!
588 \fn QSizeF QSizeF::transposed() const
589 \since 5.0
590
591 Returns the size with width and height values swapped.
592
593 \sa transpose()
594*/
595
596/*!
597 \fn void QSizeF::scale(qreal width, qreal height, Qt::AspectRatioMode mode)
598
599 Scales the size to a rectangle with the given \a width and \a
600 height, according to the specified \a mode.
601
602 \list
603 \li If \a mode is Qt::IgnoreAspectRatio, the size is set to (\a width, \a height).
604 \li If \a mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle
605 as large as possible inside (\a width, \a height), preserving the aspect ratio.
606 \li If \a mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle
607 as small as possible outside (\a width, \a height), preserving the aspect ratio.
608 \endlist
609
610 Example:
611 \snippet code/src_corelib_tools_qsize.cpp 5
612
613 \sa setWidth(), setHeight(), scaled()
614*/
615
616/*!
617 \fn void QSizeF::scale(const QSizeF &size, Qt::AspectRatioMode mode)
618 \overload
619
620 Scales the size to a rectangle with the given \a size, according to
621 the specified \a mode.
622*/
623
624/*!
625 \fn QSizeF QSizeF::scaled(qreal width, qreal height, Qt::AspectRatioMode mode) const
626 \since 5.0
627
628 Returns a size scaled to a rectangle with the given \a width and
629 \a height, according to the specified \a mode.
630
631 \sa scale()
632*/
633
634/*!
635 \overload
636 \since 5.0
637
638 Returns a size scaled to a rectangle with the given size \a s,
639 according to the specified \a mode.
640*/
641QSizeF QSizeF::scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept
642{
643 if (mode == Qt::IgnoreAspectRatio || qIsNull(wd) || qIsNull(ht)) {
644 return s;
645 } else {
646 bool useHeight;
647 qreal rw = s.ht * wd / ht;
648
649 if (mode == Qt::KeepAspectRatio) {
650 useHeight = (rw <= s.wd);
651 } else { // mode == Qt::KeepAspectRatioByExpanding
652 useHeight = (rw >= s.wd);
653 }
654
655 if (useHeight) {
656 return QSizeF(rw, s.ht);
657 } else {
658 return QSizeF(s.wd, s.wd * ht / wd);
659 }
660 }
661}
662
663/*!
664 \fn int &QSizeF::rwidth()
665
666 Returns a reference to the width.
667
668 Using a reference makes it possible to manipulate the width
669 directly. For example:
670
671 \snippet code/src_corelib_tools_qsize.cpp 6
672
673 \sa rheight(), setWidth()
674*/
675
676/*!
677 \fn int &QSizeF::rheight()
678
679 Returns a reference to the height.
680
681 Using a reference makes it possible to manipulate the height
682 directly. For example:
683
684 \snippet code/src_corelib_tools_qsize.cpp 7
685
686 \sa rwidth(), setHeight()
687*/
688
689/*!
690 \fn QSizeF &QSizeF::operator+=(const QSizeF &size)
691
692 Adds the given \a size to this size and returns a reference to
693 this size. For example:
694
695 \snippet code/src_corelib_tools_qsize.cpp 8
696*/
697
698/*!
699 \fn QSizeF &QSizeF::operator-=(const QSizeF &size)
700
701 Subtracts the given \a size from this size and returns a reference
702 to this size. For example:
703
704 \snippet code/src_corelib_tools_qsize.cpp 9
705*/
706
707/*!
708 \fn QSizeF &QSizeF::operator*=(qreal factor)
709 \overload
710
711 Multiplies both the width and height by the given finite \a factor and
712 returns a reference to the size.
713
714 \sa scale()
715*/
716
717/*!
718 \fn bool QSizeF::operator==(const QSizeF &lhs, const QSizeF &rhs)
719
720 Returns \c true if \a lhs and \a rhs are approximately equal; otherwise
721 returns false.
722
723 \warning This function does not check for strict equality; instead,
724 it uses a fuzzy comparison to compare the sizes' extents.
725
726 \sa qFuzzyCompare
727*/
728
729/*!
730 \fn bool QSizeF::operator!=(const QSizeF &lhs, const QSizeF &rhs)
731
732 Returns \c true if \a lhs and \a rhs are sufficiently different; otherwise
733 returns \c false.
734
735 \warning This function does not check for strict inequality; instead,
736 it uses a fuzzy comparison to compare the sizes' extents.
737*/
738
739/*!
740 \fn QSizeF QSizeF::operator+(const QSizeF &s1, const QSizeF &s2)
741
742 Returns the sum of \a s1 and \a s2; each component is added separately.
743*/
744
745/*!
746 \fn QSizeF QSizeF::operator-(const QSizeF &s1, const QSizeF &s2)
747
748 Returns \a s2 subtracted from \a s1; each component is subtracted
749 separately.
750*/
751
752/*!
753 \fn QSizeF QSizeF::operator*(const QSizeF &size, qreal factor)
754
755 \overload
756
757 Multiplies the given \a size by the given finite \a factor and returns
758 the result.
759
760 \sa QSizeF::scale()
761*/
762
763/*!
764 \fn QSizeF QSizeF::operator*(qreal factor, const QSizeF &size)
765
766 \overload
767
768 Multiplies the given \a size by the given finite \a factor and returns
769 the result.
770*/
771
772/*!
773 \fn QSizeF &QSizeF::operator/=(qreal divisor)
774
775 \overload
776
777 Divides both the width and height by the given \a divisor and returns a
778 reference to the size. The \a divisor must not be either zero or NaN.
779
780 \sa scale()
781*/
782
783/*!
784 \fn QSizeF QSizeF::operator/(const QSizeF &size, qreal divisor)
785
786 \overload
787
788 Divides the given \a size by the given \a divisor and returns the
789 result. The \a divisor must not be either zero or NaN.
790
791 \sa QSizeF::scale()
792*/
793
794/*!
795 \fn QSizeF QSizeF::expandedTo(const QSizeF & otherSize) const
796
797 Returns a size holding the maximum width and height of this size
798 and the given \a otherSize.
799
800 \sa boundedTo(), scale()
801*/
802
803/*!
804 \fn QSizeF QSizeF::boundedTo(const QSizeF & otherSize) const
805
806 Returns a size holding the minimum width and height of this size
807 and the given \a otherSize.
808
809 \sa expandedTo(), scale()
810*/
811
812/*!
813 \fn bool QSizeF::qFuzzyCompare(const QSizeF &lhs, const QSizeF &rhs)
814 \since 6.8
815
816 Returns \c true if the size \a lhs is approximately equal to the
817 size \a rhs; otherwise returns \c false.
818
819 The sizes are considered approximately equal if their width and
820 height are approximately equal.
821*/
822
823/*!
824 \fn bool QSizeF::qFuzzyIsNull(const QSizeF &size)
825 \since 6.8
826
827 Returns \c true if both width and height of the size \a size
828 are approximately equal to zero.
829*/
830
831/*****************************************************************************
832 QSizeF stream functions
833 *****************************************************************************/
834#ifndef QT_NO_DATASTREAM
835/*!
836 \fn QDataStream &operator<<(QDataStream &stream, const QSizeF &size)
837 \relates QSizeF
838
839 Writes the given \a size to the given \a stream and returns a
840 reference to the stream.
841
842 \sa {Serializing Qt Data Types}
843*/
844
845QDataStream &operator<<(QDataStream &s, const QSizeF &sz)
846{
847 s << double(sz.width()) << double(sz.height());
848 return s;
849}
850
851/*!
852 \fn QDataStream &operator>>(QDataStream &stream, QSizeF &size)
853 \relates QSizeF
854
855 Reads a size from the given \a stream into the given \a size and
856 returns a reference to the stream.
857
858 \sa {Serializing Qt Data Types}
859*/
860
861QDataStream &operator>>(QDataStream &s, QSizeF &sz)
862{
863 double w, h;
864 s >> w;
865 s >> h;
866 sz.setWidth(qreal(w));
867 sz.setHeight(qreal(h));
868 return s;
869}
870#endif // QT_NO_DATASTREAM
871
872#ifndef QT_NO_DEBUG_STREAM
873QDebug operator<<(QDebug dbg, const QSizeF &s)
874{
875 QDebugStateSaver saver(dbg);
876 dbg.nospace();
877 dbg << "QSizeF(";
878 QtDebugUtils::formatQSize(dbg, s);
879 dbg << ')';
880 return dbg;
881}
882#endif
883
884QT_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, QSizeF &sz)
Definition qsize.cpp:861
QDebug operator<<(QDebug dbg, const QSizeF &s)
Definition qsize.cpp:873