簡述
QPainterPath類提供了一個容器,用於繪圖操作,可以創建和重用圖形形狀。
QPainterPath是一個圖形構建塊的對象,如矩形、橢圓、直線和曲線。構建塊可以加入在封閉的子路徑中,例如:矩形或橢圓形。一個封閉的路徑同時存在開始點和結束點。或者作為未封閉的子路徑獨立存在,如:直線和曲線。
QPainterPath可以進行填充、顯示輪廓和裁剪。要生成可填充的輪廓的繪圖路徑,可以使用QPainterPathStroker類。QPainterPath比正常繪制的主要優點在於:復雜的圖形只需創建一次,然后可以僅僅通過調用QPainter::drawPath()函數來進行多次繪制。
QPainterPath提供了一組函數,可以用來獲取路徑及其元素的信息。除了可以使用toReversed()函數來改變元素的順序外,還有幾個函數將QPainterPath對象轉換成一個多邊形表示。
QPainterPath對象可以構造一個空的路徑,用給定的起點,或者另一個QPainterPath對象的副本。一旦創建,可以使用lineTo()、arcTo()、cubicTo()和quadTo()函數將直線和曲線添加到路徑中。直線和曲線從currentPosition()到作為參數傳遞的點的位置拉伸。
直線和曲線從currentPosition()開始繪制。currentPosition()總是返回最后的子路經繪制的終點。使用moveTo()函數可以在不增加路徑的情況下移動currentPositon(),它關閉了一個子路徑,開始一個新的子路徑。closeSubPath()也可以關閉當前路徑,並從currentPosition()連接一條直線到繪圖路徑的起點。
QPainterPath類也提供了一些便利的函數來添加一個封閉的子路徑-addEllipse()、addPath()、 addRect()、addRegion()和addText()。addPolygon()函數添加一個未封閉的子路徑。事實上,這些函數都是moveTo()、lineTo()、cubicTo()操作的集合。
Qt提供了Painter Paths Example
和Vector Deformation example
示例,分別位於Qt的例子目錄下。
它們分別介紹了如何通過QPainterPath來構建復雜的形狀,讓用戶嘗試填充和描邊。以及展示了如何使用QPainterPath繪制文本。
橢圓
void QPainterPath::addEllipse(const QRectF & boundingRectangle)
創建指定boundingRectangle內的一個橢圓,並將其添加到繪制路徑中作為一個封閉的子路徑。橢圓由順時針曲線組成,開始點和結束點在0度(3點鍾的位置)。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3 QRectF boundingRectangle; 4
5 QPainterPath myPath; 6 myPath.addEllipse(boundingRectangle); 7
8 QPainter painter(this); 9 painter.setBrush(myGradient); 10 painter.setPen(myPen); 11 painter.drawPath(myPath);
多邊形
void QPainterPath::addPolygon(const QPolygonF & polygon)
將給定的多邊形添加到路徑作為子路徑(未封閉)。
注意:添加了多邊形后的當前位置,是多邊形的最后一點。要回到起始點畫一條線,使用closeSubpath()函數。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3 QPolygonF myPolygon; 4
5 QPainterPath myPath; 6 myPath.addPolygon(myPolygon); 7
8 QPainter painter(this); 9 painter.setBrush(myGradient); 10 painter.setPen(myPen); 11 painter.drawPath(myPath);
矩形
void QPainterPath::addRect(const QRectF & rectangle)
將給定的矩形添加到繪制路徑作為一個封閉的子路徑。矩形添加作為一個順時針的一組線。添加了矩形后,繪制路徑的當前位置是矩形的左上角。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3 QRectF myRectangle; 4
5 QPainterPath myPath; 6 myPath.addRect(myRectangle); 7
8 QPainter painter(this); 9 painter.setBrush(myGradient); 10 painter.setPen(myPen); 11 painter.drawPath(myPath);
文本
void QPainterPath::addText(const QPointF & point, const QFont & font, const QString & text)
將給定的文本添加到此路徑,做為一組封閉的子路徑從字體創建提供。定位子路徑,使文本的基線的左端在指定的點。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3 QFont myFont; 4 QPointF baseline(x, y); 5
6 QPainterPath myPath; 7 myPath.addText(baseline, myFont, tr("Qt")); 8
9 QPainter painter(this); 10 painter.setBrush(myGradient); 11 painter.setPen(myPen); 12 painter.drawPath(myPath);
弧形
void QPainterPath::arcTo(const QRectF & rectangle, qreal startAngle, qreal sweepLength)
創建一個弧,占據了給定的矩形,開始在指定startAngle和擴展sweepLength度逆時針。
角度都以度為單位。順時針圓弧可以用負角度來指定。
注意:此函數連接弧的起點到當前位置。如果它們尚未連接,弧形被加入后,當前位置是在弧的最后一點。要再回到起始點繪制一條線,使用closeSubpath()函數。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3
4 QPointF center, startPoint; 5
6 QPainterPath myPath; 7 myPath.moveTo(center); 8 myPath.arcTo(boundingRect, startAngle, 9 sweepLength); 10
11 QPainter painter(this); 12 painter.setBrush(myGradient); 13 painter.setPen(myPen); 14 painter.drawPath(myPath);
貝塞爾曲線
void QPainterPath::cubicTo(const QPointF & c1, const QPointF & c2, const QPointF & endPoint)
添加一個貝塞爾曲線在當前位置和給定端點之間,使用指定的控制點c1、c2。
曲線被添加后,當前位置被更新為曲線的終點。
效果
源碼
1 QLinearGradient myGradient; 2 QPen myPen; 3
4 QPainterPath myPath; 5 myPath.cubicTo(c1, c2, endPoint); 6
7 QPainter painter(this); 8 painter.setBrush(myGradient); 9 painter.setPen(myPen); 10 painter.drawPath(myPath);
填充規則
設置繪制路徑給出fillRule的填充規則。Qt提供了填充規則,方法有兩種:
Qt::OddEvenFill (默認) | Qt::WindingFill |
---|---|
![]() |
![]() |