3

I am using QGraphicsTextItem to paint the text on the scene. Text is painted along the path (QGraphicsPathItem), wich is parent of my QGraphicsTextItem - so the text rotation is changed to be along the path element and is sticked to it while zooming the view. But the font size of QGraphicsTextItem is also changing while zooming the view - this is what I am trying to avoid. Of I set QGraphicsItem::ItemIgnoresTransformations flag to the QGraphicsTextItem it stops rotating while it's parent (QGraphicsPathItem) does.

enter image description here

I do understand that I have to re-implement QGraphicsTextItem::paint function, but I am stuck with the coordination system. Here is the code (Label class inherits 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 );
}

The position (and rotation) of my text on the screen is unpredictable :( What am I doing wrong? Thank you very much in advance.

pau
  • 352
  • 3
  • 14
  • 2
    I have a similar issue with a QGraphicsTextItem: ignoring transformations causes an apparent shift in position with a different view scale. I believe that centering might resolve this. – handle Feb 10 '15 at 16:16

3 Answers3

3

I solved a problem this way - for drawing a line/circle/rectangle/path, which I want to be transformed, I use an appropriate QGraphicsLine/Ellipse/Rect/PathItem. For drawing the text (which I do NOT want to be transformed) I use QGraphicsSimpleTextItem. I set text's flag to ignore transormations and set it's parent to Line/Ellipse/Rect/Path item. The Line/Ellipse/Rect/Path item transforms, but text does not - this is what I wanted. I can also rotate text and set it's position. Thank you very much for answers.

pau
  • 352
  • 3
  • 14
1

I had this issue once. Instead of ignoring transformations, you need to scale down the items you don't want to be zoomed in in your zoom-in function.

When you zoom in, if you change the scale by ds for example, scale the items by 1.0 / ds

You might need to change their positions though.

I hope this helps.

Edit: I hope I understood the question right.

1

The following solution worked perfectly for me:

void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
{
    double scaleValue = scale()/painter->transform().m11();
    painter->save();
    painter->scale(scaleValue, scaleValue);
    painter->drawText(...);
    painter->restore();
    ...
}

We can also multiply the scaleValue by other mesures we want to keep its size constant outside the save/restore environment.

QPointF ref(500, 500);
QPointF vector = scaleValue * QPointF(100, 100);
painter->drawLine(ref+vector, ref-vector);
Adriel Jr
  • 2,451
  • 19
  • 25