文章目錄
- 類描述
- 構造方法
- 主要方法
- 1. 基礎屬性
- 2. 邊界操作
- 3. 幾何運算
- 4. 坐標調整
- 5. 轉換方法
- 6. 狀態判斷
- 類特點總結
- 1. 浮點精度:
- 2. 坐標系統:
- 3. 有效性判斷:
- 4. 幾何運算:
- 5. 類型轉換:
- 6. 特殊處理:
- 典型應用場景
- 1. 圖形碰撞檢測:
- 2. 動態布局調整:
- 3. 高精度繪圖:
- 4. 動畫路徑計算:
- 注意事項
- 1. 坐標系差異:
- 2. 精度轉換:
- 3. 性能優化:
- 4. 異常處理:
- 5. 邊界情況:
- 附:與QRect核心區別
類描述
QRectF
類使用浮點精度定義平面上的矩形,繼承自Shiboken.Object
。用于圖形界面開發中高精度矩形區域操作。
構造方法
# 通過坐標構造
rect1 = QRectF(100.0, 200.0, 50.5, 30.2) # QRectF(float x, float y, float w, float h)
print(rect1) # QRectF(100.0,200.0,50.5,30.2)
# 通過QPointF和QSizeF構造
top_left = QPointF(100.0, 200.0)
size = QSizeF(50.5, 30.2)
rect2 = QRectF(top_left, size) # QRectF(QPointF topLeft, QSizeF size)
print(rect2) # QRectF(100.0,200.0,50.5,30.2)# 從QRect轉換
qrect = QRect(100, 200, 50, 30)
rect3 = QRectF(qrect) # QRectF(QRect rect)
print(rect3) # QRectF(100.0,200.0,50.0,30.0)
主要方法
1. 基礎屬性
print(rect1.x()) # 左邊緣x坐標 (100.0)
print(rect1.y()) # 上邊緣y坐標 (200.0)
print(rect1.width()) # 寬度 (50.5)
print(rect1.height()) # 高度 (30.2)
print(rect1.center()) # 中心點 QPointF(125.25, 215.1)
2. 邊界操作
rect = QRectF(100, 200, 50, 30)
rect.setLeft(110) # 調整左邊界,寬度變為40.0
print(rect) # QRectF(110.0, 200.0, 40.0, 30.0)
rect.moveBottom(250) # 保持高度,移動到底部到y=250
print(rect) # QRectF(110.0, 220.0, 40.0, 30.0)
print(rect.bottomRight()) # 右下角 QPointF(150.0, 250.0)
3. 幾何運算
rectA = QRectF(0, 0, 100, 100)
rectB = QRectF(50, 50, 100, 100)# 交集
intersection = rectA.intersected(rectB) # QRectF(50,50,50,50)
print(intersection) # 并集
united = rectA.united(rectB) # QRectF(0,0,150,150)
print(united) # 包含檢測
print(rectA.contains(QPointF(30, 30))) # True
print(rectA.intersects(rectB)) # True
4. 坐標調整
rect = QRectF(100, 200, 50.5, 30.2)
# 相對調整
new_rect = rect.adjusted(10, 5, -20, -10)
# 原rect (100,200,50.5,30.2)
# 新矩形 (110,205,20.5,15.2)
print(new_rect) # QRectF(110.0,205.0,20.5,15.2)# 絕對設置
rect.setCoords(110, 205, 130.5, 220.2)
print(rect) # QRectF(110.0,205.0,20.5,15.2)
5. 轉換方法
# 轉為整數矩形QRect
int_rect = rect.toRect() # 四舍五入坐標# 轉置寬高
transposed = rect.transposed() # 寬高交換
6. 狀態判斷
print(QRectF().isNull()) # True(寬高均為0)
print(QRectF(0,0,0,10).isEmpty()) # True(寬或高為0)
print(QRectF(0,0,10,10).isValid()) # True
類特點總結
1. 浮點精度:
? 使用float類型存儲坐標,適合需要高精度定位的場景
? 可精確表示小數位置和尺寸(如50.5像素)
2. 坐標系統:
? 使用Top-Left坐標系(左上角為原點)
? 邊界方法包含數學邊界(right()和bottom()返回實際坐標+寬高)
3. 有效性判斷:
? isValid()
:寬高均>0時為True
? isEmpty()
:寬或高≤0時為True
? isNull()
:寬高均為0時為True
4. 幾何運算:
? 支持交集(intersected)、并集(united)、包含檢測
? 提供adjusted()進行相對調整和setRect()絕對設置
5. 類型轉換:
? 與QRect可互轉(toRect()會四舍五入)
? 與QPointF/QSizeF無縫協作
6. 特殊處理:
? normalized()自動處理負寬高情況
? transposed()交換寬高
? 支持margin操作(marginsAdded/Removed)
典型應用場景
1. 圖形碰撞檢測:
def check_collision(obj1, obj2):return obj1.geometry().intersects(obj2.geometry())
2. 動態布局調整:
def resize_handler(new_size):global rectrect.setSize(QSizeF(new_size.width()*0.8, new_size.height()-20))update()
3. 高精度繪圖:
painter.drawEllipse(QRectF(100.5, 200.3, 50.7, 50.7)) # 平滑圓形繪制
4. 動畫路徑計算:
def animate(pos):current_rect = QRectF(pos.x(), pos.y(), 50, 50)return current_rect.translated(2.5, 1.8) # 平滑移動
注意事項
1. 坐標系差異:
? 數學坐標系:右下邊界包含計算值(right=x+width)
? 渲染時:抗鋸齒處理會使實際繪制范圍略大于數學范圍
2. 精度轉換:
? 轉QRect時使用四舍五入,可能丟失精度
? 關鍵計算建議保持QRectF類型直到最終渲染
3. 性能優化:
? 頻繁計算時建議使用adjust()代替adjusted()
? 批量操作時優先使用setCoords/setRect
4. 異常處理:
? 所有參數必須為有限數值(非NaN/Inf)
? 無效矩形無法進行幾何運算
5. 邊界情況:
# 處理負尺寸
rect = QRectF(100, 100, -50, -30)
valid_rect = rect.normalized() # (50, 70, 50, 30)
附:與QRect核心區別
特性 | QRect | QRectF |
---|---|---|
存儲類型 | 整數(int) | 浮點數(float) |
精度 | 像素級對齊 | 亞像素級精度 |
構造開銷 | 較低 | 略高 |
適用場景 | UI元素定位 | 圖形繪制/動畫/復雜計算 |
轉換方式 | 直接截斷 | 四舍五入 |
無效值處理 | (0,0,0,0) | 允許負寬高(需normalized) |