2011-04-20 103 views
4

有什麼辦法可以採取QPainterPath並擴大它,就像Photoshop中的Selection> Grow ...(或Expand ...)命令一樣嗎?QPainterPath增長/擴大

我想從QGraphicsItem::shape返回QPainterPath並將其作爲QGraphicsPathItem的基礎。但是我想擴展一個給定量的形狀,比如10個像素。然後圍繞這個擴展的形狀繪製一個薄的輪廓。

我可以通過設置用於繪製QGraphicsPathItem 20(我想要的寬度* 2,因爲它吸引裏面的一半,另一半外)QPen的寬度做到這一點。這給了正確的外形,但有一條醜陋的粗線;有沒有辦法(我可以看到)得到這個形狀,並用細線勾勒出來。

QPainterPathStroker類看起來很有前途,但我似乎無法讓它達到我想要的水平。

回答

4

QPainterPathStroker是正確的想法:

QPainterPathStroker stroker; 
stroker.setWidth(20); 
stroker.setJoinStyle(Qt::MiterJoin); // and other adjustments you need 
QPainterPath newpath = (stroker.createStroke(oldPath) + oldPath).simplified(); 

QPainterPath::operator+()團結2條路徑和simplified()合併子路徑。這也將處理「挖空」的路徑。

5

要通過x像素長出QPainterPath,你可以使用一個QPainterPathStroker與寬鋼筆,然後團結原來與描邊路徑:

QPainterPath grow(const QPainterPath & pp, int amount) { 
    QPainterPathStroker stroker; 
    stroker.setWidth(2 * amount); 
    const QPainterPath stroked = stroker.createStroke(pp); 
    return stroked.united(pp); 
} 

注意,但是,由於Qt的4.7,該united() function (以及類似的設置操作)將路徑變成多段線以解決路徑交叉碼中的數字不穩定問題。雖然這對繪圖來說很好(兩種方法之間不應該有任何明顯的區別),但是如果您打算保持QPainterPath,例如以允許進一步的操作(你提到的Photoshop),那麼這會破壞所有貝塞爾曲線,這可能不是你想要的。