Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2087640
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2010-07-08 13:49:15

[ module]

The QRect class defines a rectangle in the plane using integer precision.

 #include 

Public Functions

()

( const QPoint & topLeft, const QPoint & bottomRight )

( const QPoint & topLeft, const QSize & size )

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

void
( int dx1, int dy1, int dx2, int dy2 )

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

int
() const

QPoint
() const

QPoint
() const

QPoint
() const

bool
( const QPoint & point, bool proper = false ) const

bool
( int x, int y, bool proper ) const

bool
( int x, int y ) const

bool
( const QRect & rectangle, bool proper = false ) const

void
( int * x1, int * y1, int * x2, int * y2 ) const

void
( int * x, int * y, int * width, int * height ) const

int
() const

QRect
( const QRect & rectangle ) const

bool
( const QRect & rectangle ) const

bool
() const

bool
() const

bool
() const

int
() const

void
( int y )

void
( const QPoint & position )

void
( const QPoint & position )

void
( const QPoint & position )

void
( int x )

void
( int x )

void
( int x, int y )

void
( const QPoint & position )

void
( int y )

void
( const QPoint & position )

void
( const QPoint & position )

QRect
() const

int
() const

void
( int y )

void
( const QPoint & position )

void
( const QPoint & position )

void
( int x1, int y1, int x2, int y2 )

void
( int height )

void
( int x )

void
( int x, int y, int width, int height )

void
( int x )

void
( const QSize & size )

void
( int y )

void
( const QPoint & position )

void
( const QPoint & position )

void
( int width )

void
( int x )

void
( int y )

QSize
() const

int
() const

QPoint
() const

QPoint
() const

void
( int dx, int dy )

void
( const QPoint & offset )

QRect
( int dx, int dy ) const

QRect
( const QPoint & offset ) const

QRect
( const QRect & rectangle ) const

int
() const

int
() const

int
() const

QRect
( const QRect & rectangle ) const

QRect &
( const QRect & rectangle )

QRect
( const QRect & rectangle ) const

QRect &
( const QRect & rectangle )


Related Non-Members

bool
( const QRect & r1, const QRect & r2 )

QDataStream &
( QDataStream & stream, const QRect & rectangle )

bool
( const QRect & r1, const QRect & r2 )

QDataStream &
( QDataStream & stream, QRect & rectangle )


Detailed Description

The QRect 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 a QRect is always equivalent to the mathematical rectangle that forms the basis for its rendering.

A QRect can be constructed with a set of left, top, width and height integers, or from a and a . 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 a QRect 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 the () and () functions deviate from the true bottom-right corner of the rectangle.

The QRect 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 a () function that moves the rectangle, leaving its top left corner at the given coordinates. Alternatively, the () function moves the rectangle the given offset relative to the current position, and the () function returns a translated copy of this rectangle.

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

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


image image

()                                     ()

The () function returns true if () > () or () > (). Note that an empty rectangle is not valid: The () function returns true if () <= () and () <= (). A null rectangle (() == true) on the other hand, has both width and height set to 0.

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

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

Rendering

When using an painter, the boundary line of a QRect 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 QRect'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.

image image  

Logical representation                  One pixel wide pen

image image

Two pixel wide pen                       Three pixel wide pen

Coordinates

The QRect 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 the (), () and () functions as an example: () returns the x-coordinate of the rectangle's left edge, () 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) and () moves the entire rectangle horizontally, leaving the rectangle's left edge at the given x coordinate and its size unchanged.

image

Note that for historical reasons the values returned by the () and () functions deviate from the true bottom-right corner of the rectangle: The () function returns () + () - 1 and the () function returns () + () - 1. The same is the case for the point returned by the () convenience function. In addition, the x and y coordinate of the () and () functions, respectively, contain the same deviation from the true right and bottom edges.

We recommend that you use () + () and () + () to find the true bottom-right corner, and avoid () and (). Another solution is to use : The class defines a rectangle in the plane using floating point accuracy for coordinates, and the () and () functions do return the right and bottom coordinates.

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

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

See also and .


Member Function Documentation

QRect::QRect ()

Constructs a null rectangle.

See also ().

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

Constructs a rectangle with the given topLeft and bottomRight corners.

See also () and ().

QRect::QRect ( const & topLeft, const & size )

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

See also () and ().

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

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

See also ().

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

Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.

See also () and ().

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

Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of this rectangle.

See also ().

int QRect::bottom () const

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

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

See also (), (), and ().

QRect::bottomLeft () const

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

See also (), (), and ().

QRect::bottomRight () const

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

Note that for historical reasons this function returns (() + () -1, () + () - 1).

See also (), (), and ().

QRect::center () const

Returns the center point of the rectangle.

See also ().

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

Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false. If proper is true, this function only returns true if the given point is inside the rectangle (i.e., not on the edge).

See also ().

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. If proper 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 ( const QRect & rectangle, bool proper = false ) const

This is an overloaded function.

Returns true if the given rectangle is inside this rectangle. otherwise returns false. If proper is true, this function only returns true if the rectangle 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 also () and ().

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

int QRect::height () const

Returns the height of the rectangle.

See also (), (), and ().

QRect QRect::intersected ( const QRect & rectangle ) const

Returns the intersection of this rectangle and the given rectangle. Note that r.intersected(s) is equivalent to r & s.

image

This function was introduced in Qt 4.2.

See also (), (), and ().

bool QRect::intersects ( const QRect & rectangle ) const

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

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

See also ().

bool QRect::isEmpty () const

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

An empty rectangle has a () > () or () > (). An empty rectangle is not valid (i.e., isEmpty() == !()).

Use the () function to retrieve a rectangle where the corners are swapped.

See also (), (), and ().

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

See also () and ().

bool QRect::isValid () const

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

A valid rectangle has a () < () and () < (). Note that non-trivial operations like intersections are not defined for invalid rectangles. A valid rectangle is not empty (i.e., isValid() == !()).

See also (), (), and ().

int QRect::left () const

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

See also (), (), and ().

void QRect::moveBottom ( int y )

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

See also (), (), and ().

void QRect::moveBottomLeft ( const & position )

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

See also (), (), and ().

void QRect::moveBottomRight ( const & position )

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

See also (), (), and ().

void QRect::moveCenter ( const & position )

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

See also ().

void QRect::moveLeft ( int x )

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

See also (), (), and ().

void QRect::moveRight ( int x )

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

See also (), (), and ().

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

void QRect::moveTo ( const & position )

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

void QRect::moveTop ( int y )

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

See also (), (), and ().

void QRect::moveTopLeft ( const & position )

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

See also (), (), and ().

void QRect::moveTopRight ( const & position )

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

See also (), (), and ().

QRect QRect::normalized () const

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

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

See also () and ().

int QRect::right () const

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

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

See also (), (), and ().

void QRect::setBottom ( int y )

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

See also () and ().

void QRect::setBottomLeft ( const & position )

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

See also () and ().

void QRect::setBottomRight ( const & position )

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

See also () and ().

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 also (), (), and ().

void QRect::setHeight ( int height )

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

See also () and ().

void QRect::setLeft ( int x )

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

Equivalent to ().

See also () and ().

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 given width and height.

See also (), (), and ().

void QRect::setRight ( int x )

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

See also () and ().

void QRect::setSize ( const & size )

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

See also (), (), and ().

void QRect::setTop ( int y )

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

Equivalent to ().

See also () and ().

void QRect::setTopLeft ( const & position )

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

See also () and ().

void QRect::setTopRight ( const & position )

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

See also () and ().

void QRect::setWidth ( int width )

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

See also () and ().

void QRect::setX ( int x )

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

Equivalent to ().

See also (), (), and ().

void QRect::setY ( int y )

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

Equivalent to ().

See also (), (), and ().

QRect::size () const

Returns the size of the rectangle.

See also (), (), and ().

int QRect::top () const

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

See also (), (), and ().

QRect::topLeft () const

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

See also (), (), and ().

QRect::topRight () const

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

Note that for historical reasons this function returns (() + () -1, ()).

See also (), (), and ().

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

Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position. Positive values move the rectangle to the right and down.

See also (), (), and ().

void QRect::translate ( const & offset )

This is an overloaded function.

Moves the rectangle offset. along the x axis and offset. 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 translated dx along the x axis and dy along the y axis, relative to the current position. Positive values move the rectangle to the right and down.

See also ().

QRect QRect::translated ( const & offset ) const

This is an overloaded function.

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

QRect QRect::united ( const QRect & rectangle ) const

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

image

This function was introduced in Qt 4.2.

See also ().

int QRect::width () const

Returns the width of the rectangle.

See also (), (), and ().

int QRect::x () const

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

See also (), (), and ().

int QRect::y () const

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

See also (), (), and ().

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

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

See also () and ().

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

Intersects this rectangle with the given rectangle.

See also () and ().

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

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

See also () and ().

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

Unites this rectangle with the given rectangle.

See also () and ().


Related Non-Members

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

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

& operator<< ( & stream, const QRect & rectangle )

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

See also .

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

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

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

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

See also .

阅读(2072) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~