Nominate our 2022 Qt Champions!

[SOLVED]How to get notified on QGraphicsView viewport update.

  • Is there a way to receive a signal every time a QGraphicsView in my app has to update?
    For example every time the user is zooming or dragging the content displayed in it, I should be able to display an appropriate message like 'contents updated' or something similar.

  • What exactly do you mean by "every time a QGraphicsView has to update".
    Do you mean changes to the scene (e.g. GraphicsItems are moved)?
    Do you mean changes to the transformation of the view (e.g. zoom or scroll)?
    Do you mean repaints?

  • No actually what I had in mind are zooming scrolling, etc.

  • What you need is to know about changes to the view's transformation, then. There's no signal for that.

    Two ideas:

    1. If you control all means by which the user can scroll and zoom, you automatically know when the transformation changes.
    2. You could overload the paint event of QGraphicsView. Call the base class paint event (so your scene is actually painted), but also compare the current transformation to one you saved previously. If the transformation matrix has changed, you can do whatever you need to do. If you want to, you can further inspect which members of QTransform have changed, so you can find out whether the change was scrolling and/or zooming, or rotating,...

  • Sounds like an idea. I'll give it a try and see how it goes.
    Thanks for the suggestion.

  • Your second suggestion worked just fine.

  • One more question though.
    How can I have access to this transformation matrix?
    I can't seem to be able to tell where exactly inside my graphicsView my image is located.

  • You get the transformation directly from the QGraphicsView (transform() or viewportTransform(), unfortunately I don't remember which, and the docs aren't very specific about it). Also, within the paint event, you can get the combined transformation directly from QPainter.

Log in to reply