QT QGraphicsScene绘图弧

时间:2013-01-11 13:25:35

标签: c++ qt qgraphicsscene qpainter

我有一个关于在场景上绘制特定弧的问题。我有关于arc的信息:

开始Koordinates, 开始角度, 结束角度, 半径。

但是我不能用QPainter来有效地使用它们。实际上,我尝试QPainterPath使用形状在QGraphicsScene上显示addPath(""),但我无法正常使用功能。我的问题是关于如何使用这个infortmation绘制弧以及如何在我的图形场景中显示它。

1 个答案:

答案 0 :(得分:4)

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

相关问题