PDA

View Full Version : Preventing font scale in QGraphicsItem



pau
3rd February 2015, 15:32
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. If I set QGraphicsItem::ItemIgnoresTransformations flag to the QGraphicsTextItem it stops rotating while it's parent (QGraphicsPathItem) does.

10940

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.

d_stranz
3rd February 2015, 17:03
Try putting the QGraphicsTextItem inside of a QGraphicsRectItem. Set ItemIgnoresTranformations on the text, but not on the rectangle. Set the rectangle size to match the bounds of your path. Rotate and scale the rectangle, instead of the text.

You can set the rectangle to have no pen and no brush, which will basically make it invisible.