How to make QGraphicsItem not change size but keep the same position?
-
I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.
This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
How could I force the QGraphicsItem to keep the same size (in pixel)?
I tried to use
setFlags(QGraphicsItem::ItemIgnoresTransformations);
in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.I tried to set a new position to my QGraphicsItem after overriding
resizeEvent()
but I could not manage to undertand which size of the light blue area (the parent widget) I should use...Any hint please?
-
@Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:
If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)
If OP wants a static "thing" on the view, and not change size/move, that is what I would do.
Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.
I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.
-
I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.
This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
How could I force the QGraphicsItem to keep the same size (in pixel)?
I tried to use
setFlags(QGraphicsItem::ItemIgnoresTransformations);
in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.I tried to set a new position to my QGraphicsItem after overriding
resizeEvent()
but I could not manage to undertand which size of the light blue area (the parent widget) I should use...Any hint please?
Hi,
If memory serves well, you want to determine the position of the item using your view's coordinates and the use the mapToScene method to adjust your item position.
Hope it helps
-
I have a QGraphicsItem on which I add several QGraphicsItems. When the user maximise/resize the window, all QGraphicsItems are resize accordingly to keep the same aspect ratio.
This behaviour is fine for most of my QGraphicsItems but I would like to avoid this resizing for one of them.
On the screenshot below, the QGraphicsItem on the right (the blue sqare with the arrow inside) has the nominal size
Yet, when the user enlarges the window, the QGraphicsItem is stretched like the window (see below)
How could I force the QGraphicsItem to keep the same size (in pixel)?
I tried to use
setFlags(QGraphicsItem::ItemIgnoresTransformations);
in the QGraphicsItem but doing so, it does not resize (good!) but I need to replace the QGraphicsItem because it is not located anymore on the right.I tried to set a new position to my QGraphicsItem after overriding
resizeEvent()
but I could not manage to undertand which size of the light blue area (the parent widget) I should use...Any hint please?
@odelaune
You may want to read up this: https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-systemIn the normal order of things, GraphicsItems keep a constant size in scene coordinates (unless you resize them). What you want is an item that keeps a constant size in the view coordinate system. This is a bit tricky in principle, since GraphicsView supports multiple views on a single scene.
If the item isn't really "part of the scene", but kind of an overlay text, you may want to consider rendering it on top of the QGraphicsView, thereby bypassing all GraphicsView coordinate systems. If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)
-
@odelaune
You may want to read up this: https://doc.qt.io/qt-6/graphicsview.html#the-graphics-view-coordinate-systemIn the normal order of things, GraphicsItems keep a constant size in scene coordinates (unless you resize them). What you want is an item that keeps a constant size in the view coordinate system. This is a bit tricky in principle, since GraphicsView supports multiple views on a single scene.
If the item isn't really "part of the scene", but kind of an overlay text, you may want to consider rendering it on top of the QGraphicsView, thereby bypassing all GraphicsView coordinate systems. If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)
@Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:
If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)
If OP wants a static "thing" on the view, and not change size/move, that is what I would do.
-
@Asperamanca said in How to make QGraphicsItem not change size but keep the same position?:
If that's what you want to do, you can use QGraphicsView::drawForeground (or drawBackground)
If OP wants a static "thing" on the view, and not change size/move, that is what I would do.
Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.
I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.
-
-
Thanks for the tip. For now I've achieved what I want by doing tricky mathematical transformations using the size of the scene in screen coordinates, the size of the scene in view coordinates and the size of the widget.
I did not know about drawForeground() and it seems to be suitable for what I want. I will keep it in mind for next time.