Qt 4.0: QRectF Class Reference

|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QRectF Class Reference
[QtCore module]

The QRectF class defines a rectangle in the plane using floating point coordinates for accuracy. More...

    #include <QRectF>

- List of all members, including inherited members

### Public Functions

- QRectF ()
- QRectF ( const QPointF & topLeft, const QSizeF & size )
- QRectF ( qreal x, qreal y, qreal width, qreal height )
- QRectF ( const QRect & rect )
- void adjust ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 )
- QRectF adjusted ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 ) const
- qreal bottom () const
- QPointF bottomLeft () const
- QPointF bottomRight () const
- QPointF center () const
- bool contains ( const QPointF & point ) const
- bool contains ( qreal x, qreal y ) const
- bool contains ( const QRectF & rectangle ) const
- void getCoords ( qreal * xp1, qreal * yp1, qreal * xp2, qreal * yp2 ) const
- void getRect ( qreal * x, qreal * y, qreal * w, qreal * h ) const
- qreal height () const
- QRectF intersect ( const QRectF & other ) const
- bool intersects ( const QRectF & rectangle ) const
- bool isEmpty () const
- bool isNull () const
- bool isValid () const
- qreal left () const
- void moveBottom ( qreal pos )
- void moveBottomLeft ( const QPointF & p )
- void moveBottomRight ( const QPointF & p )
- void moveCenter ( const QPointF & pos )
- void moveLeft ( qreal pos )
- void moveRight ( qreal pos )
- void moveTo ( qreal x, qreal y )
- void moveTo ( const QPointF & pt )
- void moveTop ( qreal pos )
- void moveTopLeft ( const QPointF & p )
- void moveTopRight ( const QPointF & p )
- QRectF normalized () const
- qreal right () const
- void setBottom ( qreal pos )
- void setBottomLeft ( const QPointF & p )
- void setBottomRight ( const QPointF & p )
- void setCoords ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 )
- void setHeight ( qreal height )
- void setLeft ( qreal pos )
- void setRect ( qreal x, qreal y, qreal width, qreal height )
- void setRight ( qreal pos )
- void setSize ( const QSizeF & size )
- void setTop ( qreal pos )
- void setTopLeft ( const QPointF & p )
- void setTopRight ( const QPointF & p )
- void setWidth ( qreal width )
- void setX ( qreal x )
- void setY ( qreal y )
- QSizeF size () const
- QRect toRect () const
- qreal top () const
- QPointF topLeft () const
- QPointF topRight () const
- void translate ( qreal dx, qreal dy )
- void translate ( const QPointF & p )
- QRectF translated ( qreal dx, qreal dy ) const
- QRectF translated ( const QPointF & p ) const
- QRectF unite ( const QRectF & other ) const
- qreal width () const
- qreal x () const
- qreal y () const
- QRectF operator& ( const QRectF & other ) const
- QRectF & operator&= ( const QRectF & other )
- QRectF operator| ( const QRectF & other ) const
- QRectF & operator|= ( const QRectF & other )

### Related Non-Members

- bool operator!= ( const QRectF & r1, const QRectF & r2 )
- QDataStream & operator<< ( QDataStream & stream, const QRectF & rectangle )
- bool operator== ( const QRectF & r1, const QRectF & r2 )
- QDataStream & operator>> ( QDataStream & stream, QRectF & rectangle )

## Detailed Description

The QRectF class defines a rectangle in the plane using floating point coordinates for accuracy.

A rectangle is normally expressed as an upper-left corner and a size. QRectF provides a variant of the QRect class that defines the position and dimension of the rectangle using `qreal` values for accuracy.

The default coordinate system has origin (0, 0) in the top-left corner. The positive direction of the y axis is down, and the positive x axis is from left to right.

The size (width and height) of a QRectF is always equivalent to the mathematical rectangle that form the basis for its rendering:

If you use an anti-aliased painter the boundary line of a QRectF will be rendered symetrically on both sides of the mathematical rectangle's boundary line. But when using an aliased painter (the default) other rules apply.

Then, when rendering with a one pixel wide pen the QRectF's boundary line will be rendered to the right and below the mathematical rectangle's boundary line.

When rendering with a two pixel wide pen the boundary line will be split in the middle by the mathematical rectangle. This will be the case whenever the pen has an even number of pixels, while rendering with a pen with an odd number of pixels, the spare pixel will be rendered to the right and below the mathematical rectangle as in the one pixel case.

One pixel wide pen | Two pixel wide pen | Three pixel wide pen

A QRectF can be constructed with a set of qreals specifying the coordinates of the top-left corner of the rectangle and its dimensions or from a QPointF and a QSizeF. The following code creates two identical rectangles.

        QRectF r1(100, 200, 11, 16);
        QRectF r2(QPoint(100,200), QSize(11,16));

There is also a third constructor creating a QRectF from a QRect.

After creation, the dimensions can be changed with setLeft(), setRight(), setTop(), and setBottom(), or by setting sizes with setWidth(), setHeight(), and setSize(). The dimensions can also be changed with the move functions, such as moveBy(), moveCenter(), and moveBottomRight(). You can also add coordinates to a rectangle with adjust(). You can test to see if a rectangle contains a specific point with the contains() function, and you can test to see if two rectangles intersect with the intersects() function.

You can also retrieve the intersection as a QRect using intersect():

And finally you can retrieve the bounding rectangle of two QRects using unite():

See also QPointF, QSizeF, QPolygonF, and QRect.

## Member Function Documentation

### QRectF::QRectF ()

Constructs a null rectangle.

See also isNull().

### QRectF::QRectF ( const QPointF & topLeft, const QSizeF & size )

Constructs a rectangle with topLeft as the top-left corner and size as the rectangle size.

### QRectF::QRectF ( qreal x, qreal y, qreal width, qreal height )

Constructs a rectangle with the top-left corner at (x, y) and dimensions specified by the width and height.

### QRectF::QRectF ( const QRect & rect )

Constructs a QRectF with from the given rect.

### void QRectF::adjust ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 )

Adds xp1, yp1, xp2 and yp2 respectively to the existing coordinates of the rectangle.

See also adjusted().

### QRectF QRectF::adjusted ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 ) const

Returns a new rectangle with (xp1, yp1) added to the existing position of the rectangle's top-left corner, and (xp2, yp2) added to the position of its bottom-right corner.

See also adjust().

### qreal QRectF::bottom () const

Returns the bottom coordinate of the rectangle.

See also setBottom(), top(), bottomLeft(), and bottomRight().

### QPointF QRectF::bottomLeft () const

Returns the bottom-left position of the rectangle.

See also setBottomLeft(), moveBottomLeft(), topRight(), bottom(), and left().

### QPointF QRectF::bottomRight () const

Returns the bottom-right position of the rectangle.

See also setBottomRight(), moveBottomRight(), topLeft(), right(), and bottom().

### QPointF QRectF::center () const

Returns the center point of the rectangle.

See also moveCenter(), topLeft(), bottomRight(), topRight(), and bottomLeft().

### bool QRectF::contains ( const QPointF & point ) const

Returns true if the given point is inside or on the edge of the rectangle; otherwise returns false.

See also unite(), intersect(), and intersects().

### bool QRectF::contains ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns true if the point (x, y) is inside this rectangle; otherwise returns false.

See also unite(), intersect(), and intersects().

### bool QRectF::contains ( const QRectF & rectangle ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns true if the given rectangle is inside this rectangle; otherwise returns false.

See also unite(), intersect(), and intersects().

### void QRectF::getCoords ( qreal * xp1, qreal * yp1, qreal * xp2, qreal * yp2 ) const

Extracts the rectangle parameters as the top-left point `*`xp1, `*`yp1 and the bottom-right point `*`xp2, `*`yp2.

See also setCoords() and getRect().

### void QRectF::getRect ( qreal * x, qreal * y, qreal * w, qreal * h ) const

Extracts the position of the rectangle's top-left corner to `*`x, `*`y, its width to `*`w, and its height to `*`h.

See also setRect() and getCoords().

### qreal QRectF::height () const

Returns the height of the rectangle.

See also width(), size(), and setHeight().

### QRectF QRectF::intersect ( const QRectF & other ) const

Returns the intersection of this rectangle and the other rectangle. `r.intersect(s)` is equivalent to `r&s`.

See also unite().

### bool QRectF::intersects ( const QRectF & rectangle ) const

Returns true if this rectangle intersects with the given rectangle (there is at least one pixel that is within both rectangles); otherwise returns false.

See also intersect() and contains().

### bool QRectF::isEmpty () const

Returns true if the rectangle is empty; otherwise returns false.

An empty rectangle has a width() <= 0 or height() <= 0.

An empty rectangle is not valid. `isEmpty() == !isValid()`

See also isNull(), isValid(), and normalized().

### bool QRectF::isNull () const

Returns true if the rectangle is a null rectangle; otherwise returns false.

A null rectangle has both the width and the height set to 0.

A null rectangle is also empty and invalid.

See also isEmpty() and isValid().

### bool QRectF::isValid () const

Returns true if the rectangle is valid; otherwise returns false.

A valid rectangle has a width() > 0 and height() > 0.

Note that non-trivial operations like intersections are not defined for invalid rectangles.

`isValid() == !isEmpty()`

See also isNull(), isEmpty(), and normalized().

### qreal QRectF::left () const

Returns the left coordinate of the rectangle. Identical to x().

See also setLeft(), right(), topLeft(), and bottomLeft().

### void QRectF::moveBottom ( qreal pos )

Sets the bottom position of the rectangle to pos, leaving the size unchanged.

See also bottom(), setBottom(), moveLeft(), moveTop(), and moveRight().

### void QRectF::moveBottomLeft ( const QPointF & p )

Sets the bottom-left position of the rectangle to p, leaving the size unchanged.

See also bottomLeft(), setBottomLeft(), moveTopLeft(), moveBottomRight(), and moveTopRight().

### void QRectF::moveBottomRight ( const QPointF & p )

Sets the bottom-right position of the rectangle to p, leaving the size unchanged.

See also bottomRight(), setBottomRight(), moveTopLeft(), moveTopRight(), and moveBottomLeft().

### void QRectF::moveCenter ( const QPointF & pos )

Set the center position of the rectangle to pos, leaving the size unchanged.

### void QRectF::moveLeft ( qreal pos )

Sets the left position of the rectangle to pos, leaving the size unchanged.

See also left(), setLeft(), moveTop(), moveRight(), and moveBottom().

### void QRectF::moveRight ( qreal pos )

Sets the right position of the rectangle to pos, leaving the size unchanged.

See also right(), setRight(), moveLeft(), moveTop(), and moveBottom().

### void QRectF::moveTo ( qreal x, qreal y )

Moves the top left corner of the rectangle to x and y, without changing the rectangles size.

See also translate() and moveTopLeft().

### void QRectF::moveTo ( const QPointF & pt )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Moves the top left corner of the rectangle to pt, without changing the rectangles size.

See also translate() and moveTopLeft().

### void QRectF::moveTop ( qreal pos )

Sets the top position of the rectangle to pos, leaving the size unchanged.

See also top(), setTop(), moveLeft(), moveRight(), and moveBottom().

### void QRectF::moveTopLeft ( const QPointF & p )

Sets the top-left position of the rectangle to p, leaving the size unchanged.

See also topLeft(), setTopLeft(), moveBottomRight(), moveTopRight(), and moveBottomLeft().

### void QRectF::moveTopRight ( const QPointF & p )

Sets the top-right position of the rectangle to p, leaving the size unchanged.

See also topRight(), setTopRight(), moveTopLeft(), moveBottomRight(), and moveBottomLeft().

### QRectF QRectF::normalized () const

Returns a normalized rectangle; i.e. a rectangle that has a non-negative width and height.

It swaps left and right if width() < 0, and swaps top and bottom if height() < 0.

See also isValid().

### qreal QRectF::right () const

Returns the right coordinate of the rectangle.

See also setRight(), left(), topRight(), and bottomRight().

### void QRectF::setBottom ( qreal pos )

Sets the bottom edge of the rectangle to pos. May change the height, but will never change the top edge of the rectangle.

See also bottom(), setTop(), and setHeight().

### void QRectF::setBottomLeft ( const QPointF & p )

Set the bottom-left corner of the rectangle to p. May change the size, but will the never change the top-right corner of the rectangle.

See also bottomLeft(), moveBottomLeft(), setTopLeft(), setBottomRight(), and setTopRight().

### void QRectF::setBottomRight ( const QPointF & p )

Set the bottom-right corner of the rectangle to p. May change the size, but will the never change the top-left corner of the rectangle.

See also bottomRight(), moveBottomRight(), setTopLeft(), setTopRight(), and setBottomLeft().

### void QRectF::setCoords ( qreal xp1, qreal yp1, qreal xp2, qreal yp2 )

Sets the position of the rectangle's top-left corner to (xp1, yp1), and the position of its bottom-right corner to (xp2, yp2).

See also getCoords() and setRect().

### void QRectF::setHeight ( qreal height )

Sets the height of the rectangle. The top edge is not moved, but the bottom edge may be moved.

See also height(), setTop(), setBottom(), and setSize().

### void QRectF::setLeft ( qreal pos )

Sets the left edge of the rectangle to pos. May change the width, but will never change the right edge of the rectangle.

Identical to setX().

See also left(), setTop(), and setWidth().

### void QRectF::setRect ( qreal x, qreal y, qreal width, qreal height )

Sets the position of the rectangle's top-left corner to (x, y), and resizes it to the specified width and height.

See also getRect() and setCoords().

### void QRectF::setRight ( qreal pos )

Sets the right edge of the rectangle to pos. May change the width, but will never change the left edge of the rectangle.

See also right(), setLeft(), and setWidth().

### void QRectF::setSize ( const QSizeF & size )

Sets the size of the rectangle to size. The top-left corner is not moved.

See also size(), setWidth(), and setHeight().

### void QRectF::setTop ( qreal pos )

Sets the top edge of the rectangle to pos. May change the height, but will never change the bottom edge of the rectangle.

Identical to setY().

See also top(), setBottom(), and setHeight().

### void QRectF::setTopLeft ( const QPointF & p )

Set the top-left corner of the rectangle to p. May change the size, but will the never change the bottom-right corner of the rectangle.

See also topLeft(), moveTopLeft(), setBottomRight(), setTopRight(), and setBottomLeft().

### void QRectF::setTopRight ( const QPointF & p )

Set the top-right corner of the rectangle to p. May change the size, but will the never change the bottom-left corner of the rectangle.

See also topRight(), moveTopRight(), setTopLeft(), setBottomRight(), and setBottomLeft().

### void QRectF::setWidth ( qreal width )

Sets the width of the rectangle. If the width is different to the old width, only the rectangle's right edge is moved. The left edge is not moved.

See also width(), setLeft(), setRight(), and setSize().

### void QRectF::setX ( qreal x )

Sets the x position of the rectangle (its left end) to x. May change the width, but will never change the right edge of the rectangle.

Identical to setLeft().

See also x() and setY().

### void QRectF::setY ( qreal y )

Sets the y position of the rectangle (its top) to y. May change the height, but will never change the bottom edge of the rectangle.

Identical to setTop().

See also y() and setX().

### QSizeF QRectF::size () const

Returns the size of the rectangle to floating point accuracy.

See also setSize(), width(), and height().

### QRect QRectF::toRect () const

Returns a QRect based on the values of this rectangle. Note that the coordinates in the returned rectangle are rounded to the nearest integer.

### qreal QRectF::top () const

Returns the top coordinate of the rectangle. Identical to y().

See also setTop(), bottom(), topLeft(), and topRight().

### QPointF QRectF::topLeft () const

Returns the top-left position of the rectangle.

See also setTopLeft(), moveTopLeft(), bottomRight(), left(), and top().

### QPointF QRectF::topRight () const

Returns the top-right position of the rectangle.

See also setTopRight(), moveTopRight(), bottomLeft(), top(), and right().

### void QRectF::translate ( qreal dx, qreal dy )

Moves the rectangle dx along the x-axis and dy along the y-axis, relative to the current position. Positive values move the rectangle to the right and downwards.

See also moveTo(), moveTopLeft(), and translated().

### void QRectF::translate ( const QPointF & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Moves the rectangle p.x() along the x-axis and p.y() along the y-axis, relative to the current position. Positive values move the rectangle to the right and downwards.

See also moveTo(), moveTopLeft(), and translated().

### QRectF QRectF::translated ( qreal dx, qreal dy ) const

Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis, relative to the current position. Positive values move the rectangle to the right and down.

See also moveTo(), moveTopLeft(), and translate().

### QRectF QRectF::translated ( const QPointF & p ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a copy of the rectangle that is translated p.x() along the x axis and p.y() along the y axis, relative to the current position. Positive values move the rectangle to the right and down.

See also moveTo(), moveTopLeft(), and translate().

### QRectF QRectF::unite ( const QRectF & other ) const

Returns the bounding rectangle of this rectangle and the other rectangle. `r.unite(s)` is equivalent to `r|s`.

See also intersect().

### qreal QRectF::width () const

Returns the width of the rectangle. The width includes both the left and right edges.

See also setWidth(), height(), size(), and setHeight().

### qreal QRectF::x () const

Returns the x-coordinate at the left edge of the rectangle. Identical to left().

See also left(), y(), and setX().

### qreal QRectF::y () const

Returns the y-coordinate at the top edge of the rectangle. Identical to top().

See also top(), x(), and setY().

### QRectF QRectF::operator& ( const QRectF & other ) const

Returns the intersection of this rectangle and the other rectangle.

Returns an empty rectangle if there is no intersection.

See also operator&=(), operator|(), isEmpty(), intersects(), and contains().

### QRectF & QRectF::operator&= ( const QRectF & other )

Intersects this rectangle with the other rectangle, and returns the result.

See also operator|=().

### QRectF QRectF::operator| ( const QRectF & other ) const

Returns the bounding rectangle of this rectangle and the other rectangle.

See also operator|=(), operator&(), intersects(), and contains().

### QRectF & QRectF::operator|= ( const QRectF & other )

Unites this rectangle with the other rectangle, and returns the result.

See also operator&=().

## Related Non-Members

### bool operator!= ( const QRectF & r1, const QRectF & r2 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns true if the rectangles r1 and r2 are different; otherwise returns false.

### QDataStream & operator<< ( QDataStream & stream, const QRectF & rectangle )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the rectangle to the stream, and returns a reference to the stream.

See also Format of the QDataStream operators.

### bool operator== ( const QRectF & r1, const QRectF & r2 )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns true if the rectangles r1 and r2 are equal; otherwise returns false.

### QDataStream & operator>> ( QDataStream & stream, QRectF & rectangle )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads a rectangle from the stream, and returns a reference to the stream.

See also Format of the QDataStream operators.