pyqt4文件閱讀 7 2 QRect

2021-07-17 03:52:30 字數 4011 閱讀 4010

本系列文章長期更新修改.

qrect,相當於qpoint+qsize,但擁有更豐富的函式和操作,本質上是4個int的結構體.

1.主要屬性

qrect是用來描述乙個矩形的資料結構.在座標系裡描述乙個矩形,只要4個數,分別是矩形左上角的x,y座標,以及矩形的寬度和高度,也就是qpoint+qsize.

注意這裡的座標系不是我們高中時常見的座標系,而是螢幕的那種座標系,它的y軸是往下增長的!

qrect在初始化的時候,如果不提供引數,則x,y座標和寬度高度都是0,稱為全0矩形.

qrect有一系列屬性的獲取修改,但很多屬性不是實際儲存的.

如右下角座標,但它的資訊能轉化成實際存在的四個屬性xpos,ypos,width,height.

但由於迷之歷史原因,許多函式用起來彷彿有bug一樣.

這個圖的意思就是,left()+width()是不等於right()的,正確的是left()+width()-1=right().

請每次使用qrect之前都回想起這個歷史問題,並且盡量不要使用容易導致出錯的這些函式!!!!!!

最好的方法是只使用只和左上角x,y座標和寬度高度相關的函式,也就是說下面的大部分函式都不應該使用.

函式含義

int x (self)

setx (self, int ax)

int left (self)

setleft (self, int pos)

左上角x座標

int y (self)

sety (self, int ay)

int top (self)

settop (self, int pos)

左上角y座標

int width (self)

setwidth (self, int w)

寬度int height (self)

setheight (self, int h)

高度int right (self)

setright (self, int pos)

右下角x座標

int bottom (self)

setbottom (self, int pos)

右下角y座標

qpoint bottomleft (self)

setbottomleft (self, qpoint p)

左下角座標

qpoint bottomright (self)

setbottomright (self, qpoint p)

右下角座標

qpoint topleft (self)

settopleft (self, qpoint p)

左上角座標

qpoint topright (self)

settopright (self, qpoint p)

右上角座標

qpoint center (self)

中心座標

qsize size (self)

setsize (self, qsize s)

寬度高度

(int ax, int ay, int aw, int ah) getrect (self)

setrect (self, int ax, int ay, int aw, int ah)

xy座標+寬度高度

(int xp1, int yp1, int xp2, int yp2) getcoords (self)

setcoords (self, int xp1, int yp1, int xp2, int yp2)

左上角座標+右下角座標

2.move操作

move操作是把矩形的某個點或者某條邊移動到引數給出的位置,並保持size不變.

函式含義

movebottom (self, int pos)

移動底邊

movebottomleft (self, qpoint p)

移動左下角

movebottomright (self, qpoint p)

移動右下角

movecenter (self, qpoint p)

移動中心點

moveleft (self, int pos)

移動左邊

moveright (self, int pos)

移動右邊

moveto (self, int ax, int ay)

moveto (self, qpoint p)

movetopleft (self, qpoint p)

移動左上角

movetop (self, int pos)

移動頂邊

movetopright (self, qpoint p)

移動右上角

3.矩形運算

下面是一些矩形運算,它們都有不帶ed字尾和帶ed字尾的版本.

不帶ed的版本會把計算結果應用到自己身上,而帶ed的版本則不會,而是把計算結果通過返回值給出.

函式含義

adjust (self, int dx1, int dy1, int dx2, int dy2)

qrect adjusted (self, int xp1, int yp1, int xp2, int yp2)

左上角右下角座標加上相應引數

qrect intersect (self, qrect r)

qrect intersected (self, qrect other)

兩個矩形取交集(沒交集時返回全0矩形)

qrect unite (self, qrect r)

qrect united (self, qrect r)

兩個矩形取並集

translate (self, int dx, int dy)

translate (self, qpoint p)

qrect translated (self, int dx, int dy)

qrect translated (self, qpoint p)

左上角座標加上相應引數,size不變

相當於movetopleft

關於矩形交集和並集的含義,參考下圖.

4.布林運算

下面是一些布林運算,其中contains()函式的proper或aproper引數表示包含是否算上邊緣,如果為true則不算上.

函式含義

bool contains (self, qpoint p, bool proper = false)

bool contains (self, int ax, int ay, bool aproper)

bool contains (self, int ax, int ay)

判斷是否包含引數中的點

bool contains (self, qrect r, bool proper = false)

判斷是否包含引數中的矩形

bool intersects (self, qrect r)

判斷兩個矩形是否有交集

bool isempty (self)

等同於left()>right() or top()>bottom()

bool isnull (self)

等同於width()==0 and height()==0

bool isvalid (self)

等同於left()

5.normalized()

normalized()函式會返回自己的合法矩形,就是說如果自己的width()<0,則交換左右邊緣,如果自己的height()<0則交換上下邊緣.

由於該函式帶有ed字尾,這意味著它並不改變自己.

pyqt4文件閱讀 6 QGridLayout

本系列文章長期更新修改.qgridlayout,網格布局,是qt裡十分常用的布局,下圖即為網格布局示意圖.1.網格系統 qgridlayout是一種網格布局系統,和css裡的bootstrap相似,但比它更複雜.乙個網格布局就是乙個矩陣,每個格仔都可以放乙個控制項或者乙個子布局,也可以乙個控制項占用...

pyqt4文件閱讀 7 QSize

本系列文章長期更新修改.qsize,是qt的乙個常用資料結構,它是整數寬度高度的封裝,本質上就是兩個int的結構體.1.主要屬性 qsize的主要屬性就是寬度和高度,所有方法都圍繞它們進行計算.初始化的時候如果沒有給出引數,寬度和高度為 1.2.函式解釋 函式 解釋transpose self 交換...

pyqt4文件閱讀 7 4 QLine

本系列文章長期更新修改.qline,是qt裡用來表示一條線的資料結構,它由兩個整數點qpoint構成,本質上是四個int的結構體.1.主要屬性 qline的主要屬性就是線條起點終點的x,y座標,共四個整數.在初始化的時候,如果不提供座標,則這四個整數均為0.下面的一系列函式是qline主要屬性的獲取...