Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QRect Class

TheQRect class defines a rectangle in the plane using integer precision.More...

Header:#include <QRect>

Public Functions

QRect()
QRect(const QPoint & topLeft, const QPoint & bottomRight)
QRect(const QPoint & topLeft, const QSize & size)
QRect(int x, int y, int width, int height)
voidadjust(int dx1, int dy1, int dx2, int dy2)
QRectadjusted(int dx1, int dy1, int dx2, int dy2) const
intbottom() const
QPointbottomLeft() const
QPointbottomRight() const
QPointcenter() const
boolcontains(const QPoint & point, bool proper = false) const
boolcontains(int x, int y, bool proper) const
boolcontains(int x, int y) const
boolcontains(const QRect & rectangle, bool proper = false) const
voidgetCoords(int * x1, int * y1, int * x2, int * y2) const
voidgetRect(int * x, int * y, int * width, int * height) const
intheight() const
QRectintersected(const QRect & rectangle) const
boolintersects(const QRect & rectangle) const
boolisEmpty() const
boolisNull() const
boolisValid() const
intleft() const
voidmoveBottom(int y)
voidmoveBottomLeft(const QPoint & position)
voidmoveBottomRight(const QPoint & position)
voidmoveCenter(const QPoint & position)
voidmoveLeft(int x)
voidmoveRight(int x)
voidmoveTo(int x, int y)
voidmoveTo(const QPoint & position)
voidmoveTop(int y)
voidmoveTopLeft(const QPoint & position)
voidmoveTopRight(const QPoint & position)
QRectnormalized() const
intright() const
voidsetBottom(int y)
voidsetBottomLeft(const QPoint & position)
voidsetBottomRight(const QPoint & position)
voidsetCoords(int x1, int y1, int x2, int y2)
voidsetHeight(int height)
voidsetLeft(int x)
voidsetRect(int x, int y, int width, int height)
voidsetRight(int x)
voidsetSize(const QSize & size)
voidsetTop(int y)
voidsetTopLeft(const QPoint & position)
voidsetTopRight(const QPoint & position)
voidsetWidth(int width)
voidsetX(int x)
voidsetY(int y)
QSizesize() const
inttop() const
QPointtopLeft() const
QPointtopRight() const
voidtranslate(int dx, int dy)
voidtranslate(const QPoint & offset)
QRecttranslated(int dx, int dy) const
QRecttranslated(const QPoint & offset) const
QRectunited(const QRect & rectangle) const
intwidth() const
intx() const
inty() const
QRectoperator&(const QRect & rectangle) const
QRect &operator&=(const QRect & rectangle)
QRectoperator|(const QRect & rectangle) const
QRect &operator|=(const QRect & rectangle)

Related Non-Members

booloperator!=(const QRect & r1, const QRect & r2)
QDataStream &operator<<(QDataStream & stream, const QRect & rectangle)
booloperator==(const QRect & r1, const QRect & r2)
QDataStream &operator>>(QDataStream & stream, QRect & rectangle)

Detailed Description

TheQRect class defines a rectangle in the plane using integer precision.

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

AQRect 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.

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

There is a third constructor that creates aQRect using the top-left and bottom-right coordinates, but we recommend that you avoid using it. The rationale is that for historical reasons the values returned by thebottom() andright() functions deviate from the true bottom-right corner of the rectangle.

TheQRect class provides a collection of functions that return the various rectangle coordinates, and enable manipulation of these.QRect 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. TheQRect 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 ifleft() >right() ortop() >bottom(). Note that an empty rectangle is not valid: TheisValid() function returns true ifleft() <=right()andtop() <=bottom(). 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 emptyQRect is defined in essentially the same way asQRectF.

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

Rendering

When using ananti-aliased painter, the boundary line of aQRect 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 theQRect'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

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

For example theleft(),setLeft() andmoveLeft() functions as an example:left() returns the x-coordinate of the rectangle's left edge,setLeft() sets the left edge of the rectangle to the given x coordinate (it may change the width, but will never change the rectangle's right edge) andmoveLeft() moves the entire rectangle horizontally, leaving the rectangle's left edge at the given x coordinate and its size unchanged.

Note that for historical reasons the values returned by thebottom() andright() functions deviate from the true bottom-right corner of the rectangle: Theright() function returnsleft() +width() - 1 and thebottom() function returnstop() +height() - 1. The same is the case for the point returned by thebottomRight() convenience function. In addition, the x and y coordinate of thetopRight() andbottomLeft() functions, respectively, contain the same deviation from the true right and bottom edges.

We recommend that you usex() +width() andy() +height() to find the true bottom-right corner, and avoidright() andbottom(). Another solution is to useQRectF: TheQRectF class defines a rectangle in the plane using floating point accuracy for coordinates, and theQRectF::right() andQRectF::bottom() functionsdo return the right and bottom coordinates.

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,QRect 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.

See alsoQRectF andQRegion.

Member Function Documentation

QRect::QRect()

Constructs a null rectangle.

See alsoisNull().

QRect::QRect(constQPoint & topLeft, constQPoint & bottomRight)

Constructs a rectangle with the giventopLeft andbottomRight corners.

See alsosetTopLeft() andsetBottomRight().

QRect::QRect(constQPoint & topLeft, constQSize & size)

Constructs a rectangle with the giventopLeft corner and the givensize.

See alsosetTopLeft() andsetSize().

QRect::QRect(int x,int y,int width,int height)

Constructs a rectangle with (x,y) as its top-left corner and the givenwidth andheight.

See alsosetRect().

void QRect::adjust(int dx1,int dy1,int dx2,int dy2)

Addsdx1,dy1,dx2 anddy2 respectively to the existing coordinates of the rectangle.

See alsoadjusted() andsetRect().

QRect QRect::adjusted(int dx1,int dy1,int dx2,int dy2) const

Returns a new rectangle withdx1,dy1,dx2 anddy2 added respectively to the existing coordinates of this rectangle.

See alsoadjust().

int QRect::bottom() const

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

Note that for historical reasons this function returnstop() +height() - 1; usey() +height() to retrieve the true y-coordinate.

See alsosetBottom(),bottomLeft(), andbottomRight().

QPoint QRect::bottomLeft() const

Returns the position of the rectangle's bottom-left corner. Note that for historical reasons this function returnsQPoint(left(),top() +height() - 1).

See alsosetBottomLeft(),bottom(), andleft().

QPoint QRect::bottomRight() const

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

Note that for historical reasons this function returnsQPoint(left() +width() -1,top() +height() - 1).

See alsosetBottomRight(),bottom(), andright().

QPoint QRect::center() const

Returns the center point of the rectangle.

See alsomoveCenter().

bool QRect::contains(constQPoint & point,bool proper = false) const

Returns true if the givenpoint is inside or on the edge of the rectangle, otherwise returns false. Ifproper is true, this function only returns true if the givenpoint isinside the rectangle (i.e., not on the edge).

See alsointersects().

bool QRect::contains(int x,int y,bool proper) const

This is an overloaded function.

Returns true if the point (x,y) is inside or on the edge of the rectangle, otherwise returns false. Ifproper is true, this function only returns true if the point is entirely inside the rectangle(not on the edge).

bool QRect::contains(int x,int y) const

This is an overloaded function.

Returns true if the point (x,y) is inside this rectangle, otherwise returns false.

bool QRect::contains(constQRect & rectangle,bool proper = false) const

This is an overloaded function.

Returns true if the givenrectangle is inside this rectangle. otherwise returns false. Ifproper is true, this function only returns true if therectangle is entirely inside this rectangle (not on the edge).

void QRect::getCoords(int * x1,int * y1,int * x2,int * y2) const

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().

void QRect::getRect(int * x,int * y,int * width,int * height) const

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

See alsosetRect() andgetCoords().

int QRect::height() const

Returns the height of the rectangle.

See alsosetHeight(),width(), andsize().

QRect QRect::intersected(constQRect & rectangle) const

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&=().

bool QRect::intersects(constQRect & rectangle) const

Returns true if this rectangle intersects with the givenrectangle (i.e., there is at least one pixel that is within both rectangles), otherwise returns false.

The intersection rectangle can be retrieved using theintersected() function.

See alsocontains().

bool QRect::isEmpty() const

Returns true if the rectangle is empty, otherwise returns false.

An empty rectangle has aleft() >right() ortop() >bottom(). 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().

bool QRect::isNull() const

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 (i.e.,right() ==left() - 1 andbottom() ==top() - 1). A null rectangle is also empty, and hence is not valid.

See alsoisEmpty() andisValid().

bool QRect::isValid() const

Returns true if the rectangle is valid, otherwise returns false.

A valid rectangle has aleft() <right() andtop() <bottom(). 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().

int QRect::left() const

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

See alsosetLeft(),topLeft(), andbottomLeft().

void QRect::moveBottom(int y)

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

See alsobottom(),setBottom(), andmoveTop().

void QRect::moveBottomLeft(constQPoint & position)

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

See alsosetBottomLeft(),moveBottom(), andmoveLeft().

void QRect::moveBottomRight(constQPoint & position)

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

See alsosetBottomRight(),moveRight(), andmoveBottom().

void QRect::moveCenter(constQPoint & position)

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

See alsocenter().

void QRect::moveLeft(int x)

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

See alsoleft(),setLeft(), andmoveRight().

void QRect::moveRight(int x)

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

See alsoright(),setRight(), andmoveLeft().

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 alsotranslate() andmoveTopLeft().

void QRect::moveTo(constQPoint & position)

Moves the rectangle, leaving the top-left corner at the givenposition.

void QRect::moveTop(int y)

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

See alsotop(),setTop(), andmoveBottom().

void QRect::moveTopLeft(constQPoint & position)

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

See alsosetTopLeft(),moveTop(), andmoveLeft().

void QRect::moveTopRight(constQPoint & position)

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

See alsosetTopRight(),moveTop(), andmoveRight().

QRect QRect::normalized() const

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().

int QRect::right() const

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

Note that for historical reasons this function returnsleft() +width() - 1; usex() +width() to retrieve the true x-coordinate.

See alsosetRight(),topRight(), andbottomRight().

void QRect::setBottom(int y)

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().

void QRect::setBottomLeft(constQPoint & position)

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().

void QRect::setBottomRight(constQPoint & position)

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().

void QRect::setCoords(int x1,int y1,int x2,int y2)

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 alsocoords(),getCoords(), andsetRect().

void QRect::setHeight(int height)

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

See alsoheight() andsetSize().

void QRect::setLeft(int x)

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().

See alsoleft() andmoveLeft().

void QRect::setRect(int x,int y,int width,int height)

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

See alsorect(),getRect(), andsetCoords().

void QRect::setRight(int x)

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().

void QRect::setSize(constQSize & size)

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

See alsosize(),setWidth(), andsetHeight().

void QRect::setTop(int y)

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().

See alsotop() andmoveTop().

void QRect::setTopLeft(constQPoint & position)

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().

void QRect::setTopRight(constQPoint & position)

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().

void QRect::setWidth(int width)

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

See alsowidth() andsetSize().

void QRect::setX(int x)

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().

void QRect::setY(int y)

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().

QSize QRect::size() const

Returns the size of the rectangle.

See alsosetSize(),width(), andheight().

int QRect::top() const

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

See alsosetTop(),topLeft(), andtopRight().

QPoint QRect::topLeft() const

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

See alsosetTopLeft(),top(), andleft().

QPoint QRect::topRight() const

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

Note that for historical reasons this function returnsQPoint(left() +width() -1,top()).

See alsosetTopRight(),top(), andright().

void QRect::translate(int dx,int dy)

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 down.

See alsomoveTopLeft(),moveTo(), andtranslated().

void QRect::translate(constQPoint & offset)

This is an overloaded function.

Moves the rectangleoffset.x() along the x axis andoffset.y() along the y axis, relative to the current position.

QRect QRect::translated(int dx,int dy) const

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().

QRect QRect::translated(constQPoint & offset) const

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.

QRect QRect::united(constQRect & rectangle) const

Returns the bounding rectangle of this rectangle and the givenrectangle.

This function was introduced in Qt 4.2.

See alsointersected().

int QRect::width() const

Returns the width of the rectangle.

See alsosetWidth(),height(), andsize().

int QRect::x() const

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

See alsosetX(),y(), andtopLeft().

int QRect::y() const

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

See alsosetY(),x(), andtopLeft().

QRect QRect::operator&(constQRect & rectangle) const

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

See alsooperator&=() andintersected().

QRect & QRect::operator&=(constQRect & rectangle)

Intersects this rectangle with the givenrectangle.

See alsointersected() andoperator&().

QRect QRect::operator|(constQRect & rectangle) const

Returns the bounding rectangle of this rectangle and the givenrectangle.

See alsooperator|=() andunited().

QRect & QRect::operator|=(constQRect & rectangle)

Unites this rectangle with the givenrectangle.

See alsounited() andoperator|().

Related Non-Members

booloperator!=(constQRect & r1, constQRect & r2)

Returns true if the rectanglesr1 andr2 are different, otherwise returns false.

QDataStream &operator<<(QDataStream & stream, constQRect & rectangle)

Writes the givenrectangle to the givenstream, and returns a reference to the stream.

See alsoSerializing Qt Data Types.

booloperator==(constQRect & r1, constQRect & r2)

Returns true if the rectanglesr1 andr2 are equal, otherwise returns false.

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

Reads a rectangle from the givenstream into the givenrectangle, 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.


[8]ページ先頭

©2009-2025 Movatter.jp