您可以使用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庫中定義的靜態函數。