I think my question is similar to this post but in C++ and inside a QGraphicsItem.
I would like to fix the movable area of my object inside an other QGraphicsItem. I want my object to stay inside if I try to move it outside.
Maybe the idea will be to use setParentItem()
.
Does someone know how to restrict a movable area inside a QGraphicsItem please?
Yes, you are correct. As in here you have to reimplement itemChange. From the qt documentation
where scene() refers to the QGraphicsScene in which the item reside. If you don't use a QGraphicScene you must set a QRectF appropriately (maybe from the parent item geometry).
I solved my problem!
For this I add to redefine how to set the position of my
QGraphicsItem
. My item is just define by theboundingRect()
like this:So I wanted this
QRectF
to stay inside the scene. The position of my item is define by the center of thisQRectF
. Using the code from the Qt documentation proposed by @Salvatore Avanzo here is my code:Don't forget to set the
QRectF
of the scene (see comments in the question).