PDA

View Full Version : Keep text readable after flip & rotation of QGraphicsItem



adeas31
1st March 2016, 15:47
I have a QGraphicsItem class which contains a QGraphicsItem inside it. The nested QGraphicsItem contains the text. When I rotate or flip the top level QGraphicsItem the text of the nested item becomes rotates & flips also and becomes un-readable. How can ensure the text always remain readable?

Adeel.

d_stranz
1st March 2016, 16:13
Set the ItemIgnoresTransformations flag on the text item using QGraphicsItem::setFlag().

adeas31
2nd March 2016, 16:54
I should have mentioned it before. I already tried that flag and it removes all the transformations. I want to keep some transformations especially scaling. Well the flipping is done using the scaling but if i set ItemIgnoresTransformations flag then my text appears bigger than the parent item.

d_stranz
2nd March 2016, 17:07
Then you probably need to derive from QGraphicsSimpleTextItem / QGraphicsTextItem, whichever you use, and override the paint method. In it, you save the QPainter's state, set a new rotation transform, call the base class paint method with the modified QPainter, then restore the QPainter state.

You could first retrieve the painter's QTransform and check for isRotated() if you want to optimize a bit.

adeas31
3rd March 2016, 00:08
I am using QGraphicsItem and already using the paint method. I solved the issue by checking the flipping and rotation state of the parent item and then doing exactly the opposite of it on the painter. So my solution is working for all the cases I tried. But just wondering if Qt has some built in support for such thing perhaps in more specific class like QGraphicsTextItem.