
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQRectF class defines a rectangle in the plane using floating point precision.More...
| Header: | #include <QRectF> |
| QRectF() | |
| QRectF(const QPointF & topLeft, const QSizeF & size) | |
| QRectF(const QPointF & topLeft, const QPointF & bottomRight) | |
| QRectF(qreal x, qreal y, qreal width, qreal height) | |
| QRectF(const QRect & rectangle) | |
| void | adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2) |
| QRectF | adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const |
| qreal | bottom() const |
| QPointF | bottomLeft() const |
| QPointF | bottomRight() const |
| QPointF | center() const |
| bool | contains(const QPointF & point) const |
| bool | contains(qreal x, qreal y) const |
| bool | contains(const QRectF & rectangle) const |
| void | getCoords(qreal * x1, qreal * y1, qreal * x2, qreal * y2) const |
| void | getRect(qreal * x, qreal * y, qreal * width, qreal * height) const |
| qreal | height() const |
| QRectF | intersected(const QRectF & rectangle) const |
| bool | intersects(const QRectF & rectangle) const |
| bool | isEmpty() const |
| bool | isNull() const |
| bool | isValid() const |
| qreal | left() const |
| void | moveBottom(qreal y) |
| void | moveBottomLeft(const QPointF & position) |
| void | moveBottomRight(const QPointF & position) |
| void | moveCenter(const QPointF & position) |
| void | moveLeft(qreal x) |
| void | moveRight(qreal x) |
| void | moveTo(qreal x, qreal y) |
| void | moveTo(const QPointF & position) |
| void | moveTop(qreal y) |
| void | moveTopLeft(const QPointF & position) |
| void | moveTopRight(const QPointF & position) |
| QRectF | normalized() const |
| qreal | right() const |
| void | setBottom(qreal y) |
| void | setBottomLeft(const QPointF & position) |
| void | setBottomRight(const QPointF & position) |
| void | setCoords(qreal x1, qreal y1, qreal x2, qreal y2) |
| void | setHeight(qreal height) |
| void | setLeft(qreal x) |
| void | setRect(qreal x, qreal y, qreal width, qreal height) |
| void | setRight(qreal x) |
| void | setSize(const QSizeF & size) |
| void | setTop(qreal y) |
| void | setTopLeft(const QPointF & position) |
| void | setTopRight(const QPointF & position) |
| void | setWidth(qreal width) |
| void | setX(qreal x) |
| void | setY(qreal y) |
| QSizeF | size() const |
| QRect | toAlignedRect() const |
| QRect | toRect() const |
| qreal | top() const |
| QPointF | topLeft() const |
| QPointF | topRight() const |
| void | translate(qreal dx, qreal dy) |
| void | translate(const QPointF & offset) |
| QRectF | translated(qreal dx, qreal dy) const |
| QRectF | translated(const QPointF & offset) const |
| QRectF | united(const QRectF & rectangle) const |
| qreal | width() const |
| qreal | x() const |
| qreal | y() const |
| QRectF | operator&(const QRectF & rectangle) const |
| QRectF & | operator&=(const QRectF & rectangle) |
| QRectF | operator|(const QRectF & rectangle) const |
| QRectF & | operator|=(const QRectF & rectangle) |
| 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) |
TheQRectF class defines a rectangle in the plane using floating point precision.
A rectangle is normally expressed as an upper-left corner and a size. The size (width and height) of aQRectF is always equivalent to the mathematical rectangle that forms the basis for its rendering.
AQRectF can be constructed with a set of left, top, width and height integers, or from aQPoint and aQSize. The following code creates two identical rectangles.
There is also a third constructor creating aQRectF from aQRect, and a correspondingtoRect() function that returns aQRect object based on the values of this rectangle (note that the coordinates in the returned rectangle are rounded to the nearest integer).
TheQRectF class provides a collection of functions that return the various rectangle coordinates, and enable manipulation of these.QRectF also provide functions to move the rectangle relative to the various coordinates. In addition there is amoveTo() function that moves the rectangle, leaving its top left corner at the given coordinates. Alternatively, thetranslate() function moves the rectangle the given offset relative to the current position, and thetranslated() function returns a translated copy of this rectangle.
Thesize() function returns the rectange's dimensions as aQSize. The dimensions can also be retrieved separately using thewidth() andheight() functions. To manipulate the dimensions use thesetSize(),setWidth() orsetHeight() functions. Alternatively, the size can be changed by applying either of the functions setting the rectangle coordinates, for example,setBottom() orsetRight().
Thecontains() function tells whether a given point is inside the rectangle or not, and theintersects() function returns true if this rectangle intersects with a given rectangle (otherwise false). TheQRectF class also provides theintersected() function which returns the intersection rectangle, and theunited() function which returns the rectangle that encloses the given rectangle and this:
TheisEmpty() 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: TheisValid() 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 wayQRect andQRectF are defined, an emptyQRectF is defined in essentially the same way asQRect.
Finally,QRectF objects can be streamed as well as compared.
When using ananti-aliased painter, the boundary line of aQRectF 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 theQRectF'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 representation | One pixel wide pen |
![]() | ![]() |
| Two pixel wide pen | Three pixel wide pen |
TheQRectF class provides a collection of functions that return the various rectangle coordinates, and enable manipulation of these.QRectF also provide functions to move the rectangle relative to the various coordinates.
For example: thebottom(),setBottom() andmoveBottom() 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) andmoveBottom() 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 theadjust() function, as well as retrieve a new rectangle based on adjustments of the original one using theadjusted() function. If either of the width and height is negative, use thenormalized() function to retrieve a rectangle where the corners are swapped.
In addition,QRectF provides thegetCoords() function which extracts the position of the rectangle's top-left and bottom-right corner, and thegetRect() function which extracts the rectangle's top-left corner, width and height. Use thesetCoords() andsetRect() function to manipulate the rectangle's coordinates and dimensions in one go.
Constructs a null rectangle.
See alsoisNull().
Constructs a rectangle with the giventopLeft corner and the givensize.
See alsosetTopLeft() andsetSize().
Constructs a rectangle with the giventopLeft andbottomRight corners.
This function was introduced in Qt 4.3.
See alsosetTopLeft() andsetBottomRight().
Constructs a rectangle with (x,y) as its top-left corner and the givenwidth andheight.
See alsosetRect().
Constructs aQRectF rectangle from the givenQRectrectangle.
See alsotoRect().
Addsdx1,dy1,dx2 anddy2 respectively to the existing coordinates of the rectangle.
See alsoadjusted() andsetRect().
Returns a new rectangle withdx1,dy1,dx2 anddy2 added respectively to the existing coordinates of this rectangle.
See alsoadjust().
Returns the y-coordinate of the rectangle's bottom edge.
See alsosetBottom(),bottomLeft(), andbottomRight().
Returns the position of the rectangle's bottom-left corner.
See alsosetBottomLeft(),bottom(), andleft().
Returns the position of the rectangle's bottom-right corner.
See alsosetBottomRight(),bottom(), andright().
Returns the center point of the rectangle.
See alsomoveCenter().
Returns true if the givenpoint is inside or on the edge of the rectangle; otherwise returns false.
See alsointersects().
This is an overloaded function.
Returns true if the point (x,y) is inside or on the edge of the rectangle; otherwise returns false.
This is an overloaded function.
Returns true if the givenrectangle is inside this rectangle; otherwise returns false.
Extracts the position of the rectangle's top-left corner to *x1 and *y1, and the position of the bottom-right corner to *x2 and *y2.
See alsosetCoords() andgetRect().
Extracts the position of the rectangle's top-left corner to *x and *y, and its dimensions to *width and *height.
See alsosetRect() andgetCoords().
Returns the height of the rectangle.
See alsosetHeight(),width(), andsize().
Returns the intersection of this rectangle and the givenrectangle. Note thatr.intersected(s) is equivalent tor & s.

This function was introduced in Qt 4.2.
See alsointersects(),united(), andoperator&=().
Returns true if this rectangle intersects with the givenrectangle (i.e. there is a non-empty area of overlap between them), otherwise returns false.
The intersection rectangle can be retrieved using theintersected() function.
See alsocontains().
Returns true if the rectangle is empty, otherwise returns false.
An empty rectangle haswidth() <= 0 orheight() <= 0. An empty rectangle is not valid (i.e., isEmpty() == !isValid()).
Use thenormalized() function to retrieve a rectangle where the corners are swapped.
See alsoisNull(),isValid(), andnormalized().
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. A null rectangle is also empty, and hence not valid.
See alsoisEmpty() andisValid().
Returns true if the rectangle is valid, otherwise returns false.
A valid rectangle has awidth() > 0 andheight() > 0. Note that non-trivial operations like intersections are not defined for invalid rectangles. A valid rectangle is not empty (i.e., isValid() == !isEmpty()).
See alsoisNull(),isEmpty(), andnormalized().
Returns the x-coordinate of the rectangle's left edge. Equivalent tox().
See alsosetLeft(),topLeft(), andbottomLeft().
Moves the rectangle vertically, leaving the rectangle's bottom edge at the giveny coordinate. The rectangle's size is unchanged.
See alsobottom(),setBottom(), andmoveTop().
Moves the rectangle, leaving the bottom-left corner at the givenposition. The rectangle's size is unchanged.
See alsosetBottomLeft(),moveBottom(), andmoveLeft().
Moves the rectangle, leaving the bottom-right corner at the givenposition. The rectangle's size is unchanged.
See alsosetBottomRight(),moveBottom(), andmoveRight().
Moves the rectangle, leaving the center point at the givenposition. The rectangle's size is unchanged.
See alsocenter().
Moves the rectangle horizontally, leaving the rectangle's left edge at the givenx coordinate. The rectangle's size is unchanged.
See alsoleft(),setLeft(), andmoveRight().
Moves the rectangle horizontally, leaving the rectangle's right edge at the givenx coordinate. The rectangle's size is unchanged.
See alsoright(),setRight(), andmoveLeft().
Moves the rectangle, leaving the top-left corner at the given position (x,y). The rectangle's size is unchanged.
See alsotranslate() andmoveTopLeft().
This is an overloaded function.
Moves the rectangle, leaving the top-left corner at the givenposition.
Moves the rectangle vertically, leaving the rectangle's top line at the giveny coordinate. The rectangle's size is unchanged.
See alsotop(),setTop(), andmoveBottom().
Moves the rectangle, leaving the top-left corner at the givenposition. The rectangle's size is unchanged.
See alsosetTopLeft(),moveTop(), andmoveLeft().
Moves the rectangle, leaving the top-right corner at the givenposition. The rectangle's size is unchanged.
See alsosetTopRight(),moveTop(), andmoveRight().
Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height.
Ifwidth() < 0 the function swaps the left and right corners, and it swaps the top and bottom corners ifheight() < 0.
See alsoisValid() andisEmpty().
Returns the x-coordinate of the rectangle's right edge.
See alsosetRight(),topRight(), andbottomRight().
Sets the bottom edge of the rectangle to the giveny coordinate. May change the height, but will never change the top edge of the rectangle.
See alsobottom() andmoveBottom().
Set the bottom-left corner of the rectangle to the givenposition. May change the size, but will never change the top-right corner of the rectangle.
See alsobottomLeft() andmoveBottomLeft().
Set the bottom-right corner of the rectangle to the givenposition. May change the size, but will never change the top-left corner of the rectangle.
See alsobottomRight() andmoveBottomRight().
Sets the coordinates of the rectangle's top-left corner to (x1,y1), and the coordinates of its bottom-right corner to (x2,y2).
See alsogetCoords() andsetRect().
Sets the height of the rectangle to the givenheight. The bottom edge is changed, but not the top one.
See alsoheight() andsetSize().
Sets the left edge of the rectangle to the givenx coordinate. May change the width, but will never change the right edge of the rectangle.
Equivalent tosetX().
Sets the coordinates of the rectangle's top-left corner to (x,y), and its size to the givenwidth andheight.
See alsogetRect() andsetCoords().
Sets the right edge of the rectangle to the givenx coordinate. May change the width, but will never change the left edge of the rectangle.
See alsoright() andmoveRight().
Sets the size of the rectangle to the givensize. The top-left corner is not moved.
See alsosize(),setWidth(), andsetHeight().
Sets the top edge of the rectangle to the giveny coordinate. May change the height, but will never change the bottom edge of the rectangle.
Equivalent tosetY().
Set the top-left corner of the rectangle to the givenposition. May change the size, but will never change the bottom-right corner of the rectangle.
See alsotopLeft() andmoveTopLeft().
Set the top-right corner of the rectangle to the givenposition. May change the size, but will never change the bottom-left corner of the rectangle.
See alsotopRight() andmoveTopRight().
Sets the width of the rectangle to the givenwidth. The right edge is changed, but not the left one.
Sets the left edge of the rectangle to the givenx coordinate. May change the width, but will never change the right edge of the rectangle.
Equivalent tosetLeft().
See alsox(),setY(), andsetTopLeft().
Sets the top edge of the rectangle to the giveny coordinate. May change the height, but will never change the bottom edge of the rectangle.
Equivalent tosetTop().
See alsoy(),setX(), andsetTopLeft().
Returns the size of the rectangle.
See alsosetSize(),width(), andheight().
Returns aQRect based on the values of this rectangle that is the smallest possible integer rectangle that completely contains this rectangle.
This function was introduced in Qt 4.3.
See alsotoRect().
Returns aQRect based on the values of this rectangle. Note that the coordinates in the returned rectangle are rounded to the nearest integer.
See alsoQRectF() andtoAlignedRect().
Returns the y-coordinate of the rectangle's top edge. Equivalent toy().
See alsosetTop(),topLeft(), andtopRight().
Returns the position of the rectangle's top-left corner.
See alsosetTopLeft(),top(), andleft().
Returns the position of the rectangle's top-right corner.
See alsosetTopRight(),top(), andright().
Moves the rectangledx along the x-axis anddy along the y-axis, relative to the current position. Positive values move the rectangle to the right and downwards.
See alsomoveTopLeft(),moveTo(), andtranslated().
This is an overloaded function.
Moves the rectangleoffset.x() along the x axis andoffset.y() along the y axis, relative to the current position.
Returns a copy of the rectangle that is translateddx along the x axis anddy along the y axis, relative to the current position. Positive values move the rectangle to the right and down.
See alsotranslate().
This is an overloaded function.
Returns a copy of the rectangle that is translatedoffset.x() along the x axis andoffset.y() along the y axis, relative to the current position.
Returns the bounding rectangle of this rectangle and the givenrectangle.

This function was introduced in Qt 4.2.
See alsointersected().
Returns the width of the rectangle.
See alsosetWidth(),height(), andsize().
Returns the x-coordinate of the rectangle's left edge. Equivalent toleft().
See alsosetX(),y(), andtopLeft().
Returns the y-coordinate of the rectangle's top edge. Equivalent totop().
See alsosetY(),x(), andtopLeft().
Returns the intersection of this rectangle and the givenrectangle. Returns an empty rectangle if there is no intersection.
See alsooperator&=() andintersected().
Intersects this rectangle with the givenrectangle.
See alsointersected() andoperator|=().
Returns the bounding rectangle of this rectangle and the givenrectangle.
See alsounited() andoperator|=().
Unites this rectangle with the givenrectangle.
Returns true if the rectanglesr1 andr2 are different, otherwise returns false.
Writes therectangle to thestream, and returns a reference to the stream.
See alsoSerializing Qt Data Types.
Returns true if the rectanglesr1 andr2 are equal, otherwise returns false.
Reads arectangle from thestream, and returns a reference to the stream.
See alsoSerializing Qt Data Types.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.