Maintaining text position after applying ItemIgnoresTransformations flag in Qt
Unsolved
General and Desktop
-
I am having QGraphicsView, which has multiple QGraphicsItem's. On QGraphicsView I am performing multiple transformation like zoom-in, zoom-out, Fit-in etc. In QGraphicsItem, I am having few rectangles and some polylines Every polyline has its own name written above it.
Whenever I zoom-in or zoom-out my view, I dont want to change my text size. It should remain same. For that I applied flag QGraphicsItem::ItemIgnoresTransformations.
But after applying this flag, whenever I zoomed-in text changes its position. Means in every zoom-in it should be just above the polyline. But it is changing it's position.myText.cpp
myText::myText(const QString &text): QGraphicsSimpleTextItem(text) {} QRectF myText::boundingRect() const { QRectF b = QGraphicsSimpleTextItem::boundingRect(); return QRectF(b.x()-b.width()/2.0, b.y()-b.height()/2.0, b.width(), b.height()); } void myText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->translate(boundingRect().topLeft()); QGraphicsSimpleTextItem::paint(painter, option, widget); painter->translate(-boundingRect().topLeft()); }
myClass.cpp
void myClass:: addText() { QGraphicsSimpleTextItem* text= new QGraphicsSimpleTextItem("Line 1"); text->setPos(QPointF(some points ); text->setDefaultTextColor(Qt::black); text->setFlag(QGraphicsItem::ItemIgnoresTransformations, true); scene->addItem(text); }
So question is :
How to ensure that text should not change its position ?