QSizeF Class Reference

#include <qsize.h>

List of all members.


Detailed Description

The QSizeF class defines the size of a two-dimensional object using floating point precision.

A size is specified by a width() and a height(). It can be set in the constructor and changed using the setWidth(), setHeight(), or scale() functions, or using arithmetic operators. A size can also be manipulated directly by retrieving references to the width and height using the rwidth() and rheight() functions. Finally, the width and height can be swapped using the transpose() function.

The isValid() function determines if a size is valid. A valid size has both width and height greater than or equal to zero. The isEmpty() function returns true if either of the width and height is less than (or equal to) zero, while the isNull() function returns true only if both the width and the height is zero.

Use the expandedTo() function to retrieve a size which holds the maximum height and width of this size and a given size. Similarly, the boundedTo() function returns a size which holds the minimum height and width of this size and a given size.

The QSizeF class also provides the toSize() function returning a QSize copy of this size, constructed by rounding the width and height to the nearest integers.

QSizeF objects can be streamed as well as compared.

See also:
QSize, QPointF, QRectF

Definition at line 180 of file qsize.h.

Public Member Functions

 QSizeF ()
 QSizeF (const QSize &sz)
 QSizeF (qreal w, qreal h)
bool isNull () const
bool isEmpty () const
bool isValid () const
qreal width () const
qreal height () const
void setWidth (qreal w)
void setHeight (qreal h)
void transpose ()
void scale (qreal w, qreal h, Qt::AspectRatioMode mode)
void scale (const QSizeF &s, Qt::AspectRatioMode mode)
QSizeF expandedTo (const QSizeF &) const
QSizeF boundedTo (const QSizeF &) const
qreal & rwidth ()
qreal & rheight ()
QSizeFoperator+= (const QSizeF &)
QSizeFoperator-= (const QSizeF &)
QSizeFoperator *= (qreal c)
QSizeFoperator/= (qreal c)
QSize toSize () const

Private Attributes

qreal wd
qreal ht

Friends

bool operator== (const QSizeF &, const QSizeF &)
bool operator!= (const QSizeF &, const QSizeF &)
const QSizeF operator+ (const QSizeF &, const QSizeF &)
const QSizeF operator- (const QSizeF &, const QSizeF &)
const QSizeF operator * (const QSizeF &, qreal)
const QSizeF operator * (qreal, const QSizeF &)
const QSizeF operator/ (const QSizeF &, qreal)

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QSizeF &size)
QDataStreamoperator>> (QDataStream &stream, QSizeF &size)


Constructor & Destructor Documentation

QSizeF::QSizeF (  )  [inline]

Constructs an invalid size.

See also:
isValid()

Definition at line 240 of file qsize.h.

References ht, and wd.

Referenced by boundedTo(), expandedTo(), and scale().

00241 { wd = ht = -1.; }

QSizeF::QSizeF ( const QSize size  )  [inline]

Constructs a size with floating point accuracy from the given size.

See also:
toSize()

Definition at line 243 of file qsize.h.

00244     : wd(sz.width()), ht(sz.height())
00245 {
00246 }

QSizeF::QSizeF ( qreal  width,
qreal  height 
) [inline]

Constructs a size with the given width and height.

Definition at line 248 of file qsize.h.

References ht, and wd.

00249 { wd = w; ht = h; }


Member Function Documentation

bool QSizeF::isNull (  )  const [inline]

Returns true if both the width and height is 0; otherwise returns false.

See also:
isValid(), isEmpty()

Definition at line 251 of file qsize.h.

References ht, and wd.

Referenced by QTextDocumentLayout::documentChanged().

00252 { return qIsNull(wd) && qIsNull(ht); }

bool QSizeF::isEmpty (  )  const [inline]

Returns true if either of the width and height is less than or equal to 0; otherwise returns false.

See also:
isNull(), isValid()

Definition at line 254 of file qsize.h.

References ht, and wd.

Referenced by qDrawRoundedCorners().

00255 { return wd <= 0. || ht <= 0.; }

bool QSizeF::isValid (  )  const [inline]

Returns true if both the width and height is equal to or greater than 0; otherwise returns false.

See also:
isNull(), isEmpty()

Definition at line 257 of file qsize.h.

References ht, and wd.

00258 { return wd >= 0. && ht >= 0.; }

qreal QSizeF::width (  )  const [inline]

Returns the width.

See also:
height(), setWidth()

Definition at line 260 of file qsize.h.

References wd.

Referenced by QTextDocument::adjustSize(), QAbstractFormBuilder::createProperty(), DocumentShape::DocumentShape(), QItemDelegate::drawDisplay(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayoutPrivate::hitTest(), ImageShape::ImageShape(), QTextDocumentLayoutPrivate::layoutBlock(), QTextDocumentLayoutPrivate::layoutFlow(), QTextDocumentLayoutPrivate::layoutFrame(), operator<<(), ArthurFrame::paintDescription(), PreviewView::paintPage(), HoverPoints::pointBoundingRect(), QTextDocumentLayoutPrivate::positionFloat(), QTextDocument::print(), qDrawRoundedCorners(), QRectF::QRectF(), PreviewView::resizeEvent(), QTextDocumentLayout::resizeInlineObject(), PrintPreview::setup(), qdesigner_internal::SizeFProperty::setValue(), Launcher::showCategories(), Launcher::showExampleSummary(), TitleShape::TitleShape(), and Launcher::updateExampleSummary().

00261 { return wd; }

qreal QSizeF::height (  )  const [inline]

Returns the height.

See also:
width(), setHeight()

Definition at line 263 of file qsize.h.

References ht.

Referenced by QGraphicsTextItemPrivate::_q_updateBoundingRect(), QTextDocument::adjustSize(), TransEditor::calculateFieldHeight(), EditorPage::calculateFieldHeight(), QGraphicsTextItemPrivate::controlOffset(), QAbstractFormBuilder::createProperty(), DocumentShape::DocumentShape(), QItemDelegate::drawDisplay(), QTextDocumentLayoutPrivate::drawFrame(), QTextDocumentLayout::dynamicPageCount(), QTextDocumentLayoutPrivate::hitTest(), ImageShape::ImageShape(), QTextDocumentLayoutPrivate::layoutFlow(), QLabelPrivate::layoutRect(), operator<<(), PreviewView::paintEvent(), PreviewView::paintPage(), HoverPoints::pointBoundingRect(), QTextDocumentLayoutPrivate::positionFloat(), QTextDocument::print(), qDrawRoundedCorners(), QRectF::QRectF(), PreviewView::resizeEvent(), QTextDocumentLayout::resizeInlineObject(), PrintPreview::setup(), qdesigner_internal::SizeFProperty::setValue(), QGraphicsTextItemPrivate::textControl(), and TitleShape::TitleShape().

00264 { return ht; }

void QSizeF::setWidth ( qreal  width  )  [inline]

Sets the width to the given width.

See also:
width(), rwidth(), setHeight()

Definition at line 266 of file qsize.h.

References wd.

Referenced by operator>>(), QTextDocument::setTextWidth(), and PrintPreview::setup().

00267 { wd = w; }

void QSizeF::setHeight ( qreal  height  )  [inline]

Sets the height to the given height.

See also:
height(), rheight(), setWidth()

Definition at line 269 of file qsize.h.

References ht.

Referenced by operator>>(), QTextDocument::setTextWidth(), and PrintPreview::setup().

00270 { ht = h; }

void QSizeF::transpose (  ) 

Swaps the width and height values.

See also:
setWidth(), setHeight()

Definition at line 582 of file qsize.cpp.

References ht, and wd.

00583 {
00584     qreal tmp = wd;
00585     wd = ht;
00586     ht = tmp;
00587 }

void QSizeF::scale ( qreal  width,
qreal  height,
Qt::AspectRatioMode  mode 
) [inline]

Scales the size to a rectangle with the given width and height, according to the specified mode.

If mode is Qt::IgnoreAspectRatio, the size is set to (width, height). If mode is Qt::KeepAspectRatio, the current size is scaled to a rectangle as large as possible inside (width, height), preserving the aspect ratio. If mode is Qt::KeepAspectRatioByExpanding, the current size is scaled to a rectangle as small as possible outside (width, height), preserving the aspect ratio.

Example:

        QSizeF t1(10, 12);
        t1.scale(60, 60, Qt::IgnoreAspectRatio);
        // t1 is (60, 60)

        QSizeF t2(10, 12);
        t2.scale(60, 60, Qt::KeepAspectRatio);
        // t2 is (50, 60)

        QSizeF t3(10, 12);
        t3.scale(60, 60, Qt::KeepAspectRatioByExpanding);
        // t3 is (60, 72)

See also:
setWidth(), setHeight()

Definition at line 272 of file qsize.h.

References QSizeF().

00273 { scale(QSizeF(w, h), mode); }

Here is the call graph for this function:

void QSizeF::scale ( const QSizeF size,
Qt::AspectRatioMode  mode 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Scales the size to a rectangle with the given size, according to the specified mode.

Definition at line 628 of file qsize.cpp.

References ht, Qt::IgnoreAspectRatio, Qt::KeepAspectRatio, s, and wd.

00629 {
00630     if (mode == Qt::IgnoreAspectRatio) {
00631         wd = s.wd;
00632         ht = s.ht;
00633     } else {
00634         bool useHeight;
00635         qreal rw = s.ht * wd / ht;
00636 
00637         if (mode == Qt::KeepAspectRatio) {
00638             useHeight = (rw <= s.wd);
00639         } else { // mode == Qt::KeepAspectRatioByExpanding
00640             useHeight = (rw >= s.wd);
00641         }
00642 
00643         if (useHeight) {
00644             wd = rw;
00645             ht = s.ht;
00646         } else {
00647             ht = s.wd * ht / wd;
00648             wd = s.wd;
00649         }
00650     }
00651 }

QSizeF QSizeF::expandedTo ( const QSizeF otherSize  )  const [inline]

Returns a size holding the maximum width and height of this size and the given otherSize.

See also:
boundedTo(), scale()

Definition at line 321 of file qsize.h.

References ht, qMax(), QSizeF(), and wd.

00322 {
00323     return QSizeF(qMax(wd,otherSize.wd), qMax(ht,otherSize.ht));
00324 }

Here is the call graph for this function:

QSizeF QSizeF::boundedTo ( const QSizeF otherSize  )  const [inline]

Returns a size holding the minimum width and height of this size and the given otherSize.

See also:
expandedTo(), scale()

Definition at line 326 of file qsize.h.

References ht, qMin(), QSizeF(), and wd.

00327 {
00328     return QSizeF(qMin(wd,otherSize.wd), qMin(ht,otherSize.ht));
00329 }

Here is the call graph for this function:

qreal & QSizeF::rwidth (  )  [inline]

Returns a reference to the width.

Using a reference makes it possible to manipulate the width directly. For example:

        QSizeF size(100.3, 10);
        size.rwidth() += 20.5;

         // size becomes (120.8,10)

See also:
rheight(), setWidth()

Definition at line 275 of file qsize.h.

References wd.

Referenced by QTextDocument::print().

00276 { return wd; }

qreal & QSizeF::rheight (  )  [inline]

Returns a reference to the height.

Using a reference makes it possible to manipulate the height directly. For example:

        QSizeF size(100, 10.2);
        size.rheight() += 5.5;

        // size becomes (100,15.7)

See also:
rwidth(), setHeight()

Definition at line 278 of file qsize.h.

References ht.

Referenced by QTextDocument::print().

00279 { return ht; }

QSizeF & QSizeF::operator+= ( const QSizeF size  )  [inline]

Adds the given size to this size and returns a reference to this size. For example:

        QSizeF s( 3, 7);
        QSizeF r(-1, 4);
        s += r;

        // s becomes (2,11)

Definition at line 281 of file qsize.h.

References ht, s, and wd.

00282 { wd += s.wd; ht += s.ht; return *this; }

QSizeF & QSizeF::operator-= ( const QSizeF size  )  [inline]

Subtracts the given size from this size and returns a reference to this size. For example:

        QSizeF s( 3, 7);
        QSizeF r(-1, 4);
        s -= r;

        // s becomes (4,3)

Definition at line 284 of file qsize.h.

References ht, s, and wd.

00285 { wd -= s.wd; ht -= s.ht; return *this; }

QSizeF & QSizeF::operator *= ( qreal  factor  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Multiplies both the width and height by the given factor and returns a reference to the size.

See also:
scale()

Definition at line 287 of file qsize.h.

References ht, and wd.

00288 { wd *= c; ht *= c; return *this; }

QSizeF & QSizeF::operator/= ( qreal  divisor  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Divides both the width and height by the given divisor and returns a reference to the size.

See also:
scale()

Definition at line 308 of file qsize.h.

References ht, and wd.

00309 {
00310     Q_ASSERT(!qFuzzyCompare(c, 0));
00311     wd = wd/c; ht = ht/c;
00312     return *this;
00313 }

QSize QSizeF::toSize (  )  const [inline]

Returns an integer based copy of this size.

Note that the coordinates in the returned size will be rounded to the nearest integer.

See also:
QSizeF()

Definition at line 331 of file qsize.h.

References ht, qRound(), and wd.

Referenced by QWhatsThat::QWhatsThat(), QTextEditPrivate::relayoutDocument(), QTextDocumentLayout::resizeInlineObject(), and QLabelPrivate::sizeForWidth().

00332 {
00333     return QSize(qRound(wd), qRound(ht));
00334 }

Here is the call graph for this function:


Friends And Related Function Documentation

bool operator== ( const QSizeF s1,
const QSizeF s2 
) [friend]

Returns true if s1 and s2 are equal; otherwise returns false.

Definition at line 290 of file qsize.h.

00291 { return qFuzzyCompare(s1.wd, s2.wd) && qFuzzyCompare(s1.ht, s2.ht); }

bool operator!= ( const QSizeF s1,
const QSizeF s2 
) [friend]

Returns true if s1 and s2 are different; otherwise returns false.

Definition at line 293 of file qsize.h.

00294 { return !qFuzzyCompare(s1.wd, s2.wd) || !qFuzzyCompare(s1.ht, s2.ht); }

const QSizeF operator+ ( const QSizeF s1,
const QSizeF s2 
) [friend]

Returns the sum of s1 and s2; each component is added separately.

Definition at line 296 of file qsize.h.

00297 { return QSizeF(s1.wd+s2.wd, s1.ht+s2.ht); }

const QSizeF operator- ( const QSizeF s1,
const QSizeF s2 
) [friend]

Returns s2 subtracted from s1; each component is subtracted separately.

Definition at line 299 of file qsize.h.

00300 { return QSizeF(s1.wd-s2.wd, s1.ht-s2.ht); }

const QSizeF operator * ( const QSizeF size,
qreal  factor 
) [friend]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Multiplies the given size by the given factor and returns the result.

See also:
QSizeF::scale()

Definition at line 302 of file qsize.h.

00303 { return QSizeF(s.wd*c, s.ht*c); }

const QSizeF operator * ( qreal  factor,
const QSizeF size 
) [friend]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Multiplies the given size by the given factor and returns the result.

Definition at line 305 of file qsize.h.

00306 { return QSizeF(s.wd*c, s.ht*c); }

const QSizeF operator/ ( const QSizeF size,
qreal  divisor 
) [friend]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Divides the given size by the given divisor and returns the result.

See also:
QSizeF::scale()

Definition at line 315 of file qsize.h.

00316 {
00317     Q_ASSERT(!qFuzzyCompare(c, 0));
00318     return QSizeF(s.wd/c, s.ht/c);
00319 }

QDataStream & operator<< ( QDataStream stream,
const QSizeF size 
) [related]

Writes the the given size to the given stream and returns a reference to the stream.

See also:
{Format of the QDataStream Operators}

Definition at line 838 of file qsize.cpp.

00839 {
00840     s << double(sz.width()) << double(sz.height());
00841     return s;
00842 }

QDataStream & operator>> ( QDataStream stream,
QSizeF size 
) [related]

Reads a size from the given stream into the given size and returns a reference to the stream.

See also:
{Format of the QDataStream Operators}

Definition at line 854 of file qsize.cpp.

00855 {
00856     double w, h;
00857     s >> w;
00858     s >> h;
00859     sz.setWidth(qreal(w));
00860     sz.setHeight(qreal(h));
00861     return s;
00862 }


Member Data Documentation

qreal QSizeF::wd [private]

Definition at line 222 of file qsize.h.

Referenced by boundedTo(), expandedTo(), isEmpty(), isNull(), isValid(), operator *=(), operator+=(), operator-=(), operator/=(), QSizeF(), rwidth(), scale(), setWidth(), toSize(), transpose(), and width().

qreal QSizeF::ht [private]

Definition at line 223 of file qsize.h.

Referenced by boundedTo(), expandedTo(), height(), isEmpty(), isNull(), isValid(), operator *=(), operator+=(), operator-=(), operator/=(), QSizeF(), rheight(), scale(), setHeight(), toSize(), and transpose().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:55:08 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1