Refine Qt top-level widget lifetimes

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2026-08-12 12:20:48 +08:00
parent 11aae2e244
commit b111be2ffc
5 changed files with 122 additions and 176 deletions
-24
View File
@@ -30,18 +30,10 @@ from PySide6.QtGui import *
from PySide6.QtWidgets import *
import io
import functools
import pyqrcode
__all__ = ['QRCodeWindow']
_OpenQRCodeWindows = {}
def _releaseOpenQRCodeWindow(key, *_args):
"""Release an exported QR-code window after Qt destroys it."""
_OpenQRCodeWindows.pop(key, None)
class QRCodeWindow(AppQMainWindow):
"""Present the QR code window."""
@@ -54,28 +46,12 @@ class QRCodeWindow(AppQMainWindow):
self.setFixedSize(640, 640)
self.setAttribute(QtCore.Qt.WidgetAttribute.WA_DeleteOnClose, True)
self._lifetimeKey = id(self)
self.destroyed.connect(
functools.partial(_releaseOpenQRCodeWindow, self._lifetimeKey)
)
self.tabWidget = QTabWidget(self)
self.tabWidget.setTabsClosable(True)
self.tabWidget.tabCloseRequested.connect(self.handleTabCloseRequested)
self.setCentralWidget(self.tabWidget)
def show(self):
"""Show and retain this unparented window until Qt destroys it."""
_OpenQRCodeWindows[self._lifetimeKey] = self
try:
return super().show()
except Exception:
_releaseOpenQRCodeWindow(self._lifetimeKey)
raise
def tabCount(self) -> int:
"""Return the tab count value used by the QR code window."""
return self.tabWidget.count()