How to implement boundingRect() when zooming?
-
Suppose you have a
QGraphicsSceneon which you place severalQGraphicsItem's.You want the user to be able to zoom, so you implement
wheelEvent()in your view class (which inherits fromQGraphicsView).When the user zooms in or out, you want the size of the items to change, except for certain items. Let's say such an item is
MyGraphicsSquareItem(which inheritsQGraphicsItem): you want the size ofMyGraphicsSquareItem's to remain the same, independently of the zoom level.In order to keep the size of
MyGraphicsSquareItemconstant, you do something like this when painting it:void MyGraphicsSquareItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { qreal detail = option->levelOfDetailFromTransform(painter->worldTransform()); [painting code which takes detail into account] }Fine, but how should one implement
MyGraphicsSquareItem::boundingRect()? How canMyGraphicsSquareItem::boundingRect()(which takes no parameters) get the currentQTransform, and thus computedetailasMyGraphicsSquareItem::paint()does? -
That is a very good queston. I am facing exactly the same issue where the painting doesn't transform via
painter.resetTransform()but myboundingRect()from the customQGraphicsItemtransforms / changes when changing the scene zoom level.src/QALibs/Recorder/GraphicsLine.py
from PyQt5.QtWidgets import QGraphicsItem class GraphicsLine(QGraphicsItem): ... def paint(self, painter: QPainter, option, widget): painter.resetTransform() # Painted items stay untransformed / unchanged no matter which zoom level on the scene ... def boundingRect(self): # How to avoid boundingRect() changes on zoom