Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QRectF Class Reference

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 QPointF & bottomRight )
- QRectF ( const QPointF & topLeft, const QSizeF & size )
- QRectF ( float x, float y, float width, float height )
- QRectF ( const QRect & rect )
- void addCoords ( float xp1, float yp1, float xp2, float yp2 )
- float bottom () const
- QPointF bottomLeft () const
- QPointF bottomRight () const
- QPointF center () const
- bool contains ( const QPointF & point ) const
- bool contains ( float x, float y ) const
- bool contains ( const QRectF & rectangle ) const
- void getCoords ( float * xp1, float * yp1, float * xp2, float * yp2 ) const
- void getRect ( float * x, float * y, float * w, float * h ) const
- float height () const
- QRectF intersect ( const QRectF & other ) const
- bool intersects ( const QRectF & rectangle ) const
- bool isEmpty () const
- bool isNull () const
- bool isValid () const
- float left () const
- void moveBottom ( float y )
- void moveBottomLeft ( const QPointF & point )
- void moveBottomRight ( const QPointF & point )
- void moveBy ( float dx, float dy )
- void moveBy ( const QPointF & p )
- void moveCenter ( const QPointF & p )
- void moveLeft ( float x )
- void moveRight ( float x )
- void moveTop ( float y )
- void moveTopLeft ( const QPointF & point )
- void moveTopRight ( const QPointF & point )
- QRectF normalize () const
- float & rHeight ()
- float & rLeft ()
- float & rTop ()
- float & rWidth ()
- float right () const
- void setBottom ( float y )
- void setBottomLeft ( const QPointF & point )
- void setBottomRight ( const QPointF & point )
- void setCoords ( float xp1, float yp1, float xp2, float yp2 )
- void setHeight ( float height )
- void setLeft ( float x )
- void setRect ( float x, float y, float width, float height )
- void setRight ( float x )
- void setSize ( const QSizeF & size )
- void setTop ( float y )
- void setTopLeft ( const QPointF & point )
- void setTopRight ( const QPointF & point )
- void setWidth ( float width )
- void setX ( float x )
- void setY ( float y )
- QSizeF size () const
- QRect toRect () const
- float top () const
- QPointF topLeft () const
- QPointF topRight () const
- QRectF unite ( const QRectF & other ) const
- float width () const
- float x () const
- float 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.

Rectangles are used throughout Qt to describe the area covered by visible items. They specify both the geometry of widgets and the extent of items that are painted using a QPainter. QRectF provides a variant of the QRect class that defines the position and dimension of the rectangle using `float` values for accuracy.

A rectangle is internally represented as the positions of its upper-left and bottom-right corners, but it is normally expressed as the position of its upper-left corner and a size.

Note that the size (width and height) of a rectangle might be different from what you expect. If the top-left corner and the bottom-right corner are the same, the height and the width of the rectangle will both be 1.

Generally, width = right - left + 1 and height = bottom - top + 1. We designed it this way to make it correspond to rectangular spaces used by drawing functions where the width and height denote a whole number of pixels. For example, drawing a rectangle with width and height 1 draws a single pixel.

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

A QRectF can be constructed with a set of floats specifying the coordinates of the top-left corner of the rectangle and its dimensions from two QPointF values, or from a QPointF and a QSizeF. 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 addCoords().

You can test to see if a rectangle contains a specific point with the contains() function. You can also test to see if two rectangles intersect with the intersects() function (see also intersect()). To get the bounding rectangle of two rectangles use unite().

See also QPointF and QSizeF.

## Member Function Documentation

### QRectF::QRectF ()

Constructs an invalid rectangle.

### QRectF::QRectF ( const QPointF & topLeft, const QPointF & bottomRight )

Constructs a rectangle with topLeft as the top-left corner and bottomRight as the bottom-right corner.

### 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 ( float x, float y, float width, float height )

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

Example (creates three identical rectangles):

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

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

Constructs a rectangle with floating point accuracy from the given rect.

### void QRectF::addCoords ( float xp1, float yp1, float xp2, float yp2 )

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

### float QRectF::bottom () const

Returns the y-coordinate at the bottom edge 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.

### bool QRectF::contains ( float x, float 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.

### 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 ( float * xp1, float * yp1, float * xp2, float * yp2 ) const

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

See also setCoords() and rect().

### void QRectF::getRect ( float * x, float * y, float * w, float * 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 coords().

### float QRectF::height () const

Returns the height of the rectangle. The height includes both the top and bottom edges; i.e. height = bottom - top + 1.

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 left() > right() or top() > bottom().

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

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

### 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, so that right() == left() - 1 and bottom() == top() - 1.

Note that if right() == left() and bottom() == top() then the rectangle has a width of 1 and a height of 1.

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 left() <= right() and top() <= bottom().

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

`isValid() == !isEmpty()`

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

### float QRectF::left () const

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

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

### void QRectF::moveBottom ( float y )

Moves the rectangle so that the y-coordinate of its bottom edge is at y. The size of the rectangle is unchanged.

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

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

Moves the rectangle so that the position of its bottom-left corner lies at the given point. The size of the rectangle is unchanged.

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

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

Moves the rectangle so that the position of its bottom-right corner lies at the given point. The size of the rectangle is unchanged.

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

### void QRectF::moveBy ( float dx, float 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 moveTopLeft().

### void QRectF::moveBy ( 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 moveTopLeft().

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

Moves the rectangle so that the position of its center point lies at the point specified by p. The size of the rectangle is unchanged.

See also center(), moveTopLeft(), moveBottomRight(), moveTopRight(), and moveBottomLeft().

### void QRectF::moveLeft ( float x )

Moves the rectangle so that the x-coordinate of its left edge is at x. The size of the rectangle is unchanged.

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

### void QRectF::moveRight ( float x )

Moves the rectangle so that the x-coordinate of its right edge is at x. The size of the rectangle is unchanged.

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

### void QRectF::moveTop ( float y )

Moves the rectangle so that the y-coordinate of its top edge is at y. The size of the rectangle is unchanged.

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

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

Moves the rectangle so that the position of its top-left corner lies at the given point. The size of the rectangle is unchanged.

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

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

Moves the rectangle so that the position of its top-right corner lies at the given point. The size of the rectangle is unchanged.

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

### QRectF QRectF::normalize () const

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

It swaps left and right if left() > right(), and swaps top and bottom if top() > bottom().

See also isValid().

### float & QRectF::rHeight ()

Returns a reference to the height of the rectangle.

See also rLeft(), rTop(), and rWidth().

### float & QRectF::rLeft ()

Returns a reference to the x-coordinate at the left edge of the rectangle.

See also rTop(), rWidth(), and rHeight().

### float & QRectF::rTop ()

Returns a reference to the y-coordinate at the top edge of the rectangle.

See also rLeft(), rWidth(), and rHeight().

### float & QRectF::rWidth ()

Returns a reference to the width of the rectangle.

See also rLeft(), rTop(), and rHeight().

### float QRectF::right () const

Returns the x-coordinate at the right edge of the rectangle.

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

### void QRectF::setBottom ( float y )

Sets the y-coordinate of the rectangle's bottom edge to y. The rectangle's height may change, but its top edge is not moved.

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

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

Sets the position of the rectangle's bottom-left corner to lie at the given point. The size of the rectangle may change. The rectangle's top and right edges are not moved.

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

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

Sets the position of the rectangle's bottom-right corner to lie at the given point. The size of the rectangle may change. The rectangle's top and left edges are not moved.

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

### void QRectF::setCoords ( float xp1, float yp1, float xp2, float 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 coords() and setRect().

### void QRectF::setHeight ( float 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 ( float x )

Sets the x-coordinate of the rectangle's left edge to x. The rectangle's width may change, but its right edge is not moved.

Identical to setX().

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

### void QRectF::setRect ( float x, float y, float width, float 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 rect() and setCoords().

### void QRectF::setRight ( float x )

Sets the x-coordinate of the rectangle's right edge to x. The rectangle's width may change, but its left edge is not moved.

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 ( float y )

Sets the y-coordinate of the rectangle's top edge to y. The rectangle's height may change, but its bottom edge is not moved.

Identical to setY().

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

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

Sets the position of the rectangle's top-left corner to lie at the given point. The size of the rectangle may change. The rectangle's bottom and right edges are not moved.

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

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

Sets the position of the rectangle's top-right corner to lie at the given point. The size of the rectangle may change. The rectangle's bottom and left edges are not moved.

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

### void QRectF::setWidth ( float 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 ( float x )

Sets the x-coordinate of the rectangle's left edge to x. The rectangle's width may change, but its right edge is not moved.

Identical to setLeft().

See also x() and setY().

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

Sets the y-coordinate of the rectangle's top edge to y. The rectangle's height may change, but its bottom edge is not moved.

Identical to setTop().

See also y() and setX().

### QSizeF QRectF::size () const

Returns the size of the rectangle to floating point accuracy.

See also width() and height().

### QRect QRectF::toRect () const

Returns a rectangle with integer precision. Note that the coordinates in the returned rectangle are less precise than those in the floating point rectangle.

### float QRectF::top () const

Returns the y-coordinate at the top edge 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().

### 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().

### float QRectF::width () const

Returns the width of the rectangle. The width includes both the left and right edges; i.e. width = right - left + 1.

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

### float QRectF::x () const

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

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

### float 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.

The bounding rectangle of a non-empty rectangle and an empty (or invalid) rectangle is defined to be the non-empty 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.