我正在使用 QGraphicsTextItem 在场景上绘制文本。文本沿着路径(QGraphicsPathItem)绘制,该路径是我 QGraphicsTextItem 的父级 - 因此,文本旋转变为沿着路径元素,并且在缩放视图时固定在其上。但是,QGraphicsTextItem 的字体大小也会随着缩放视图而更改 - 这就是我试图避免的。如果我将 QGraphicsItem :: ItemIgnoresTransformations 标志设置为 QGraphicsTextItem ,则它会停止旋转,而其父级 (QGraphicsPathItem ) 则会继续旋转。
我明白我必须重新实现 QGraphicsTextItem :: paint 函数,但是我卡在了坐标系上。 以下是代码(Label 类公开继承 QGraphicsTextItem):
void Label::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
// Store current position and rotation
QPointF position = pos();
qreal angle = rotation();
// Store current transformation matrix
QTransform transform = painter->worldTransform();
// Reset painter transformation
painter->setTransform( QTransform() );
// Rotate painter to the stored angle
painter->rotate( angle );
// Draw the text
painter->drawText( mapToScene( position ), toPlainText() );
// Restore transformation matrix
painter->setTransform( transform );
}
我的文字在屏幕上的位置(和旋转)是不可预测的 :( 我做错了什么?非常感谢您提前。