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

# QRect Class Reference

The QRect class defines a rectangle in the plane. More...

    #include <QRect>

Part of the QtCore module.

- List of all members, including inherited members
- Qt 3 support members

### Public Functions

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

### Related Non-Members

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

## Detailed Description

The QRect class defines a rectangle in the plane.

A rectangle is internally represented as an upper-left corner and a bottom-right corner, but it is normally expressed as an upper-left corner and a size.

Note that the size (width and height) of a rectangle might be different from what you are used to. 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 in which the width and height denote a 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 y axis is down, and the positive x axis is from left to right.

A QRect can be constructed with a set of left, top, width and height integers, from two QPoints or from a QPoint and a QSize. The following code creates three identical triangles.

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

After creation the dimensions can be changed, e.g. with setLeft(), setRight(), setTop() and setBottom(), or by setting sizes, e.g. setWidth(), setHeight() and setSize(). The dimensions can also be changed with the move functions, e.g. translate(), moveCenter(), moveBottomRight(), etc. You can also add coordinates to a rectangle with adjust(). You can get a new rectangle based on adjustments from the original rectangled with adjusted().

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

See also QPoint, QSize, QPolygon, and QRectF.

## Member Function Documentation

### QRect::QRect ()

Constructs a null rectangle.

See also isNull().

### QRect::QRect ( const QPoint & topLeft, const QPoint & bottomRight )

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

### QRect::QRect ( const QPoint & topLeft, const QSize & size )

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

### QRect::QRect ( int left, int top, int width, int height )

Constructs a rectangle with the top, left corner and width and height.

### void QRect::adjust ( int xp1, int yp1, int xp2, int yp2 )

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

See also adjusted().

### QRect QRect::adjusted ( int xp1, int yp1, int xp2, int yp2 ) const

Returns a new rectangle with xp1, yp1, xp2 and yp2 added to the existing coordinates of this rectangle.

See also adjust().

### int QRect::bottom () const

Returns the bottom coordinate of the rectangle.

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

### QPoint QRect::bottomLeft () const

Returns the bottom-left position of the rectangle.

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

### QPoint QRect::bottomRight () const

Returns the bottom-right position of the rectangle.

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

### QPoint QRect::center () const

Returns the center point of the rectangle.

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

### bool QRect::contains ( const QPoint & p, bool proper = false ) const

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

If proper is true, this function returns true only if p is inside (not on the edge).

### bool QRect::contains ( int x, int y, bool proper ) 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.

If proper is true, this function returns true only if the point is entirely inside (not on the edge).

### bool QRect::contains ( int x, int 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 QRect::contains ( const QRect & r, bool proper = false ) const

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

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

If proper is true, this function returns true only if r is entirely inside (not on the edge).

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

### void QRect::getCoords ( int * xp1, int * yp1, int * xp2, int * 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 QRect::getRect ( int * x, int * y, int * w, int * h ) const

Extracts the rectangle parameters as the position `*`x, `*`y and width `*`w and height `*`h.

See also setRect() and getCoords().

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

### QRect QRect::intersect ( const QRect & r ) const

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

### bool QRect::intersects ( const QRect & r ) const

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

See also intersect() and contains().

### bool QRect::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 normalized().

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

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

A null rectangle is also empty.

A null rectangle is not valid.

See also isEmpty() and isValid().

### bool QRect::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 normalized().

### int QRect::left () const

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

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

### void QRect::moveBottom ( int pos )

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

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

### void QRect::moveBottomLeft ( const QPoint & p )

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

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

### void QRect::moveBottomRight ( const QPoint & p )

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

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

### void QRect::moveCenter ( const QPoint & p )

Sets the center point of the rectangle to p, leaving the size unchanged.

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

### void QRect::moveLeft ( int pos )

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

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

### void QRect::moveRight ( int pos )

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

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

### void QRect::moveTo ( int x, int y )

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

See also translate() and moveTopLeft().

### void QRect::moveTo ( const QPoint & 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 QRect::moveTop ( int pos )

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

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

### void QRect::moveTopLeft ( const QPoint & p )

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

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

### void QRect::moveTopRight ( const QPoint & p )

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

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

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

### int QRect::right () const

Returns the right coordinate of the rectangle.

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

### void QRect::setBottom ( int 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 QRect::setBottomLeft ( const QPoint & 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 QRect::setBottomRight ( const QPoint & 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 QRect::setCoords ( int xp1, int yp1, int xp2, int yp2 )

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

See also getCoords() and setRect().

### void QRect::setHeight ( int h )

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

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

### void QRect::setLeft ( int 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 QRect::setRect ( int x, int y, int w, int h )

Sets the coordinates of the rectangle's top-left corner to (x, y), and its size to (w, h).

See also getRect() and setCoords().

### void QRect::setRight ( int 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 QRect::setSize ( const QSize & s )

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

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

### void QRect::setTop ( int 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 QRect::setTopLeft ( const QPoint & 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 QRect::setTopRight ( const QPoint & 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 QRect::setWidth ( int w )

Sets the width of the rectangle to w. The right edge is changed, but not the left edge.

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

### void QRect::setX ( int 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 QRect::setY ( int 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().

### QSize QRect::size () const

Returns the size of the rectangle.

See also width() and height().

### int QRect::top () const

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

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

### QPoint QRect::topLeft () const

Returns the top-left position of the rectangle.

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

### QPoint QRect::topRight () const

Returns the top-right position of the rectangle.

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

### void QRect::translate ( int dx, int 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 down.

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

### void QRect::translate ( const QPoint & 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 down.

See also moveTopLeft().

### QRect QRect::translated ( int dx, int 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 translate().

### QRect QRect::translated ( const QPoint & 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 translate().

### QRect QRect::unite ( const QRect & r ) const

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

### int QRect::width () const

Returns the width of the rectangle.

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

### int QRect::x () const

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

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

### int QRect::y () const

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

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

### QRect QRect::operator& ( const QRect & r ) const

Returns the intersection of this rectangle and rectangle r.

Returns an empty rectangle if there is no intersection.

This is equivalent to calling

        intersect(r)

.

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

### QRect & QRect::operator&= ( const QRect & r )

Intersects this rectangle with rectangle r.

### QRect QRect::operator| ( const QRect & r ) const

Returns the bounding rectangle of this rectangle and rectangle r.

This is the equivalent to calling

        unite(r)

.

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

### QRect & QRect::operator|= ( const QRect & r )

Unites this rectangle with rectangle r.

## Related Non-Members

### bool operator!= ( const QRect & r1, const QRect & 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 QRect & 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 QRect & r1, const QRect & 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, QRect & 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.