1 Attachment(s)
QGraphicsItem : keep position when zoom changes
Hello,
In the code below, I create a QGraphicsRectItem and I want to display its name with a QGraphicsSimpleTextItem. I want the size of the text to be unaffected by the zoom, so I use the flag QGraphicsItem::ItemIgnoresTransformations. I also want the position of the text to be centered on the QGraphicsRectItem.
Unfortunately you can see on the capture that when I unzoom ( on the right ), the text doesn't stay inside the rectangle Attachment 12766 .
How to keep the text inside the rectangle and centered ?
Code:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPen>
#include <QWheelEvent>
#include <cmath>
#include <QDebug>
public:
protected:
double scaleFactor = pow(2.0, event->delta() / 240.0);
scale(scaleFactor, scaleFactor);
}
};
int main(int argc, char *argv[])
{
scene.setSceneRect(0, 0, 800, 800);
rectItem->setPos(200, 200);
rectItem
->setBrush
(QColor(255,
0,
0));
scene.addItem(rectItem);
QFont f
= nameItem
->font
();
f.setPointSize(12);
nameItem->setFont(f);
nameItem->setPos(rectItem->rect().center());
MainView view(&scene);
view.show();
return a.exec();
}
Thank you