Code executed in a function will not work as expected

Hi, I’m playing with this piece of program now Canvas Render - How to? - #7 by AkiR
However, when I put this piece of code

active_window = Application.activeWindow()
active_view = active_window.activeView()
if active_view.document() is None:
    raise RuntimeError('Document of active view is None!')
my_overlay = MyOverlay(active_view)
my_overlay.show()

into a function and call that function

def draw():
    active_window = Application.activeWindow()
    active_view = active_window.activeView()
    if active_view.document() is None:
        raise RuntimeError('Document of active view is None!')
    my_overlay = MyOverlay(active_view)
    my_overlay.show()

draw()

It will not work as expected. From the printed log I can see that draw(), MyOverlay's __init__ and eventFilter are called once, but paintEvent is not called, therefore no triangles are shown.
I don’t why putting the part of code into a function can make such a difference.
I’m making an extension, so code like this has to be called from a function connected to the user’s action.

I guess the MyOverlay widget simply ceases to exist at the end of the function call, since it is only held by a local variable, hence it never gets a paint event.

2 Likes

Well, my bad, my bad :sweat_smile: