6.0.0-rc4Source

QRect Class

The QRect class defines a rectangle in the plane using integer precision. More…

Header#include <QRect>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QRect()
QRect(const QPoint &topLeft, const QPoint &bottomRight)
QRect(const QPoint &topLeft, const QSize &s)
QRect(int left, int top, int width, int height)
boolisNull() const
boolisEmpty() const
boolisValid() const
intleft() const
inttop() const
intright() const
intbottom() const
intx() const
inty() const
intwidth() const
intheight() const
QSizesize() const
QPointtopLeft() const
QPointbottomRight() const
QPointtopRight() const
QPointbottomLeft() const
QPointcenter() const
voidsetLeft(int v)
voidsetTop(int v)
voidsetRight(int v)
voidsetBottom(int v)
voidsetX(int v)
voidsetY(int v)
voidsetTopLeft(const QPoint &p)
voidsetBottomRight(const QPoint &p)
voidsetTopRight(const QPoint &p)
voidsetBottomLeft(const QPoint &p)
voidsetWidth(int w)
voidsetHeight(int h)
voidsetSize(const QSize &s)
voidsetRect(int x, int y, int w, int h)
voidsetCoords(int xa, int ya, int xb, int yb)
voidgetRect(int *x, int *y, int *w, int *h) const
voidgetCoords(int *xa, int *ya, int *xb, int *yb) const
voidmoveLeft(int p)
voidmoveTop(int p)
voidmoveRight(int p)
voidmoveBottom(int p)
voidmoveTo(int x, int y)
voidmoveTo(const QPoint &p)
voidmoveTopLeft(const QPoint &p)
voidmoveBottomRight(const QPoint &p)
voidmoveTopRight(const QPoint &p)
voidmoveBottomLeft(const QPoint &p)
voidmoveCenter(const QPoint &p)
voidtranslate(int dx, int dy)
voidtranslate(const QPoint &p)
QRecttranslated(int dx, int dy) const
QRecttranslated(const QPoint &p) const
QRecttransposed() const
voidadjust(int a, int b, int c, int d)
QRectadjusted(int a, int b, int c, int d) const
QRectmarginsAdded(const QMargins &m) const
QRectmarginsRemoved(const QMargins &m) const
QRectnormalized() const
boolcontains(const QPoint &p, bool proper = false) const
boolcontains(int x, int y, bool proper = false) const
boolcontains(const QRect &o, bool proper = false) const
QRectunited(const QRect &o) const
QRectintersected(const QRect &o) const
boolintersects(const QRect &o) const
QRectoperator|(const QRect &o) const
QRectoperator&(const QRect &o) const
QRect &operator|=(const QRect &o)
QRect &operator&=(const QRect &o)
QRect &operator+=(const QMargins &m)
QRect &operator-=(const QMargins &m)
QRectFtoRectF() const

Static Public Members

QRectspan(const QPoint &p1, const QPoint &p2)

Detailed Description

A rectangle is stored as the coordinates of its top-left and bottom-right corners, both inclusive. For historical reasons right() is therefore left() + width() - 1 and bottom() is top() + height() - 1. Use QRectF for floating-point coordinates, where the right and bottom edges are exclusive.

A rectangle with a width or height less than 1 is empty (see isEmpty()); a default-constructed rectangle is null (see isNull()). Use normalized() to obtain an equivalent rectangle with non-negative width and height.

See also QRectF, QPoint, QSize and QMargins.

Differences from Qt

None.

Member Function Documentation

[constexpr] QRect::QRect()

Constructs a null rectangle.

See also isNull().

[constexpr] QRect::QRect(const QPoint &topLeft, const QPoint &bottomRight)

Constructs a rectangle with the given topLeft and bottomRight corners, both included in the rectangle.

[constexpr] QRect::QRect(const QPoint &topLeft, const QSize &s)

Constructs a rectangle with the given topLeft corner and the given size s.

[constexpr] QRect::QRect(int left, int top, int width, int height)

Constructs a rectangle with left, top as its top-left corner and the given width and height.

[constexpr] void QRect::adjust(int a, int b, int c, int d)

Adds a, b, c and d to the left, top, right and bottom coordinates of the rectangle respectively.

See also adjusted() and setRect().

[constexpr] QRect QRect::adjusted(int a, int b, int c, int d) const

Returns a copy of the rectangle with a, b, c and d added to the left, top, right and bottom coordinates respectively.

See also adjust().

[constexpr] int QRect::bottom() const

Returns the y coordinate of the rectangle's bottom edge, which is top() + height() - 1.

See also setBottom() and top().

[constexpr] QPoint QRect::bottomLeft() const

Returns the position of the rectangle's bottom-left corner.

See also setBottomLeft() and topRight().

[constexpr] QPoint QRect::bottomRight() const

Returns the position of the rectangle's bottom-right corner.

See also setBottomRight() and topLeft().

[constexpr] QPoint QRect::center() const

Returns the center point of the rectangle, with each coordinate truncated to an integer.

See also moveCenter().

[constexpr] bool QRect::contains(const QPoint &p, bool proper = false) const

Returns true if the given point p is inside or on the edge of the rectangle; otherwise returns false. If proper is true, the point must be strictly inside the rectangle, not on its edge.

The rectangle is normalized before the test.

See also intersects().

[constexpr] bool QRect::contains(int x, int y, bool proper = false) const

This is an overloaded function.

Returns true if the point (x, y) is inside this rectangle, or on its edge unless proper is true.

[constexpr] bool QRect::contains(const QRect &o, bool proper = false) const

This is an overloaded function.

Returns true if the rectangle o is inside this rectangle; otherwise returns false. If proper is true, o must not touch the edges of this rectangle. A null rectangle never contains, and is never contained by, another rectangle.

[constexpr] void QRect::getCoords(int *xa, int *ya, int *xb, int *yb) const

Extracts the position of the rectangle's top-left corner to xa and ya, and the position of the bottom-right corner to xb and yb.

See also setCoords() and getRect().

[constexpr] void QRect::getRect(int *x, int *y, int *w, int *h) const

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

See also setRect() and getCoords().

[constexpr] int QRect::height() const

Returns the height of the rectangle.

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

[constexpr] QRect QRect::intersected(const QRect &o) const

Returns the intersection of this rectangle and the rectangle o. Returns a null rectangle if they do not intersect.

See also intersects() and united().

[constexpr] bool QRect::intersects(const QRect &o) const

Returns true if this rectangle intersects the rectangle o, that is, if there is at least one pixel that is within both rectangles; otherwise returns false.

See also intersected() and contains().

[constexpr] bool QRect::isEmpty() const

Returns true if the rectangle is empty, that is, its width or height is less than 1; otherwise returns false.

See also isNull() and isValid().

[constexpr] bool QRect::isNull() const

Returns true if the rectangle is a null rectangle, that is, it has width 0 and height 0; otherwise returns false.

See also isEmpty() and isValid().

[constexpr] bool QRect::isValid() const

Returns true if the rectangle is valid, that is, its width and height are at least 1; otherwise returns false.

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

[constexpr] int QRect::left() const

Returns the x coordinate of the rectangle's left edge. Equivalent to x().

See also setLeft() and right().

[constexpr] QRect QRect::marginsAdded(const QMargins &m) const

Returns a rectangle grown by the margins m: the left and top edges move outwards by the left and top margins, the right and bottom edges by the right and bottom margins.

See also marginsRemoved().

[constexpr] QRect QRect::marginsRemoved(const QMargins &m) const

Returns a rectangle shrunk by the margins m.

See also marginsAdded().

[constexpr] void QRect::moveBottom(int p)

Moves the rectangle vertically, leaving the rectangle's bottom edge at the given p. The rectangle's size is unchanged.

See also bottom() and moveTop().

[constexpr] void QRect::moveBottomLeft(const QPoint &p)

Moves the rectangle, leaving the bottom-left corner at the given position p. The rectangle's size is unchanged.

See also setBottomLeft().

[constexpr] void QRect::moveBottomRight(const QPoint &p)

Moves the rectangle, leaving the bottom-right corner at the given position p. The rectangle's size is unchanged.

See also setBottomRight().

[constexpr] void QRect::moveCenter(const QPoint &p)

Moves the rectangle, leaving the center point at the given position p. The rectangle's size is unchanged.

See also center().

[constexpr] void QRect::moveLeft(int p)

Moves the rectangle horizontally, leaving the rectangle's left edge at the given p. The rectangle's size is unchanged.

See also left() and moveRight().

[constexpr] void QRect::moveRight(int p)

Moves the rectangle horizontally, leaving the rectangle's right edge at the given p. The rectangle's size is unchanged.

See also right() and moveLeft().

[constexpr] void QRect::moveTo(int x, int y)

Moves the rectangle, leaving the top-left corner at the given position (x, y). The rectangle's size is unchanged.

See also translate() and moveTopLeft().

[constexpr] void QRect::moveTo(const QPoint &p)

This is an overloaded function.

Moves the rectangle, leaving the top-left corner at the given position p.

[constexpr] void QRect::moveTop(int p)

Moves the rectangle vertically, leaving the rectangle's top edge at the given p. The rectangle's size is unchanged.

See also top() and moveBottom().

[constexpr] void QRect::moveTopLeft(const QPoint &p)

Moves the rectangle, leaving the top-left corner at the given position p. The rectangle's size is unchanged.

See also moveTo() and setTopLeft().

[constexpr] void QRect::moveTopRight(const QPoint &p)

Moves the rectangle, leaving the top-right corner at the given position p. The rectangle's size is unchanged.

See also setTopRight().

[constexpr] QRect QRect::normalized() const

Returns a normalized rectangle, that is, one that has a non-negative width and height. If the width or height is negative, the corresponding edges are swapped.

See also isValid().

[constexpr] QRect QRect::operator&(const QRect &o) const

Returns the intersection of this rectangle and the rectangle o. Equivalent to intersected().

[constexpr] QRect & QRect::operator&=(const QRect &o)

Sets this rectangle to its intersection with the rectangle o, and returns a reference to this rectangle.

[constexpr] QRect & QRect::operator+=(const QMargins &m)

Adds the margins m to this rectangle, as marginsAdded() does, and returns a reference to this rectangle.

[constexpr] QRect & QRect::operator-=(const QMargins &m)

Removes the margins m from this rectangle, as marginsRemoved() does, and returns a reference to this rectangle.

[constexpr] QRect QRect::operator|(const QRect &o) const

Returns the bounding rectangle of this rectangle and the rectangle o. Equivalent to united().

[constexpr] QRect & QRect::operator|=(const QRect &o)

Sets this rectangle to the bounding rectangle of itself and the rectangle o, and returns a reference to this rectangle.

Returns the x coordinate of the rectangle's right edge, which is left() + width() - 1.

See also setRight() and left().

[constexpr] void QRect::setBottom(int v)

Sets the bottom edge of the rectangle to v, leaving the top edge unchanged and so changing the height.

See also bottom() and moveBottom().

[constexpr] void QRect::setBottomLeft(const QPoint &p)

Sets the bottom-left corner of the rectangle to p, leaving the top-right corner unchanged.

See also bottomLeft() and moveBottomLeft().

[constexpr] void QRect::setBottomRight(const QPoint &p)

Sets the bottom-right corner of the rectangle to p, leaving the top-left corner unchanged.

See also bottomRight() and moveBottomRight().

[constexpr] void QRect::setCoords(int xa, int ya, int xb, int yb)

Sets the coordinates of the rectangle's top-left corner to (xa, ya), and of its bottom-right corner to (xb, yb).

See also getCoords() and setRect().

[constexpr] void QRect::setHeight(int h)

Sets the height of the rectangle to h, moving the bottom edge and keeping the top edge.

See also height() and setWidth().

[constexpr] void QRect::setLeft(int v)

Sets the left edge of the rectangle to v, leaving the right edge unchanged and so changing the width.

See also left() and moveLeft().

[constexpr] 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 the given w and h.

See also getRect() and setCoords().

[constexpr] void QRect::setRight(int v)

Sets the right edge of the rectangle to v, leaving the left edge unchanged and so changing the width.

See also right() and moveRight().

[constexpr] void QRect::setSize(const QSize &s)

Sets the size of the rectangle to s, keeping the top-left corner.

See also size().

[constexpr] void QRect::setTop(int v)

Sets the top edge of the rectangle to v, leaving the bottom edge unchanged and so changing the height.

See also top() and moveTop().

[constexpr] void QRect::setTopLeft(const QPoint &p)

Sets the top-left corner of the rectangle to p, leaving the bottom-right corner unchanged.

See also topLeft() and moveTopLeft().

[constexpr] void QRect::setTopRight(const QPoint &p)

Sets the top-right corner of the rectangle to p, leaving the bottom-left corner unchanged.

See also topRight() and moveTopRight().

[constexpr] void QRect::setWidth(int w)

Sets the width of the rectangle to w, moving the right edge and keeping the left edge.

See also width() and setHeight().

[constexpr] void QRect::setX(int v)

Sets the left edge of the rectangle to v. Equivalent to setLeft().

See also x() and setY().

[constexpr] void QRect::setY(int v)

Sets the top edge of the rectangle to v. Equivalent to setTop().

See also y() and setX().

[constexpr] QSize QRect::size() const

Returns the size of the rectangle.

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

[static, constexpr] QRect QRect::span(const QPoint &p1, const QPoint &p2)

Returns a rectangle that spans the two points p1 and p2, including both points.

This function was introduced in 6.0.

[constexpr] int QRect::top() const

Returns the y coordinate of the rectangle's top edge. Equivalent to y().

See also setTop() and bottom().

[constexpr] QPoint QRect::topLeft() const

Returns the position of the rectangle's top-left corner.

See also setTopLeft() and bottomRight().

[constexpr] QPoint QRect::topRight() const

Returns the position of the rectangle's top-right corner.

See also setTopRight() and bottomLeft().

[constexpr] QRectF QRect::toRectF() const

Returns this rectangle as a rectangle with floating point accuracy.

This function was introduced in 6.4.

See also QRectF::toRect().

[constexpr] 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.

See also translated() and moveTo().

[constexpr] void QRect::translate(const QPoint &p)

This is an overloaded function.

Moves the rectangle by the offset p, relative to the current position.

[constexpr] 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.

See also translate().

[constexpr] QRect QRect::translated(const QPoint &p) const

This is an overloaded function.

Returns a copy of the rectangle that is translated by the offset p.

[constexpr] QRect QRect::transposed() const

Returns a copy of the rectangle with its width and height exchanged, keeping the top-left corner.

See also QSize::transposed().

[constexpr] QRect QRect::united(const QRect &o) const

Returns the bounding rectangle of this rectangle and the rectangle o. If one of the rectangles is null, the other is returned.

See also intersected().

[constexpr] int QRect::width() const

Returns the width of the rectangle.

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

[constexpr] int QRect::x() const

Returns the x coordinate of the rectangle's left edge. Equivalent to left().

See also setX() and y().

[constexpr] int QRect::y() const

Returns the y coordinate of the rectangle's top edge. Equivalent to top().

See also setY() and x().