QRectF Class

The QRectF class defines a rectangle in the plane using floating point precision. More...

Header: #include <QRectF>
qmake: QT += core

Note: All functions in this class are reentrant.

Public Functions

QRectF()
QRectF(const QPointF &topleft, const QSizeF &size)
QRectF(const QPointF &topleft, const QPointF &bottomRight)
QRectF(qreal left, qreal top, qreal width, qreal height)
QRectF(const QRect &rect)
void adjust(qreal x1, qreal y1, qreal x2, qreal y2)
QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const
qreal bottom() const
QPointF bottomLeft() const
QPointF bottomRight() const
QPointF center() const
bool contains(const QRectF &r) const
bool contains(const QPointF &p) const
bool contains(qreal x, qreal y) const
void getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const
void getRect(qreal *x, qreal *y, qreal *w, qreal *h) const
qreal height() const
QRectF intersect(const QRectF &r) const
QRectF intersected(const QRectF &other) const
bool intersects(const QRectF &r) const
bool isEmpty() const
bool isNull() const
bool isValid() const
qreal left() const
QRectF marginsAdded(const QMarginsF &margins) const
QRectF marginsRemoved(const QMarginsF &margins) const
void moveBottom(qreal pos)
void moveBottomLeft(const QPointF &p)
void moveBottomRight(const QPointF &p)
void moveCenter(const QPointF &p)
void moveLeft(qreal pos)
void moveRight(qreal pos)
void moveTo(qreal x, qreal y)
void moveTo(const QPointF &p)
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 x1, qreal y1, qreal x2, qreal y2)
void setHeight(qreal h)
void setLeft(qreal pos)
void setRect(qreal x, qreal y, qreal w, qreal h)
void setRight(qreal pos)
void setSize(const QSizeF &s)
void setTop(qreal pos)
void setTopLeft(const QPointF &p)
void setTopRight(const QPointF &p)
void setWidth(qreal w)
void setX(qreal pos)
void setY(qreal pos)
QSizeF size() const
QRect toAlignedRect() const
CGRect toCGRect() 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 transposed() const
QRectF unite(const QRectF &r) const
QRectF united(const QRectF &other) const
qreal width() const
qreal x() const
qreal y() const
QRectF operator&(const QRectF &r) const
QRectF &operator&=(const QRectF &r)
QRectF &operator+=(const QMarginsF &margins)
QRectF &operator-=(const QMarginsF &margins)
QRectF operator|(const QRectF &r) const
QRectF &operator|=(const QRectF &r)

Static Public Members

QRectF fromCGRect(CGRect rect)
bool operator!=(const QRectF &r1, const QRectF &r2)
QRectF operator+(const QRectF &lhs, const QMarginsF &rhs)
QRectF operator+(const QMarginsF &lhs, const QRectF &rhs)
QRectF operator-(const QRectF &lhs, const QMarginsF &rhs)
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 precision.

A rectangle is normally expressed as a top-left corner and a size. The size (width and height) of a QRectF is always equivalent to the mathematical rectangle that forms the basis for its rendering.

A QRectF can be constructed with a set of left, top, width and height coordinates, or from a QPointF and a QSizeF. The following code creates two identical rectangles.


  QRectF r1(100.0, 200.1, 11.2, 16.3);
  QRectF r2(QPointF(100.0, 200.1), QSizeF(11.2, 16.3));

There is also a third constructor creating a QRectF from a QRect, and a corresponding toRect() function that returns a QRect object based on the values of this rectangle (note that the coordinates in the returned rectangle are rounded to the nearest integer).

The QRectF class provides a collection of functions that return the various rectangle coordinates, and enable manipulation of these. QRectF also provides functions to move the rectangle relative to the various coordinates. In addition there is a moveTo() function that moves the rectangle, leaving its top left corner at the given coordinates. Alternatively, the translate() function moves the rectangle the given offset relative to the current position, and the translated() function returns a translated copy of this rectangle.

The size() function returns the rectange's dimensions as a QSizeF. The dimensions can also be retrieved separately using the width() and height() functions. To manipulate the dimensions use the setSize(), setWidth() or setHeight() functions. Alternatively, the size can be changed by applying either of the functions setting the rectangle coordinates, for example, setBottom() or setRight().

The contains() function tells whether a given point is inside the rectangle or not, and the intersects() function returns true if this rectangle intersects with a given rectangle (otherwise false). The QRectF class also provides the intersected() function which returns the intersection rectangle, and the united() function which returns the rectangle that encloses the given rectangle and this:

The isEmpty() function returns true if the rectangle's width or height is less than, or equal to, 0. Note that an empty rectangle is not valid: The isValid() function returns true if both width and height is larger than 0. A null rectangle (isNull() == true) on the other hand, has both width and height set to 0.

Note that due to the way QRect and QRectF are defined, an empty QRectF is defined in essentially the same way as QRect.

Finally, QRectF objects can be streamed as well as compared.

Rendering

When using an anti-aliased painter, the boundary line of a QRectF will be rendered symmetrically 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 pixels wide pen the boundary line will be split in the middle by the mathematical rectangle. This will be the case whenever the pen is set to 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.

Logical representationOne pixel wide pen
Two pixel wide penThree pixel wide pen

Coordinates

The QRectF class provides a collection of functions that return the various rectangle coordinates, and enable manipulation of these. QRectF also provides functions to move the rectangle relative to the various coordinates.

For example: the bottom(), setBottom() and moveBottom() functions: bottom() returns the y-coordinate of the rectangle's bottom edge, setBottom() sets the bottom edge of the rectangle to the given y coordinate (it may change the height, but will never change the rectangle's top edge) and moveBottom() moves the entire rectangle vertically, leaving the rectangle's bottom edge at the given y coordinate and its size unchanged.

It is also possible to add offsets to this rectangle's coordinates using the adjust() function, as well as retrieve a new rectangle based on adjustments of the original one using the adjusted() function. If either of the width and height is negative, use the normalized() function to retrieve a rectangle where the corners are swapped.

In addition, QRectF provides the getCoords() function which extracts the position of the rectangle's top-left and bottom-right corner, and the getRect() function which extracts the rectangle's top-left corner, width and height. Use the setCoords() and setRect() function to manipulate the rectangle's coordinates and dimensions in one go.

See also QRect and QRegion.

Member Function Documentation

QRectF::QRectF()

Default constructs an instance of QRectF.

QRectF::QRectF(const QPointF &topleft, const QSizeF &size)

Default constructs an instance of QRectF.

QRectF::QRectF(const QPointF &topleft, const QPointF &bottomRight)

Default constructs an instance of QRectF.

QRectF::QRectF(qreal left, qreal top, qreal width, qreal height)

Default constructs an instance of QRectF.

QRectF::QRectF(const QRect &rect)

Default constructs an instance of QRectF.

void QRectF::adjust(qreal x1, qreal y1, qreal x2, qreal y2)

QRectF QRectF::adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const

qreal QRectF::bottom() const

See also setBottom().

QPointF QRectF::bottomLeft() const

See also setBottomLeft().

QPointF QRectF::bottomRight() const

See also setBottomRight().

QPointF QRectF::center() const

bool QRectF::contains(const QRectF &r) const

bool QRectF::contains(const QPointF &p) const

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

[static] QRectF QRectF::fromCGRect(CGRect rect)

Creates a QRectF from CGRect rect.

This function was introduced in Qt 5.8.

See also toCGRect().

void QRectF::getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const

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

qreal QRectF::height() const

See also setHeight().

QRectF QRectF::intersect(const QRectF &r) const

QRectF QRectF::intersected(const QRectF &other) const

bool QRectF::intersects(const QRectF &r) const

bool QRectF::isEmpty() const

bool QRectF::isNull() const

bool QRectF::isValid() const

qreal QRectF::left() const

See also setLeft().

QRectF QRectF::marginsAdded(const QMarginsF &margins) const

QRectF QRectF::marginsRemoved(const QMarginsF &margins) const

void QRectF::moveBottom(qreal pos)

void QRectF::moveBottomLeft(const QPointF &p)

void QRectF::moveBottomRight(const QPointF &p)

void QRectF::moveCenter(const QPointF &p)

void QRectF::moveLeft(qreal pos)

void QRectF::moveRight(qreal pos)

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

void QRectF::moveTo(const QPointF &p)

void QRectF::moveTop(qreal pos)

void QRectF::moveTopLeft(const QPointF &p)

void QRectF::moveTopRight(const QPointF &p)

QRectF QRectF::normalized() const

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

If width() < 0 the function swaps the left and right corners, and it swaps the top and bottom corners if height() < 0.

See also isValid() and isEmpty().

See also setRight().

void QRectF::setBottom(qreal pos)

See also bottom().

void QRectF::setBottomLeft(const QPointF &p)

See also bottomLeft().

void QRectF::setBottomRight(const QPointF &p)

See also bottomRight().

void QRectF::setCoords(qreal x1, qreal y1, qreal x2, qreal y2)

void QRectF::setHeight(qreal h)

See also height().

void QRectF::setLeft(qreal pos)

See also left().

void QRectF::setRect(qreal x, qreal y, qreal w, qreal h)

void QRectF::setRight(qreal pos)

See also right().

void QRectF::setSize(const QSizeF &s)

See also size().

void QRectF::setTop(qreal pos)

See also top().

void QRectF::setTopLeft(const QPointF &p)

See also topLeft().

void QRectF::setTopRight(const QPointF &p)

See also topRight().

void QRectF::setWidth(qreal w)

See also width().

void QRectF::setX(qreal pos)

See also x().

void QRectF::setY(qreal pos)

See also y().

QSizeF QRectF::size() const

See also setSize().

QRect QRectF::toAlignedRect() const

CGRect QRectF::toCGRect() const

Creates a CGRect from a QRectF.

This function was introduced in Qt 5.8.

See also fromCGRect().

QRect QRectF::toRect() const

qreal QRectF::top() const

See also setTop().

QPointF QRectF::topLeft() const

See also setTopLeft().

QPointF QRectF::topRight() const

See also setTopRight().

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

void QRectF::translate(const QPointF &p)

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

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

QRectF QRectF::transposed() const

QRectF QRectF::unite(const QRectF &r) const

QRectF QRectF::united(const QRectF &other) const

qreal QRectF::width() const

See also setWidth().

qreal QRectF::x() const

See also setX().

qreal QRectF::y() const

See also setY().

QRectF QRectF::operator&(const QRectF &r) const

QRectF &QRectF::operator&=(const QRectF &r)

QRectF &QRectF::operator+=(const QMarginsF &margins)

QRectF &QRectF::operator-=(const QMarginsF &margins)

QRectF QRectF::operator|(const QRectF &r) const

QRectF &QRectF::operator|=(const QRectF &r)

Related Non-Members

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

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

QRectF operator+(const QRectF &lhs, const QMarginsF &rhs)

Returns the lhs rectangle grown by the rhs margins.

This function was introduced in Qt 5.3.

QRectF operator+(const QMarginsF &lhs, const QRectF &rhs)

This is an overloaded function.

Returns the lhs rectangle grown by the rhs margins.

This function was introduced in Qt 5.3.

QRectF operator-(const QRectF &lhs, const QMarginsF &rhs)

Returns the lhs rectangle grown by the rhs margins.

This function was introduced in Qt 5.3.

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

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

See also Serializing Qt Data Types.

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

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

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

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

See also Serializing Qt Data Types.