我正在使用QGraphicsTextItem在场景上绘制文本。文本沿着路径(QGraphicsPathItem)绘制,它是我的QGraphicsTextItem的父级 - 因此文本旋转更改为沿着路径元素,并在缩放时坚持它风景。但是 QGraphicsTextItem 的字体大小在缩放 View 时也会发生变化 - 这是我试图避免的。我将 QGraphicsItem::ItemIgnoresTransformations 标志设置为 QGraphicsTextItem,它会停止旋转,而其父级 (QGraphicsPathItem) 则会停止旋转。
我确实知道我必须重新实现 QGraphicsTextItem::paint 函数,但我被坐标系统困住了。这是代码(Label类继承公共(public)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 );
}
我的文本在屏幕上的位置(和旋转)是不可预测的:( 我究竟做错了什么?预先非常感谢您。
最佳答案
我用这种方式解决了一个问题 - 为了绘制一条线/圆/矩形/路径,我想要对其进行转换,我使用适当的 QGraphicsLine/椭圆/矩形/PathItem。为了绘制文本(我不想对其进行转换),我使用QGraphicsSimpleTextItem。我将文本的标志设置为忽略变换并将其父项设置为直线/椭圆/矩形/路径项。直线/椭圆/矩形/路径项目会变换,但文本不会 - 这就是我想要的。我还可以旋转文本并设置其位置。 非常感谢您的解答。
关于qt - 防止 QGraphicsItem 中的字体缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301470/