QT QGraphicsScene Drawing Arc

荒凉一梦 提交于 2019-12-08 07:02:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!