您可以使用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 库中定义的静态函数。