问题
I have a question about drawing specific arc on a scene. I have this information about arc:
Starting Koordinates, Start Angle, End Angle , Radius.
But I can't use them efficently with QPainter
. Actually I tried QPainterPath
to use shape to show on QGraphicsScene
with addPath("")
but I can't use function properly. My questions are about how to use this infortmation to draw arc and how to show it on my graphic scene.
回答1:
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.
来源:https://stackoverflow.com/questions/14279162/qt-qgraphicsscene-drawing-arc