Retain QR code windows while open

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2026-08-12 12:20:15 +08:00
parent 3966c5e047
commit 556e97f691
+25
View File
@@ -29,10 +29,18 @@ 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."""
@@ -43,6 +51,12 @@ class QRCodeWindow(AppQMainWindow):
self.setWindowTitle(_(APPLICATION_NAME))
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)
@@ -50,6 +64,17 @@ class QRCodeWindow(AppQMainWindow):
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()