QgsRectangle class

A rectangle specified with double values.

QgsRectangle is used to store a rectangle when double values are required. Examples are storing a layer extent or the current view extent of a map

Derived classes

class QgsReferencedRectangle
A QgsRectangle with associated coordinate reference system.

Public static functions

static auto fromCenterAndSize(QgsPointXY center, double width, double height) -> QgsRectangle
Creates a new rectangle, given the specified center point and width and height.
static auto fromWkt(const QString& wkt) -> QgsRectangle
Creates a new rectangle from a wkt string.

Constructors, destructors, conversion operators

QgsRectangle() defaulted
Constructor for a null rectangle.
QgsRectangle(double xMin, double yMin = 0, double xMax = 0, double yMax = 0) explicit
Constructor.
QgsRectangle(const QgsPointXY& p1, const QgsPointXY& p2)
Construct a rectangle from two points. The rectangle is normalized after construction.
QgsRectangle(const QRectF& qRectF)
Construct a rectangle from a QRectF. The rectangle is normalized after construction.
QgsRectangle(const QgsRectangle& other)
Copy constructor.
operator QVariant() const
Allows direct construction of QVariants from rectangles.

Public functions

auto area() const -> double
Returns the area of the rectangle.
auto asPolygon() const -> QString
Returns the rectangle as a polygon.
auto asWktCoordinates() const -> QString
Returns a string representation of the rectangle in WKT format.
auto asWktPolygon() const -> QString
Returns a string representation of the rectangle as a WKT Polygon.
auto buffered(double width) const -> QgsRectangle
Gets rectangle enlarged by buffer.
auto center() const -> QgsPointXY
Returns the center point of the rectangle.
void combineExtentWith(const QgsRectangle& rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void combineExtentWith(double x, double y)
Expands the rectangle so that it covers both the original rectangle and the given point.
void combineExtentWith(const QgsPointXY& point)
Expands the rectangle so that it covers both the original rectangle and the given point.
auto contains(const QgsRectangle& rect) const -> bool
Returns true when rectangle contains other rectangle.
auto contains(const QgsPointXY& p) const -> bool
Returns true when rectangle contains a point.
void grow(double delta)
Grows the rectangle in place by the specified amount.
auto height() const -> double
Returns the height of the rectangle.
void include(const QgsPointXY& p)
Updates the rectangle to include the specified point.
auto intersect(const QgsRectangle& rect) const -> QgsRectangle
Returns the intersection with the given rectangle.
auto intersects(const QgsRectangle& rect) const -> bool
Returns true when rectangle intersects with other rectangle.
void invert()
Swap x/y coordinates in the rectangle.
auto isEmpty() const -> bool
Returns true if the rectangle is empty.
auto isFinite() const -> bool
Returns true if the rectangle has finite boundaries.
auto isNull() const -> bool
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
void normalize()
Normalize the rectangle so it has non-negative width/height.
auto operator!=(const QgsRectangle& r1) const -> bool
Comparison operator.
auto operator+(QgsVector v) const -> QgsRectangle
Returns a rectangle offset from this one in the direction of the vector.
auto operator+=(QgsVector v) -> QgsRectangle&
Moves this rectangle in the direction of the vector.
auto operator-(QgsVector v) const -> QgsRectangle
Returns a rectangle offset from this one in the direction of the reversed vector.
auto operator-=(QgsVector v) -> QgsRectangle&
Moves this rectangle in the direction of the reversed vector.
auto operator=(const QgsRectangle& r1) -> QgsRectangle&
Assignment operator.
auto operator==(const QgsRectangle& r1) const -> bool
Comparison operator.
auto perimeter() const -> double
Returns the perimeter of the rectangle.
void scale(double scaleFactor, const QgsPointXY* c = nullptr)
Scale the rectangle around its center point.
void scale(double scaleFactor, double centerX, double centerY)
Scale the rectangle around its center point.
auto scaled(double scaleFactor, const QgsPointXY* center = nullptr) const -> QgsRectangle
Scale the rectangle around its center point.
void set(const QgsPointXY& p1, const QgsPointXY& p2)
Sets the rectangle from two QgsPoints.
void set(double xMin, double yMin, double xMax, double yMax)
Sets the rectangle from four points.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
void setXMaximum(double x)
Set the maximum x value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setYMinimum(double y)
Set the minimum y value.
auto snappedToGrid(double spacing) const -> QgsRectangle
Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines.
auto toBox3d(double zMin, double zMax) const -> QgsBox3d
Converts the rectangle to a 3D box, with the specified zMin and zMax z values.
auto toRectF() const -> QRectF
Returns a QRectF with same coordinates as the rectangle.
auto toString(int precision = 16) const -> QString
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the specified precision.
auto width() const -> double
Returns the width of the rectangle.
auto xMaximum() const -> double
Returns the x maximum value (right side of rectangle).
auto xMinimum() const -> double
Returns the x minimum value (left side of rectangle).
auto yMaximum() const -> double
Returns the y maximum value (top side of rectangle).
auto yMinimum() const -> double
Returns the y minimum value (bottom side of rectangle).

Function documentation

static QgsRectangle QgsRectangle::fromCenterAndSize(QgsPointXY center, double width, double height)

Creates a new rectangle, given the specified center point and width and height.

static QgsRectangle QgsRectangle::fromWkt(const QString& wkt)

Creates a new rectangle from a wkt string.

The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.

double QgsRectangle::area() const

Returns the area of the rectangle.

QgsRectangle QgsRectangle::buffered(double width) const

Gets rectangle enlarged by buffer.

void QgsRectangle::combineExtentWith(const QgsPointXY& point)

Expands the rectangle so that it covers both the original rectangle and the given point.

void QgsRectangle::grow(double delta)

Grows the rectangle in place by the specified amount.

double QgsRectangle::height() const

Returns the height of the rectangle.

bool QgsRectangle::isEmpty() const

Returns true if the rectangle is empty.

An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).

bool QgsRectangle::isFinite() const

Returns true if the rectangle has finite boundaries.

Will return false if any of the rectangle boundaries are NaN or Inf.

bool QgsRectangle::isNull() const

Test if the rectangle is null (all coordinates zero or after call to setMinimal()).

A null rectangle is also an empty rectangle.

bool QgsRectangle::operator!=(const QgsRectangle& r1) const

Comparison operator.

Returns False if rectangles are equal

QgsRectangle QgsRectangle::operator+(QgsVector v) const

Returns a rectangle offset from this one in the direction of the vector.

QgsRectangle& QgsRectangle::operator+=(QgsVector v)

Moves this rectangle in the direction of the vector.

QgsRectangle QgsRectangle::operator-(QgsVector v) const

Returns a rectangle offset from this one in the direction of the reversed vector.

QgsRectangle& QgsRectangle::operator-=(QgsVector v)

Moves this rectangle in the direction of the reversed vector.

QgsRectangle& QgsRectangle::operator=(const QgsRectangle& r1)

Assignment operator.

Parameters
r1 QgsRectangle to assign from

bool QgsRectangle::operator==(const QgsRectangle& r1) const

Comparison operator.

Returns True if rectangles are equal

double QgsRectangle::perimeter() const

Returns the perimeter of the rectangle.

QgsRectangle QgsRectangle::scaled(double scaleFactor, const QgsPointXY* center = nullptr) const

Scale the rectangle around its center point.

void QgsRectangle::set(const QgsPointXY& p1, const QgsPointXY& p2)

Sets the rectangle from two QgsPoints.

The rectangle is normalised after construction.

void QgsRectangle::set(double xMin, double yMin, double xMax, double yMax)

Sets the rectangle from four points.

The rectangle is normalised after construction.

void QgsRectangle::setMinimal()

Set a rectangle so that min corner is at max and max corner is at min.

It is NOT normalized.

QgsRectangle QgsRectangle::snappedToGrid(double spacing) const

Returns a copy of this rectangle that is snapped to a grid with the specified spacing between the grid lines.

QgsBox3d QgsRectangle::toBox3d(double zMin, double zMax) const

Converts the rectangle to a 3D box, with the specified zMin and zMax z values.

QString QgsRectangle::toString(int precision = 16) const

Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the specified precision.

If the specified precision is less than 0, a suitable minimum precision is used.

double QgsRectangle::width() const

Returns the width of the rectangle.