sik*_*izi 2 c++ qt qpainter qgraphicsscene
我有一个关于在场景中绘制特定弧线的问题。我有关于弧的这些信息:
起始坐标、起始角、终止角、半径。
但我不能有效地将它们与QPainter. 实际上我尝试QPainterPath使用形状来显示QGraphicsScene,addPath("")但我无法正确使用功能。我的问题是关于如何使用这些信息来绘制弧线以及如何在我的图形场景中显示它。
您可以使用 aQGraphicsEllipseItem将椭圆、圆和线段/圆弧添加到QGraphicsScene.
尝试
QGraphicsEllipseItem* item = new QGraphicsEllipseItem(x, y, width, height);
item->setStartAngle(startAngle);
item->setSpanAngle(endAngle - startAngle);
scene->addItem(item);
Run Code Online (Sandbox Code Playgroud)
不幸的是,QGraphicsEllipseItem 只支持QPainter::drawEllipse()and 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);
}
};
Run Code Online (Sandbox Code Playgroud)
然后您仍然需要处理项目突出显示,不幸的qt_graphicsItem_highlightSelected是在 Qt 库中定义了一个静态函数。
| 归档时间: |
|
| 查看次数: |
6980 次 |
| 最近记录: |