Important: Please read the Qt Code of Conduct -

Focusing on item

  • Hi ,
    I want my (custom) QGraphicsItem to always be centered in the screen while all other items stand still in the scene .

    Nice regards :)

  • I tried ensureVisible() and centerOn() but unfortunately that didn't work , even tho i only have one instance of the item in the scene .
    Does anyone have any idea what to do ?

    I'll be grateful for your support :)

  • This is not how the Qt Graphics Framework works (note the link, I'd strongly suggest giving it a read before starting to work with it).
    You can't fix an item in the scene and let all the others move around. What you would do is having all the other items fixed and move that one item around.

    Keeping it centered on the scene is also not possible. all you can do is keeping it centered on the view. For stuff like QGraphicsView::centerOn() to work the viewport must actually be able to scroll. This means that if your scene is too small / your view to large (ignoring the scaling here) it won't move at all. You might want to play around in the 40000 Chips example to get a feeling what it means when the scene is moving and when the viewport is just too large to do any panning at all.
    I assume that this explanation didn't help at all but increased confusion instead. It should be a lot clearer to you once you read the linked documentation and played around with some of the examples.

  • @Joel-Bodenmann

    Huge thanks for your support , i got one step forward to solving my problem thanks to your advices .

  • I had misconfigured ensureVisible's margins , and the view's size and the scene's rect .
    Now it works fine :)