raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
852 stars 180 forks source link

[HOW-TO] Detect when preview window is closed by the user (using top right X button) #953

Closed jareff-g closed 7 months ago

jareff-g commented 7 months ago

Describe what it is that you want to accomplish I have a Picamera2 preview window that is controlled (opened/closed) by my application, however I need to handle the use case where the preview window is directly closed by the user, by clicking on the window X button, in order to synchronize the status on the application with the real PiCamera2 status.

Describe alternatives you've considered After having a look at past issues (nothing found there) and also at PiCamera2 code (more specifically picamera2.py), the only way I found to do what I need is to use '_preview' property. My question is whether or not this is the right way to do it, just to be sure it will not be broken by some future update.

Thanks in advance for any feedback. Cheers, Juan

davidplowman commented 7 months ago

Yes, when the preview goes away spontaneously (a user closing it for example), I think it calls the detach_preview method. So you could intercept that and implement something in there. I can't 100% guarantee that will never change, but I have no plans to do any work on Qt and GUIs ever again if I can possibly help it, so hopefully it will remain as it is. Or there's probably some Qt callback on the window itself that you could catch... I don't really know enough about Qt.

The Qt preview window is just a convenience to save folks from having to do lots of work just to see a camera image, whilst leaving the main Python thread free for interactive commands. However, I really don't think this is the "right" way to use Qt (indeed coercing it behave like this is quite awkward). For this reason I think folks who want to interact more closely with the camera and GUI should consider writing their own Qt application, with a Picamera2 widget embedded. I don't know if that would work in your application?

jareff-g commented 7 months ago

Hello David,

Thanks for your quick answer, after reading it I better understand my situation. I'll try to explore the option you propose and build something custom in my Tkinter app. Cheers, Juan