2016-05-04 11 views
0

MyクラスはQGraphicsItemを継承しています。私はpainter-> drawArcで描画しますが、そのオブジェクトに同じバインドを作成したいのですが、QpainterPathにはpainterのような関数がありません。それは中心からの線を持っているので、それは同じではありません。boundingRect()を円弧状にするには?

コード(幅ペンの幅があるので、衝突が円弧の外側の境界線上にある。):

QRectF Circle::boundingRect() const 
{ 
    QRectF rect(-radius, -radius, radius*2, radius*2); 
    return rect; 
} 

QPainterPath Circle::shape() const 
{ 
    QPainterPath path; 
    path.arcTo(-radius-width, -radius-width, (radius+width)*2, (radius+width)*2, startAngle/16, spanAngle/16); 
    return path; 
} 

void Circle::paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    QPen pen; 
    pen.setCapStyle(Qt::FlatCap); 
    pen.setWidth(width); 
    painter->setPen(pen); 
    painter->drawArc(boundingRect(), startAngle, spanAngle); 

} 
+0

ない私はあなたの質問を理解しますが、 'boundingRect()は'常に** RECT **角度を返すために必要であることを確認:そしてまた、バウンディング矩形

例にペン幅を追加します。それのまわりには道がない。あなたがこれを必要としているかどうかはわかりませんが、別の方法を見つける必要があると思います。あなたがもう少しコンテキストを提供できるなら、ここにいる誰かが良い提案をしているかもしれません。 – Bowdzone

+0

これは私が 'shape();'を使ったのですが、この関数は 'QPainterPath'を返す必要があり、私はこれをペインタでやったように' QPainterPath'で円弧を描く方法を知らない。 –

答えて

0

あなたはQPaintePath::arcToを使用する必要がありますが、あなたはスタートで現在位置を移動する必要がありますアークの現在の位置に線で接続されます。開始時点での現在位置を移動するには

は、あなたがQPaintePath::arcMoveTo

QPainterPath pp; 
pp.arcMoveTo(rect, startAngle); 
pp.arcTo(rect, startAngle, spanAngle); 

を使用することができる形状に厚さを与えるためにQPainterPathStrokerを使用することも考えてみましょう。

QRectF Circle::boundingRect() const 
{ 
    return QRectF(-radius - width, -radius - width, (radius + width) * 2, (radius + width) * 2); 
} 

QPainterPath Circle::shape() const 
{ 
    QRectF rect(-radius, -radius, radius * 2, radius * 2); 
    QPainterPath path; 
    path.arcMoveTo(rect, startAngle/16); 
    path.arcTo(rect, startAngle/16, spanAngle/16); 
    QPainterPathStroker pps; 
    pps.setCapStyle(Qt::FlatCap); 
    pps.setWidth(width); 
    return pps.makeStroke(path); 
} 

void Circle::paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    QPen pen; 
    pen.setCapStyle(Qt::FlatCap); 
    pen.setWidth(width); 
    painter->setPen(pen); 
    QRectF rect(-radius, -radius, radius * 2, radius * 2); 
    painter->drawArc(rect, startAngle, spanAngle); 

} 
関連する問題