QRect 類

The QRect 類使用整數精度定義平麵矩形。 更多...

頭: #include <QRect>
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共函數

QRect ()
QRect (const QPoint & topLeft , const QPoint & bottomRight )
QRect (const QPoint & topLeft , const QSize & size )
QRect (int x , int y , int width , int height )
void adjust (int dx1 , int dy1 , int dx2 , int dy2 )
QRect adjusted (int dx1 , int dy1 , int dx2 , int dy2 ) const
int bottom () const
QPoint bottomLeft () const
QPoint bottomRight () const
QPoint center () const
bool contains (const QPoint & point , bool proper = false) const
bool contains (const QRect & rectangle , bool proper = false) const
bool contains (int x , int y ) const
bool contains (int x , int y , bool proper ) const
void getCoords (int * x1 , int * y1 , int * x2 , int * y2 ) const
void getRect (int * x , int * y , int * width , int * height ) const
int height () const
QRect intersected (const QRect & rectangle ) const
bool intersects (const QRect & rectangle ) const
bool isEmpty () const
bool isNull () const
bool isValid () const
int left () const
QRect marginsAdded (const QMargins & margins ) const
QRect marginsRemoved (const QMargins & margins ) const
void moveBottom (int y )
void moveBottomLeft (const QPoint & position )
void moveBottomRight (const QPoint & position )
void moveCenter (const QPoint & position )
void moveLeft (int x )
void moveRight (int x )
void moveTo (int x , int y )
void moveTo (const QPoint & position )
void moveTop (int y )
void moveTopLeft (const QPoint & position )
void moveTopRight (const QPoint & position )
QRect normalized () const
int right () const
void setBottom (int y )
void setBottomLeft (const QPoint & position )
void setBottomRight (const QPoint & position )
void setCoords (int x1 , int y1 , int x2 , int y2 )
void setHeight (int height )
void setLeft (int x )
void setRect (int x , int y , int width , int height )
void setRight (int x )
void setSize (const QSize & size )
void setTop (int y )
void setTopLeft (const QPoint & position )
void setTopRight (const QPoint & position )
void setWidth (int width )
void setX (int x )
void setY (int y )
QSize size () const
CGRect toCGRect () const
int top () const
QPoint topLeft () const
QPoint topRight () const
void translate (int dx , int dy )
void translate (const QPoint & offset )
QRect translated (int dx , int dy ) const
QRect translated (const QPoint & offset ) const
QRect transposed () const
QRect united (const QRect & rectangle ) const
int width () const
int x () const
int y () const
QRect operator& (const QRect & rectangle ) const
QRect & operator&= (const QRect & rectangle )
QRect & operator+= (const QMargins & margins )
QRect & operator-= (const QMargins & margins )
QRect operator| (const QRect & rectangle ) const
QRect & operator|= (const QRect & rectangle )
bool operator!= (const QRect & r1 , const QRect & r2 )
QRect operator+ (const QRect & rectangle , const QMargins & margins )
QRect operator+ (const QMargins & margins , const QRect & rectangle )
QRect operator- (const QRect & lhs , const QMargins & rhs )
QDataStream & operator<< (QDataStream & stream , const QRect & rectangle )
bool operator== (const QRect & r1 , const QRect & r2 )
QDataStream & operator>> (QDataStream & stream , QRect & rectangle )

詳細描述

The QRect 類使用整數精度定義平麵矩形。

A rectangle is normally expressed as a top-left corner and a size. The size (width and height) of a QRect 始終相當於形成其渲染基礎的數學矩形。

A QRect 可以按左、上、寬度及高度的一組整數構造 QRect,或從 QPoint QSize 。以下代碼創建 2 個恒等矩形。

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

有第 3 個構造函數創建 QRect 使用左上和右下坐標,但推薦避免使用它。基本原理是齣於曆史原因,值返迴通過 bottom () 和 right () 函數會偏離矩形的真正右下角。

The QRect 類提供瞭返迴各種矩形坐標,並使之能夠操縱這些坐標的一批函數。 QRect also provides functions to move the rectangle relative to the various coordinates. In addition there is a moveTo () 函數能移動矩形,使其左上角留在給定坐標處。另外, translate () 函數相對當前位置將矩形移動給定偏移,和 translated () 函數返迴此矩形的翻譯副本。

The size () 函數返迴矩形的尺度按 QSize 。還可以單獨檢索尺度使用 width () 和 height () 函數。要操縱維度使用 setSize (), setWidth () 或 setHeight () 函數。另外,通過應用設置矩形坐標的函數可以改變大小,例如 setBottom () 或 setRight ().

The contains () 函數告訴給定點是否在矩形內,和 intersects () 函數返迴 true 若此矩形與給定矩形相交。 QRect 類還提供 intersected () 函數返迴交集矩形,和 united () 函數返迴包圍給定矩形的矩形,如下所示:

intersected () united ()

The isEmpty () 函數返迴 true if left () > right () 或 top () > bottom ()。注意,空矩形無效: isValid () 函數返迴 true if left () <= right () and top () <= bottom ()。null 矩形 ( isNull () == true) 另一方麵,有將寬度和高度兩者設為 0。

注意,由於方式有關 QRect and QRectF 的定義,空 QRect 的定義方式本質上如同 QRectF .

最後, QRect 對象可以被流化及比較。

渲染

當使用 抗鋸齒 描繪器,邊界綫對於 QRect 將對稱渲染在數學矩形邊界綫的 2 側。但當使用鋸齒描繪器 (默認) 時,應用其它規則。

那麼,當按 1 像素寬鋼筆渲染時 QRect 的邊界綫將渲染在數學矩形邊界綫的右側和下方。

當按 2 像素寬鋼筆渲染時,數學矩形將從中間分割邊界綫。將是這種情況每當將鋼筆設為偶數像素時,當以奇數像素鋼筆渲染時,將把備用像素渲染到數學矩形的右側和下方 (如 1 像素情況)。

邏輯錶示 1 像素寬鋼筆
2 像素寬鋼筆 3 像素寬鋼筆

坐標

The QRect 類提供瞭返迴各種矩形坐標,並使之能夠操縱這些坐標的一批函數。 QRect also provides functions to move the rectangle relative to the various coordinates.

例如 left (), setLeft () 和 moveLeft () 函數範例: left () 返迴矩形左邊緣的 X 坐標, setLeft () 將矩形左邊緣設為給定 X 坐標 (可能改變寬度,但從不改變矩形的右邊緣) 和 moveLeft () 水平移動整個矩形,使矩形左邊緣留在給定 X 坐標處且其大小保持不變。

注意,由於曆史原因,值返迴通過 bottom () 和 right () 函數會偏離矩形的真正右下角: right () 函數返迴 left () + width () - 1 bottom () 函數返迴 top () + height () - 1 。相同情況是點返迴通過 bottomRight () 方便函數。此外,X 和 Y 坐標對於 topRight () 和 bottomLeft () 函數,分彆包含來自真正右邊緣和底邊緣的相同偏離。

推薦使用 x () + width () 和 y () + height () 查找真正右下角,並避免 right () 和 bottom ()。另一解決方案是使用 QRectF QRectF 類使用浮點精度坐標定義平麵矩形,和 QRectF::right () 和 QRectF::bottom () 函數 do 返迴右坐標和下坐標。

也是可能的,將偏移添加到此矩形的坐標使用 adjust () 函數,及基於原始矩形的調節檢索新矩形是使用 adjusted () 函數。若寬度和高度兩者任一為負,使用 normalized () 函數以檢索角交換矩形。

此外, QRect 提供 getCoords () 函數提取矩形左上角和右下角的位置,和 getRect () 函數提取矩形的左上角、寬度和高度。使用 setCoords () 和 setRect () 函數能一次性操縱矩形的坐標和尺度。

約束

QRect 被限製到最小和最大值是由於 int 類型。操作 QRect 潛在可能導緻值超齣此範圍,會産生未定義行為。

另請參閱 QRectF and QRegion .

成員函數文檔編製

QRect:: QRect ()

構造 null 矩形。

另請參閱 isNull ().

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

構造矩形采用給定 topLeft and bottomRight 角。

另請參閱 setTopLeft () 和 setBottomRight ().

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

構造矩形采用給定 topLeft 角和給定 size .

另請參閱 setTopLeft () 和 setSize ().

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

構造矩形采用 ( x , y ) 作為其左上角和給定 width and height .

另請參閱 setRect ().

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

添加 dx1 , dy1 , dx2 and dy2 分彆到矩形的現有坐標。

另請參閱 adjusted () 和 setRect ().

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

返迴新的矩形采用 dx1 , dy1 , dx2 and dy2 分彆添加到此矩形的現有坐標。

另請參閱 adjust ().

int QRect:: bottom () const

返迴矩形底部邊緣的 Y 坐標。

注意:由於曆史原因,此函數返迴 top () + height () - 1;使用 y () + height () 來檢索為 true 的 Y 坐標。

另請參閱 setBottom (), bottomLeft (),和 bottomRight ().

QPoint QRect:: bottomLeft () const

返迴矩形的左下角位置。注意,由於曆史原因,此函數返迴 QPoint ( left (), top () + height () - 1).

另請參閱 setBottomLeft (), bottom (),和 left ().

QPoint QRect:: bottomRight () const

返迴矩形右下角的位置。

注意:由於曆史原因,此函數返迴 QPoint ( left () + width () -1, top () + height () - 1).

另請參閱 setBottomRight (), bottom (),和 right ().

QPoint QRect:: center () const

返迴矩形的中心點。

另請參閱 moveCenter ().

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

返迴 true 若給定 point 在矩形內 (或在矩形邊緣上),否則返迴 false 。若 proper 為 True,此函數纔返迴 true 若給定 point is inside 矩形 (即:不在邊緣)。

另請參閱 intersects ().

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

這是重載函數。

返迴 true 若給定 rectangle 在此矩形內。否則返迴 false 。若 proper 為 True,此函數纔返迴 true rectangle 完全在此矩形內 (不在邊緣上)。

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

這是重載函數。

返迴 true 若點 ( x , y ) 在此矩形內,否則返迴 false .

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

這是重載函數。

返迴 true 若點 ( x , y ) 在矩形內 (或在矩形邊緣上),否則返迴 false 。若 proper 為 True,此函數纔返迴 true 若點完全在矩形內 (不在邊緣上)。

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

將矩形左上角位置提取到 * x1 和 * y1 ,和右下角位置到 * x2 和 * y2 .

另請參閱 setCoords () 和 getRect ().

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

將矩形左上角位置提取到 * x 和 * y ,和其尺度到 * width 和 * height .

另請參閱 setRect () 和 getCoords ().

int QRect:: height () const

返迴矩形的高度。

另請參閱 setHeight (), width (),和 size ().

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

返迴交集為此矩形和給定 rectangle 。注意, r.intersected(s) 相當於 r & s .

該函數在 Qt 4.2 引入。

另請參閱 intersects (), united (),和 operator&= ().

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

返迴 true 若此矩形相交給定 rectangle (即:至少有一像素在 2 矩形內),否則返迴 false .

可以檢索交集矩形使用 intersected () 函數。

另請參閱 contains ().

bool QRect:: isEmpty () const

返迴 true 若矩形為空,否則返迴 false .

空矩形擁有 left () > right () 或 top () > bottom ()。空矩形無效 (即 isEmpty() == ! isValid ()).

使用 normalized () 函數以檢索角交換矩形。

另請參閱 isNull (), isValid (),和 normalized ().

bool QRect:: isNull () const

返迴 true 若矩形是 null 矩形,否則返迴 false .

null 矩形有將寬度和高度兩者設為 0 (即 right () == left () - 1 and bottom () == top () - 1)。null 矩形也為空,因此無效。

另請參閱 isEmpty () 和 isValid ().

bool QRect:: isValid () const

返迴 true 若矩形有效,否則返迴 false .

有效矩形擁有 left () <= right () 和 top () <= bottom ()。注意,非通俗操作 (像交集) 對於無效矩形未定義。有效矩形不為空 (即 isValid() == ! isEmpty ()).

另請參閱 isNull (), isEmpty (),和 normalized ().

int QRect:: left () const

返迴矩形左邊緣的 X 坐標。相當於 x ().

另請參閱 setLeft (), topLeft (),和 bottomLeft ().

QRect QRect:: marginsAdded (const QMargins & margins ) const

返迴矩形增長按 margins .

該函數在 Qt 5.1 引入。

另請參閱 operator+= (), marginsRemoved (),和 operator-= ().

QRect QRect:: marginsRemoved (const QMargins & margins ) const

移除 margins 從矩形,收縮它。

該函數在 Qt 5.1 引入。

另請參閱 marginsAdded (), operator+= (),和 operator-= ().

void QRect:: moveBottom ( int y )

垂直移動矩形,使矩形底邊綫留在給定 y 坐標。矩形的大小保持不變。

另請參閱 bottom (), setBottom (),和 moveTop ().

void QRect:: moveBottomLeft (const QPoint & position )

移動矩形,使左下角留在給定 position 。矩形的大小保持不變。

另請參閱 setBottomLeft (), moveBottom (),和 moveLeft ().

void QRect:: moveBottomRight (const QPoint & position )

移動矩形,使右下角留在給定 position 。矩形的大小保持不變。

另請參閱 setBottomRight (), moveRight (),和 moveBottom ().

void QRect:: moveCenter (const QPoint & position )

移動矩形,使中心點位於給定 position 。矩形的大小保持不變。

另請參閱 center ().

void QRect:: moveLeft ( int x )

水平移動矩形,使矩形左邊緣留在給定 x 坐標。矩形的大小保持不變。

另請參閱 left (), setLeft (),和 moveRight ().

void QRect:: moveRight ( int x )

水平移動矩形,使矩形右邊緣留在給定 x 坐標。矩形的大小保持不變。

另請參閱 right (), setRight (),和 moveLeft ().

void QRect:: moveTo ( int x , int y )

移動矩形,使左上角留在給定位置 ( x , y )。矩形的大小保持不變。

另請參閱 translate () 和 moveTopLeft ().

void QRect:: moveTo (const QPoint & position )

移動矩形,使左上角留在給定 position .

void QRect:: moveTop ( int y )

垂直移動矩形,使矩形上邊綫留在給定 y 坐標。矩形的大小保持不變。

另請參閱 top (), setTop (),和 moveBottom ().

void QRect:: moveTopLeft (const QPoint & position )

移動矩形,使左上角留在給定 position 。矩形的大小保持不變。

另請參閱 setTopLeft (), moveTop (),和 moveLeft ().

void QRect:: moveTopRight (const QPoint & position )

移動矩形,使右上角留在給定 position 。矩形的大小保持不變。

另請參閱 setTopRight (), moveTop (),和 moveRight ().

QRect QRect:: normalized () const

返迴規範化矩形;即:矩形擁有非負值寬度和高度。

width () < 0 函數交換左右角,和它交換上下角若 height () < 0.

另請參閱 isValid () 和 isEmpty ().

返迴矩形右邊的 X 坐標。

注意:由於曆史原因,此函數返迴 left () + width () - 1;使用 x () + width () 以檢索真正 X 坐標。

另請參閱 setRight (), topRight (),和 bottomRight ().

void QRect:: setBottom ( int y )

將矩形下邊緣設為給定 y 坐標。可能改變高度,但從不會改變矩形的上邊緣。

另請參閱 bottom () 和 moveBottom ().

void QRect:: setBottomLeft (const QPoint & position )

將矩形左下角設為給定 position 。可能改變大小,但從不會改變矩形的右上角。

另請參閱 bottomLeft () 和 moveBottomLeft ().

void QRect:: setBottomRight (const QPoint & position )

將矩形右下角設為給定 position 。可能改變大小,但從不改變矩形左上角。

另請參閱 bottomRight () 和 moveBottomRight ().

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

將矩形左上角坐標設為 ( x1 , y1 ),和它的右下角坐標到 ( x2 , y2 ).

另請參閱 getCoords () 和 setRect ().

void QRect:: setHeight ( int height )

將矩形高度設為給定 height 。下邊緣改變,但不改變上邊緣。

另請參閱 height () 和 setSize ().

void QRect:: setLeft ( int x )

將矩形左邊緣設為給定 x 坐標。可能改變寬度,但從不會改變矩形的右邊緣。

相當於 setX ().

另請參閱 left () 和 moveLeft ().

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

將矩形左上角坐標設為 ( x , y ),和它的大小到給定 width and height .

另請參閱 getRect () 和 setCoords ().

void QRect:: setRight ( int x )

將矩形右邊緣設為給定 x 坐標。可能改變寬度,但從不會改變矩形的左邊緣。

另請參閱 right () 和 moveRight ().

void QRect:: setSize (const QSize & size )

將矩形大小設為給定 size 。不移動左上角。

另請參閱 size (), setWidth (),和 setHeight ().

void QRect:: setTop ( int y )

將矩形上邊緣設為給定 y 坐標。可能改變高度,但從不會改變矩形的下邊緣。

相當於 setY ().

另請參閱 top () 和 moveTop ().

void QRect:: setTopLeft (const QPoint & position )

將矩形左上角設為給定 position 。可能改變大小,但從不改變矩形的右下角。

另請參閱 topLeft () 和 moveTopLeft ().

void QRect:: setTopRight (const QPoint & position )

將矩形右上角設為給定 position 。可能改變大小,但從不改變矩形的右下角。

另請參閱 topRight () 和 moveTopRight ().

void QRect:: setWidth ( int width )

將矩形寬度設為給定 width 。右邊緣改變,但左邊緣不改變。

另請參閱 width () 和 setSize ().

void QRect:: setX ( int x )

將矩形左邊緣設為給定 x 坐標。可能改變寬度,但從不會改變矩形的右邊緣。

相當於 setLeft ().

另請參閱 x (), setY (),和 setTopLeft ().

void QRect:: setY ( int y )

將矩形上邊緣設為給定 y 坐標。可能改變高度,但從不會改變矩形的下邊緣。

相當於 setTop ().

另請參閱 y (), setX (),和 setTopLeft ().

QSize QRect:: size () const

返迴矩形的大小。

另請參閱 setSize (), width (),和 height ().

CGRect QRect:: toCGRect () const

創建 CGRect 從 QRect .

該函數在 Qt 5.8 引入。

另請參閱 QRectF::fromCGRect ().

int QRect:: top () const

返迴矩形上邊緣的 Y 坐標。相當於 y ().

另請參閱 setTop (), topLeft (),和 topRight ().

QPoint QRect:: topLeft () const

返迴矩形左上角的位置。

另請參閱 setTopLeft (), top (),和 left ().

QPoint QRect:: topRight () const

返迴矩形右上角的位置。

注意:由於曆史原因,此函數返迴 QPoint ( left () + width () -1, top ()).

另請參閱 setTopRight (), top (),和 right ().

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

移動矩形 dx 沿 X 軸和 dy 沿 Y 軸,相對當前位置。正值嚮右下移動矩形。

另請參閱 moveTopLeft (), moveTo (),和 translated ().

void QRect:: translate (const QPoint & offset )

這是重載函數。

移動矩形 offset . x() 沿 X 軸和 offset . y() 沿 Y 軸,相對當前位置。

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

返迴的矩形副本有平移 dx 沿 X 軸和 dy 沿 Y 軸,相對當前位置。正值嚮右下移動矩形。

另請參閱 translate ().

QRect QRect:: translated (const QPoint & offset ) const

這是重載函數。

返迴的矩形副本有平移 offset . x() 沿 X 軸和 offset . y() 沿 Y 軸,相對當前位置。

QRect QRect:: transposed () const

返迴交換瞭寬度和高度的矩形副本:

QRect r = {15, 51, 42, 24};
r = r.transposed(); // r == {15, 51, 24, 42}
					

該函數在 Qt 5.7 引入。

另請參閱 QSize::transposed ().

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

返迴邊界矩形為此矩形和給定 rectangle .

該函數在 Qt 4.2 引入。

另請參閱 intersected ().

int QRect:: width () const

返迴矩形的寬度。

另請參閱 setWidth (), height (),和 size ().

int QRect:: x () const

返迴矩形左邊緣的 X 坐標。相當於 left ().

另請參閱 setX (), y (),和 topLeft ().

int QRect:: y () const

返迴矩形上邊緣的 Y 坐標。相當於 top ().

另請參閱 setY (), x (),和 topLeft ().

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

返迴交集為此矩形和給定 rectangle 。返迴空矩形若沒有交集。

另請參閱 operator&= () 和 intersected ().

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

相交此矩形與給定 rectangle .

另請參閱 intersected () 和 operator& ().

QRect &QRect:: operator+= (const QMargins & margins )

添加 margins 到矩形,增長它。

該函數在 Qt 5.1 引入。

另請參閱 marginsAdded (), marginsRemoved (),和 operator-= ().

QRect &QRect:: operator-= (const QMargins & margins )

返迴矩形的收縮按 margins .

該函數在 Qt 5.1 引入。

另請參閱 marginsRemoved (), operator+= (),和 marginsAdded ().

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

返迴邊界矩形為此矩形和給定 rectangle .

另請參閱 operator|= () 和 united ().

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

聯閤此矩形與給定 rectangle .

另請參閱 united () 和 operator| ().

相關非成員

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

返迴 true 若矩形 r1 and r2 不同,否則返迴 false .

QRect operator+ (const QRect & rectangle , const QMargins & margins )

返迴 rectangle 增長按 margins .

該函數在 Qt 5.1 引入。

QRect operator+ (const QMargins & margins , const QRect & rectangle )

這是重載函數。

返迴 rectangle 增長按 margins .

該函數在 Qt 5.1 引入。

QRect operator- (const QRect & lhs , const QMargins & rhs )

返迴 lhs rectangle shrunk by the rhs 邊距。

該函數在 Qt 5.3 引入。

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

寫入給定 rectangle 到給定 stream ,並返迴流引用。

另請參閱 序列化 Qt 數據類型 .

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

返迴 true 若矩形 r1 and r2 相等,否則返迴 false .

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

讀取矩形從給定 stream 進給定 rectangle ,並返迴流引用。

另請參閱 序列化 Qt 數據類型 .