qt - 防止 QGraphicsItem 中的字体缩放

标签 qt font-size zooming qgraphicstextitem

我正在使用QGraphicsTextItem在场景上绘制文本。文本沿着路径(QGraphicsPathItem)绘制,它是我的QGraphicsTextItem的父级 - 因此文本旋转更改为沿着路径元素,并在缩放时坚持它风景。但是 QGraphicsTextItem 的字体大小在缩放 View 时也会发生变化 - 这是我试图避免的。我将 QGraphicsItem::ItemIgnoresTransformations 标志设置为 QGraphicsTextItem,它会停止旋转,而其父级 (QGraphicsPathItem) 则会停止旋转。

enter image description here

我确实知道我必须重新实现 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/

相关文章:

ios - Swift:如何在使用自动布局时调整 UITextView 中的字体大小?

javascript - 背景图像不使用 SVG 缩放和平移

python - 如何在 QtDesigner 中加载 QMovie?

c++ - CreateProcess 可以启动一个进程,但是QProcess 不能……为什么?

c++ - 如何有效地在窗口中滚动 1024x90000 图像?

javascript - 单击按钮时无法增加字体大小

css - 字体在不同的缩放和显示尺寸下显示不正确

图像太大而无法显示在屏幕上 (MATLAB)

android - 使用 ScaleGestureDetector 的缩放和 Canvas 问题

Python/QT - 我该怎么做才能在一系列小部件之间获得 4 个像素边框?