CopperSpice API  1.9.1
QRectF Class Reference

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

Public Methods

 QRectF ()
 
 QRectF (const QPointF &topLeft, const QPointF &bottomRight)
 
 QRectF (const QPointF &topLeft, const QSizeF &size)
 
 QRectF (const QRect &rect)
 
 QRectF (qreal left, qreal top, qreal width, qreal height)
 
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 QPointF &point) const
 
bool contains (const QRectF &rect) 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 *width, qreal *height) const
 
qreal height () const
 
QRectF intersected (const QRectF &rect) const
 
bool intersects (const QRectF &rect) 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 &point)
 
void moveBottomRight (const QPointF &point)
 
void moveCenter (const QPointF &point)
 
void moveLeft (qreal pos)
 
void moveRight (qreal pos)
 
void moveTo (const QPointF &point)
 
void moveTo (qreal x, qreal y)
 
void moveTop (qreal pos)
 
void moveTopLeft (const QPointF &point)
 
void moveTopRight (const QPointF &point)
 
QRectF normalized () const
 
QRectF operator& (const QRectF &rect) const
 
QRectF & operator&= (const QRectF &rect)
 
QRectF & operator+= (const QMarginsF &margins)
 
QRectF & operator-= (const QMarginsF &margins)
 
QRectF operator| (const QRectF &rect) const
 
QRectF & operator|= (const QRectF &rect)
 
qreal right () const
 
void setBottom (qreal pos)
 
void setBottomLeft (const QPointF &point)
 
void setBottomRight (const QPointF &point)
 
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 width, qreal height)
 
void setRight (qreal pos)
 
void setSize (const QSizeF &size)
 
void setTop (qreal pos)
 
void setTopLeft (const QPointF &point)
 
void setTopRight (const QPointF &point)
 
void setWidth (qreal w)
 
void setX (qreal xPos)
 
void setY (qreal yPos)
 
QSizeF size () const
 
QRect toAlignedRect () const
 
qreal top () const
 
QPointF topLeft () const
 
QPointF topRight () const
 
QRect toRect () const
 
void translate (const QPointF &offset)
 
void translate (qreal x, qreal y)
 
QRectF translated (const QPointF &offset) const
 
QRectF translated (qreal x, qreal y) const
 
QRectF united (const QRectF &rect) const
 
qreal width () const
 
qreal x () const
 
qreal y () const
 

Friends

bool operator!= (const QRectF &rect1, const QRectF &rect2)
 
bool operator== (const QRectF &rect1, const QRectF &rect2)
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &stream, const QRectF &rectF)
 
QDataStreamoperator>> (QDataStream &stream, QRectF &rectF)
 

Detailed Description

The QRectF 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 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 integers, or from a QPoint and a QSize. The following code creates two identical rectangles.

QRectF r1(100, 200, 11, 16);
QRectF r2(QPoint(100, 200), QSize(11, 16));

There is also a third constructor which creates a QRectF from a QRect and a corresponding toRect() method which returns a QRect object based on the values of this rectangle. 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 provide 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 rectangles dimensions as a QSize. 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() method 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() method which returns the intersection rectangle, and the united() function which returns the rectangle that encloses the given rectangle.

intersected()united()

The isEmpty() method returns true if the rectangle's width or height is less than or equal to, 0. An empty rectangle is not valid. The isValid() method returns true if both width and height is larger than 0. A null rectangle has both width and height set to 0. An empty QRectF is defined in essentially the same way as QRect. 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 provide 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, QRegion

Constructor & Destructor Documentation

QRectF::QRectF ( )
inline

Constructs a null rectangle.

See also
isNull()
QRectF::QRectF ( const QPointF topLeft,
const QSizeF size 
)
inline

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

See also
setTopLeft(), setSize()
QRectF::QRectF ( const QPointF topLeft,
const QPointF bottomRight 
)
inline

Constructs a rectangle with the given topLeft and bottomRight corners.

See also
setTopLeft(), setBottomRight()
QRectF::QRectF ( qreal  left,
qreal  top,
qreal  width,
qreal  height 
)
inline

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

See also
setRect()
QRectF::QRectF ( const QRect rect)
inline

Constructs a QRectF rectangle from the given QRect rectangle.

See also
toRect()

Method Documentation

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

Adds x1, y1, x2 and y2 respectively to the existing coordinates of the rectangle.

See also
adjusted(), setRect()
QRectF QRectF::adjusted ( qreal  x1,
qreal  y1,
qreal  x2,
qreal  y2 
) const
inline

Returns a new rectangle with x1, y1, x2 and y2 added respectively to the existing coordinates of this rectangle.

See also
adjust()
qreal QRectF::bottom ( ) const
inline

Returns the y-coordinate of the rectangle's bottom edge.

See also
setBottom(), bottomLeft(), bottomRight()
QPointF QRectF::bottomLeft ( ) const
inline

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

See also
setBottomLeft(), bottom(), left()
QPointF QRectF::bottomRight ( ) const
inline

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

See also
setBottomRight(), bottom(), right()
QPointF QRectF::center ( ) const
inline

Returns the center point of the rectangle.

See also
moveCenter()
bool QRectF::contains ( const QPointF point) const

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

See also
intersects()
bool QRectF::contains ( const QRectF &  rect) const

Returns true if the given rect is inside this rectangle, otherwise returns false.

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

Returns true if the point (x, y) is inside or on the edge of the rectangle, otherwise returns false.

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

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 also
setCoords(), getRect()
void QRectF::getRect ( qreal x,
qreal y,
qreal width,
qreal height 
) const
inline

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

See also
setRect(), getCoords()
qreal QRectF::height ( ) const
inline

Returns the height of the rectangle.

See also
setHeight(), width(), size()
QRectF QRectF::intersected ( const QRectF &  rect) const
inline

Returns the intersection of this rectangle and the given rect. Calling a.intersected(b) is equivalent to a & b.

See also
intersects(), united(), operator&=()
bool QRectF::intersects ( const QRectF &  rect) const

Returns true if this rectangle intersects with the given rect. This means there is a non-empty area of overlap between them. The intersection rectangle can be retrieved using intersected().

See also
contains()
bool QRectF::isEmpty ( ) const
inline

Returns true if the rectangle is empty, otherwise returns false. An empty rectangle has width() <= 0 or height() <= 0. An empty rectangle is not valid (i.e., isEmpty() == !isValid()). Use the normalized() function to retrieve a rectangle where the corners are swapped.

See also
isNull(), isValid(), normalized()
bool QRectF::isNull ( ) const
inline

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 also
isEmpty(), isValid()
bool QRectF::isValid ( ) const
inline

Returns true if the rectangle is valid, otherwise returns false. A valid rectangle has a width() > 0 and height() > 0. Non-trivial operations like intersections are not defined for invalid rectangles. A valid rectangle is not empty (i.e., isValid() == !isEmpty()).

See also
isNull(), isEmpty(), normalized()
qreal QRectF::left ( ) const
inline

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

See also
setLeft(), topLeft(), bottomLeft()
QRectF QRectF::marginsAdded ( const QMarginsF margins) const
inline

Returns a new rectangle grown by margins.

See also
marginsRemoved(), operator+=(), operator-=()
QRectF QRectF::marginsRemoved ( const QMarginsF margins) const
inline

Returns a new rectangle shrunk by margins

See also
marginsAdded(), operator+=(), operator-=()
void QRectF::moveBottom ( qreal  pos)
inline

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

See also
bottom(), setBottom(), moveTop()
void QRectF::moveBottomLeft ( const QPointF point)
inline

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

See also
setBottomLeft(), moveBottom(), moveLeft()
void QRectF::moveBottomRight ( const QPointF point)
inline

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

See also
setBottomRight(), moveBottom(), moveRight()
void QRectF::moveCenter ( const QPointF point)
inline

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

See also
center()
void QRectF::moveLeft ( qreal  pos)
inline

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

See also
left(), setLeft(), moveRight()
void QRectF::moveRight ( qreal  pos)
inline

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

See also
right(), setRight(), moveLeft()
void QRectF::moveTo ( const QPointF point)
inline

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

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

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

See also
translate(), moveTopLeft()
void QRectF::moveTop ( qreal  pos)
inline

Moves the rectangle vertically, leaving the rectangle's top line at the given pos coordinate. The rectangle's size is unchanged.

See also
top(), setTop(), moveBottom()
void QRectF::moveTopLeft ( const QPointF point)
inline

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

See also
setTopLeft(), moveTop(), moveLeft()
void QRectF::moveTopRight ( const QPointF point)
inline

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

See also
setTopRight(), moveTop(), moveRight()
QRectF QRectF::normalized ( ) const

Returns a normalized rectangle; i.e., a rectangle that has a non-negative width and height. If width() < 0 the method swaps the left and right corners, and it swaps the top and bottom corners if height() < 0.

See also
isValid(), isEmpty()
QRectF QRectF::operator& ( const QRectF &  rect) const

Returns the intersection of this rectangle and the given rect. Returns an empty rectangle if there is no intersection.

See also
operator&=(), intersected()
QRectF & QRectF::operator&= ( const QRectF &  rect)
inline

Intersects this rectangle with the given rect.

See also
intersected(), operator|=()
QRectF & QRectF::operator+= ( const QMarginsF margins)
inline

Grows this rectangle by margins.

See also
marginsAdded(), marginsRemoved(), operator-=()
QRectF & QRectF::operator-= ( const QMarginsF margins)
inline

Shrinks this rectangle by margins.

See also
marginsAdded(), marginsRemoved(), operator+=()
QRectF QRectF::operator| ( const QRectF &  rect) const

Returns the bounding rectangle of this rectangle and the given rect.

See also
united(), operator|=()
QRectF & QRectF::operator|= ( const QRectF &  rect)
inline

Unites this rectangle with the given rect.

See also
united(), operator|()
qreal QRectF::right ( ) const
inline

Returns the x-coordinate of the rectangle's right edge.

See also
setRight(), topRight(), bottomRight()
void QRectF::setBottom ( qreal  pos)
inline

Sets the bottom edge of the rectangle to the given pos coordinate. May change the height, but will never change the top edge of the rectangle.

See also
bottom(), moveBottom()
void QRectF::setBottomLeft ( const QPointF point)
inline

Set the bottom-left corner of the rectangle to the given point. May change the size, but will never change the top-right corner of the rectangle.

See also
bottomLeft(), moveBottomLeft()
void QRectF::setBottomRight ( const QPointF point)
inline

Set the bottom-right corner of the rectangle to the given point. May change the size, but will never change the top-left corner of the rectangle.

See also
bottomRight(), moveBottomRight()
void QRectF::setCoords ( qreal  x1,
qreal  y1,
qreal  x2,
qreal  y2 
)
inline

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 also
getCoords(), setRect()
void QRectF::setHeight ( qreal  h)
inline

Sets the height of the rectangle to the given h. The bottom edge is changed, but not the top one.

See also
height(), setSize()
void QRectF::setLeft ( qreal  pos)
inline

Sets the left edge of the rectangle to the given pos coordinate. May change the width, but will never change the right edge of the rectangle.

Equivalent to setX().

See also
left(), moveLeft()
void QRectF::setRect ( qreal  x,
qreal  y,
qreal  width,
qreal  height 
)
inline

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

See also
getRect(), setCoords()
void QRectF::setRight ( qreal  pos)
inline

Sets the right edge of the rectangle to the given pos coordinate. May change the width, but will never change the left edge of the rectangle.

See also
right(), moveRight()
void QRectF::setSize ( const QSizeF size)
inline

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

See also
size(), setWidth(), setHeight()
void QRectF::setTop ( qreal  pos)
inline

Sets the top edge of the rectangle to the given pos coordinate. May change the height, but will never change the bottom edge of the rectangle.

Equivalent to setY().

See also
top(), moveTop()
void QRectF::setTopLeft ( const QPointF point)
inline

Set the top-left corner of the rectangle to the given point. May change the size, but will never change the bottom-right corner of the rectangle.

See also
topLeft(), moveTopLeft()
void QRectF::setTopRight ( const QPointF point)
inline

Set the top-right corner of the rectangle to the given point. May change the size, but will never change the bottom-left corner of the rectangle.

See also
topRight(), moveTopRight()
void QRectF::setWidth ( qreal  w)
inline

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

See also
width(), setSize()
void QRectF::setX ( qreal  xPos)
inline

Sets the left edge of the rectangle to the given xPos coordinate. May change the width, but will never change the right edge of the rectangle.

Equivalent to setLeft().

See also
x(), setY(), setTopLeft()
void QRectF::setY ( qreal  yPos)
inline

Sets the top edge of the rectangle to the given yPos coordinate. May change the height, but will never change the bottom edge of the rectangle.

Equivalent to setTop().

See also
y(), setX(), setTopLeft()
QSizeF QRectF::size ( ) const
inline

Returns the size of the rectangle.

See also
setSize(), width(), height()
QRect QRectF::toAlignedRect ( ) const

Returns a QRect based on the values of this rectangle that is the smallest possible integer rectangle that completely contains this rectangle.

See also
toRect()
qreal QRectF::top ( ) const
inline

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

See also
setTop(), topLeft(), topRight()
QPointF QRectF::topLeft ( ) const
inline

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

See also
setTopLeft(), top(), left()
QPointF QRectF::topRight ( ) const
inline

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

See also
setTopRight(), top(), right()
QRect QRectF::toRect ( ) const
inline

Returns a QRect based on the values of this rectangle. The coordinates in the returned rectangle are rounded to the nearest integer.

See also
QRectF(), toAlignedRect()
void QRectF::translate ( const QPointF offset)
inline

Moves the rectangle offset.x() along the x axis and offset.y() along the y axis, relative to the current position.

void QRectF::translate ( qreal  x,
qreal  y 
)
inline

Moves the rectangle x along the x-axis and y along the y-axis, relative to the current position. Positive values move the rectangle to the right and downwards.

See also
moveTopLeft(), moveTo(), translated()
QRectF QRectF::translated ( const QPointF offset) const
inline

Returns a copy of the rectangle that is translated offset.x() along the x axis and offset.y() along the y axis, relative to the current position.

QRectF QRectF::translated ( qreal  x,
qreal  y 
) const
inline

Returns a copy of the rectangle that is translated x along the x axis and y along the y axis, relative to the current position. Positive values move the rectangle to the right and down.

See also
translate()
QRectF QRectF::united ( const QRectF &  rect) const
inline

Returns the bounding rectangle of this QRectF and the given rect.

See also
intersected()
qreal QRectF::width ( ) const
inline

Returns the width of the rectangle.

See also
setWidth(), height(), size()
qreal QRectF::x ( ) const
inline

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

See also
setX(), y(), topLeft()
qreal QRectF::y ( ) const
inline

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

See also
setY(), x(), topLeft()

Friends And Related Function Documentation

bool operator!= ( const QRectF &  rect1,
const QRectF &  rect2 
)
friend

Returns true if the rectangles rect1 and rect2 are different, otherwise returns false.

QDataStream & operator<< ( QDataStream stream,
const QRectF &  rectF 
)
related

Writes the given rectF to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

bool operator== ( const QRectF &  rect1,
const QRectF &  rect2 
)
friend

Returns true if the rectangles rect1 and rect2 are equal, otherwise returns false.

QDataStream & operator>> ( QDataStream stream,
QRectF &  rectF 
)
related

Reads from the stream into the given rectF. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.