2013-05-17 54 views
1

凹形矩形我想在Qt來創建這樣的形狀:創建使用QPainterPath

enter image description here

這裏是一塊代碼(基本上繪製一個矩形,並在其上繪製了一個弧)的:

QPainterPath groupPath; 
QPen pen; 

pen.setCosmetic(true); 

groupPath.moveTo(60.0, 40.0); 
groupPath.arcTo(40.0, 35.0, 40.0, 10.0, 180.0, 180.0); 
groupPath.moveTo(40.0, 40.0); 
groupPath.lineTo(40.0, 80.0); 
groupPath.arcTo(40.0, 75.0, 40.0, 10.0, 0.0, 180.0); 
groupPath.arcTo(40.0, 75.0, 40.0, 10.0, 0.0, 180.0); 
groupPath.lineTo(80.0, 80.0); 
groupPath.lineTo(80.0, 40.0); 
groupPath.closeSubpath(); 
//setFixedSize(135, 80); 
QPainter painter(this); 
painter.setPen(pen); 
painter.drawPath(groupPath); 

該代碼創建頂部和底部彎曲,但我無法創建左側和右側彎曲。有沒有另一種方法來做到這一點?我看到剪輯,但不知道它是否會起作用。

回答

2

這裏是一個近似值

auto convexRect = [](QPainterPath& pp, QRect r) { 

    const int K = 20; 
    QPoint 
     tl = r.topLeft(), tr = r.topRight(), bl = r.bottomLeft(), br = r.bottomRight(), 
     dy(0, K), dx(K, 0); 

    pp.moveTo(tl); 
    pp.cubicTo(tl + dy, tr + dy, tr); 
    pp.cubicTo(tr - dx, br - dx, br); 
    pp.cubicTo(br - dy, bl - dy, bl); 
    pp.cubicTo(bl + dx, tl + dx, tl); 
}; 

QPainterPath pp; 
QRect r(0, 0, 200, 600); 
convexRect(pp, r); 
convexRect(pp, r.adjusted(20, 20, -20, -20)); 

產生

enter image description here

也許你能得到更好的結果縮放凸矩形,而不是重新定義它。

+0

非常感謝這幫了我:) – www