c++ - QT QGraphicsScene 绘制圆弧

标签 c++ qt qgraphicsscene qpainter

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

起始坐标, 起始角, 结束角 , 半径。

但我无法通过 QPainter 有效地使用它们。实际上,我尝试使用 QPainterPath 使用形状在 QGraphicsSceneaddPath("") 上显示,但我无法正确使用函数。我的问题是关于如何使用此信息绘制弧线以及如何在我的图形场景中显示它。

最佳答案

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

关于c++ - QT QGraphicsScene 绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14279162/

相关文章:

c++ - 链接内核后跳转到保护模式不起作用

具有类成员默认值的 C++11 默认构造函数行为

c++ - 如何将文本添加到 qtablewidget 的单元格

qt - 如何正确使用 QGraphicsLinearLayout 和 QGraphicsScene 来定位 QGraphicsItems?

qt4 - 调整 QGraphicsView 大小不会移动小部件

c++ - 暂停和恢复 std::thread 的正确方法

c++ - int x[] 和 int *x 有区别吗?

c++ - 如何更改 QtCreator 中的 C++ 运行时库设置?

qt - 如何在 PCL(使用 Qt)中可视化一系列 TOF 传感器数据?

python - QMouseEvent,收到意外类型