You can use a QGraphicsEllipseItem
to add ellipses, circles, and segments/arcs to a QGraphicsScene
.
Try
QGraphicsEllipseItem* item = new QGraphicsEllipseItem(x, y, width, height);
item->setStartAngle(startAngle);
item->setSpanAngle(endAngle - startAngle);
scene->addItem(item);
Unfortunately, QGraphicsEllipseItem only supports QPainter::drawEllipse()
and QPainter::drawPie()
- the latter can be used to draw arcs, but has the side effect that there is always a line drawn from the start and the end of the arc to the center.
If you require a true arc, you can e.g. subclass QGraphicsEllipseItem
and override the paint()
method:
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);
}
};
You then still need to handle the item highlighting, unfortunately qt_graphicsItem_highlightSelected
is a static function defined inside the Qt library.