我有一个关于在场景中绘制特定弧线的问题。我有关于 arc 的信息:
起始坐标, 起始角, 结束角 , 半径。
但我无法通过 QPainter
有效地使用它们。实际上,我尝试使用 QPainterPath
使用形状在 QGraphicsScene
和 addPath("")
上显示,但我无法正确使用函数。我的问题是关于如何使用此信息绘制弧线以及如何在我的图形场景中显示它。
最佳答案
您可以使用 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/