Qt之圖形(QPainterPath)


簡述

QPainterPath 類(繪圖路徑)提供了一個容器,用於繪圖操作,可以創建和重用圖形形狀。

繪圖路徑是由許多圖形化的構建塊組成的對象,例如:矩形、橢圓、直線和曲線。構建塊可以加入在封閉的子路徑中,例如:矩形或橢圓。封閉的路徑的起點和終點是一致的,或者他們可以作為未封閉的子路徑獨立存在,如:直線和曲線。

QPainterPath 可以被填充、描繪輪廓、裁剪。要為一個指定的繪圖路徑生成可填充的輪廓,可以使用 QPainterPathStroker 類。與正常繪圖相比,QPainterPath 的主要優點在於:復雜的圖形只需創建一次,然后只需調用 QPainter::drawPath() 函數即可繪制多次。

QPainterPath 提供了一組函數,可用於獲取繪圖路徑及其元素的信息。除了可以使用 toReversed() 函數來改變元素的順序外,還有幾個函數將 QPainterPath 對象轉換成一個多邊形表示。

 

  • 簡述
  • 創建 QPainterPath
  • QPainterPath 信息
  • QPainterPath 轉換
  • 橢圓
  • 多邊形
  • 矩形
  • 文本
  • 弧形
  • 貝塞爾曲線
  • 填充規則

 

創建 QPainterPath

QPainterPath 對象可以用指定的起點,或者另一個 QPainterPath 對象的副本來構造一個空路徑。

一旦創建,可以使用 lineTo()、arcTo()、cubicTo() 和 quadTo() 函數將直線和曲線添加到路徑中,直線和曲線從 currentPosition() 處伸展到其傳遞的參數的所在點的位置。

QPainterPath 對象的 currentPosition() 始終是最后一個添加的子路徑的最終位置(或初始起點),使用 moveTo() 函數可以在不增加組件的情況下移動 currentPositon(),moveTo() 函數會隱式地啟動一個新的子路徑,並關閉前一個。啟動新的子路徑的另一種方式是調用 closeSubpath() 函數,該函數通過添加一條直線(從 currentPosition() 到起始位置)來關閉當前路徑。注意:新路徑將 (0, 0) 作為其初始 currentPosition()。

QPainterPath 也提供了一些便利的函數來添加一個封閉的子路徑 - addEllipse()、addPath()、 addRect()、addRegion() 和 addText()。addPolygon() 函數添加一個未封閉的子路徑。事實上,這些函數都是 moveTo()、lineTo()、cubicTo() 操作的集合。

此外,使用 connectPath() 函數將路徑添加至當前路徑。但需要注意,該函數將通過添加一條直線,將當前路徑的最后一個元素連接到給定的第一個元素。

QPainterPath 信息

QPainterPath 類提供了一組函數,用於返回有關該路徑及其元素的信息。

currentPosition() 函數返回被添加的最后一個子路徑的終點(或初始起始點)。elementAt() 函數可用於檢索各種子路徑元素,可以使用 elementCount() 函數檢索元素的數量,isEmpty() 函數可以告訴該 QPainterPath 對象是否包含任何元素。

controlPointRect() 函數返回包含此路徑中所有點和控制點的矩形。與使用浮點精度返回此畫家路徑的邊界矩形的精確的 boundingRect() 相比,此函數的計算速度要快得多。

最后,QPainterPath 提供了 contains() 函數,用於確定給定點或矩形是否在路徑內。以及 intersects() 函數,用於確定給定矩形內的任何點是否也在該路徑內。

QPainterPath 轉換

出於兼容性原因,可能需要簡化繪圖路徑的表示形式:QPainterPath 提供的 toFillPolygon()、toFillPolygons()和 toSubpathPolygons() 函數,用於將繪圖路徑轉換為多邊形。toFillPolygon() 將繪圖路徑作為單個多邊形返回,而后兩個函數返回一個多邊形列表。

提供了 toFillPolygons() 和 toSubpathPolygons() 函數,因為繪制幾個小多邊形通常比繪制一個大的多邊形更快,即使繪制的總點數相同。兩者之間的差異是它們返回的多邊形數:toSubpathPolygons() 為每個子路徑創建一個多邊形,而不管相交的子路徑(即重疊的邊界矩形),而 toFillPolygons() 函數僅為重疊的子路徑創建一個多邊形。

toFillPolygon() 和 toFillPolygons() 函數首先將所有子路徑轉換為多邊形,然后使用重卷技術確保可以使用正確的填充規則來填充重疊的子路徑。注意:重卷會在多邊形中插入額外的線,因此填充多邊形的輪廓與路徑的輪廓不匹配。

橢圓

void QPainterPath::addEllipse(const QRectF & boundingRectangle)

在指定的 boundingRectangle 內創建一個橢圓,並將其作為一個封閉的子路徑添加至繪圖路徑中。

橢圓由順時針曲線組成,起始點和結束點在 0°(3 點鍾的位置)。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;
QRectF boundingRectangle;

QPainterPath myPath;
myPath.addEllipse(boundingRectangle);

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

多邊形

void QPainterPath::addPolygon(const QPolygonF & polygon)

將指定的 polygon 作為子路徑(未封閉)添加至繪圖路徑中。

注意:添加了 polygon 后,當前位置是 polygon 的最后一個點。要畫一條線回到起始點,使用 closeSubpath() 函數。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;
QPolygonF myPolygon;

QPainterPath myPath;
myPath.addPolygon(myPolygon);

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

矩形

void QPainterPath::addRect(const QRectF & rectangle)

將指定的 rectangle 作為子路徑(封閉)添加至繪圖路徑中。

rectangle 作為順時針的一組線被添加。添加 rectangle 后,繪圖路徑的當前位置是 rectangle 的左上角。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;
QRectF myRectangle;

QPainterPath myPath;
myPath.addRect(myRectangle);

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

文本

void QPainterPath::addText(const QPointF & point, const QFont & font, const QString & text)

將指定的 text 添加至此路徑中,作為由 font 創建的一組封閉子路徑。定位子路徑,使 text 基線的左端位於指定的 point。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;
QFont myFont;
QPointF baseline(x, y);

QPainterPath myPath;
myPath.addText(baseline, myFont, tr("Qt"));

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

弧形

void QPainterPath::arcTo(const QRectF & rectangle, qreal startAngle, qreal sweepLength)

創建一個弧形,占據了指定的 rectangle,以指定 startAngle 開始並逆時針擴展 sweepLength 度。

角度都以度為單位,可以用負角度來指定順時針弧形。

注意:如果它們尚未連接,此函數將連接弧的起點到當前位置。弧形被加入后,當前位置是弧的最后一點。要畫一條線回到起始點,使用 closeSubpath() 函數。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;

QPointF center, startPoint;

QPainterPath myPath;
myPath.moveTo(center);
myPath.arcTo(boundingRect, startAngle,
             sweepLength);

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

貝塞爾曲線

void QPainterPath::cubicTo(const QPointF & c1, const QPointF & c2, const QPointF & endPoint)

使用指定的控制點 c1、c2,在當前位置和指定的 endPoint 之間添加一條貝塞爾曲線。

曲線被添加后,當前位置會被更新為曲線的終點。

這里寫圖片描述

QLinearGradient myGradient;
QPen myPen;

QPainterPath myPath;
myPath.cubicTo(c1, c2, endPoint);

QPainter painter(this);
painter.setBrush(myGradient);
painter.setPen(myPen);
painter.drawPath(myPath);

填充規則

Qt 提供了兩種填充路徑的規則:

Qt::OddEvenFill (默認) Qt::WindingFill
這里寫圖片描述 這里寫圖片描述

 

原文鏈接: http://blog.csdn.net/liang19890820/article/details/51393152

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM