2013-01-11 140 views
1

我有一個關於在場景中繪製特定弧的問題。我有關於弧的這個信息:QT QGraphicsScene繪圖弧

起動Koordinates, 起始角度, 結束角度, 半徑。我不能用QPainter高效地使用它們。其實我嘗試QPainterPath使用形狀顯示在QGraphicsSceneaddPath(""),但我不能正常使用功能。我的問題是關於如何使用這個infortmation繪製弧線以及如何在我的圖形場景中顯示它。

回答

4

您可以使用QGraphicsEllipseItem將橢圓,圓和段/弧添加到QGraphicsScene

嘗試

QGraphicsEllipseItem* item = new QGraphicsEllipseItem(x, y, width, height); 
item->setStartAngle(startAngle); 
item->setSpanAngle(endAngle - startAngle); 
scene->addItem(item); 

不幸的是,QGraphicsEllipseItem只支持QPainter::drawEllipse()QPainter::drawPie() - 後者可以用來畫弧,但有副作用,總是有從一開始的結束畫的線弧到中心。

如果你需要一個真正的弧,你可以例如子類QGraphicsEllipseItem並覆蓋paint()方法:

class QGraphicsArcItem : public QGraphicsEllipseItem { 
public: 
    QGraphicsArcItem (qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0) : 
     QGraphicsEllipseItem(x, y, width, height, parent) { 
    } 

protected: 
    void paint (QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { 
     painter->setPen(pen()); 
     painter->setBrush(brush()); 
     painter->drawArc(rect(), startAngle(), spanAngle()); 

//  if (option->state & QStyle::State_Selected) 
//   qt_graphicsItem_highlightSelected(this, painter, option); 
    } 
}; 

,那麼你仍然需要處理突出的項目,可惜qt_graphicsItem_highlightSelected是Qt庫中定義的靜態函數。