Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gc
- import logging
- import os
- import psutil
- from PySide2 import QtWidgets, QtCore
- def create_timer():
- timer = QtCore.QTimer(qApp)
- timer.setSingleShot(True)
- timer.setInterval(10)
- timer.start()
- return timer
- logging.basicConfig()
- L = logging.getLogger()
- L.setLevel(1)
- qApp = QtWidgets.QApplication(os.sys.argv)
- TIMER = create_timer()
- WINDOW = QtWidgets.QPushButton("Quit me!")
- WINDOW.clicked.connect(qApp.quit)
- WINDOW.show()
- COUNTER = 0
- ACTUAL_WINDOW = None
- class ManagedSubWindow(QtWidgets.QWidget):
- """Class fixing the multiple concurrent window problem.
- It "remembers" on the class level all its children, until they are to be
- destroyed."""
- active_sub_windows = set() # register of still active windows - prevention
- # against garbage collecting and SigSeg (11) signal = exitcode 139
- def __init__(self, *args, **kwargs):
- """Register the window on its creation"""
- kwargs["parent"] = WINDOW
- super().__init__(*args, **kwargs)
- self.setWindowFlags(QtCore.Qt.Window |
- QtCore.Qt.WindowMinimizeButtonHint |
- QtCore.Qt.WindowMaximizeButtonHint |
- QtCore.Qt.WindowCloseButtonHint)
- self.active_sub_windows.add(self)
- self.internals = kwargs.get("internals")
- def closeEvent(self, ce):
- """Un-register the window + delete what is inside"""
- # L.debug("ManagedSubWindow.closeEvent received %s", ce)
- if self not in self.active_sub_windows:
- L.debug("This window was yet closed... strange.")
- self.active_sub_windows.discard(self)
- del self.internals
- self.setParent(None)
- ce.accept()
- self.deleteLater()
- ret_value = super().closeEvent(ce)
- self.destroy(destroyWindow=True, destroySubWindows=True)
- return ret_value
- def create_window():
- """Create a subwindow"""
- global COUNTER
- global ACTUAL_WINDOW
- global TIMER
- COUNTER += 1
- if not (COUNTER % 100):
- gc.collect()
- process = psutil.Process(os.getpid())
- used_ram = process.memory_info().rss
- used_ram /= 2. ** 20
- L.debug("active_sub_windows count %s ; used_ram %s",
- len(ManagedSubWindow.active_sub_windows), used_ram)
- ACTUAL_WINDOW = ManagedSubWindow()
- ACTUAL_WINDOW.show()
- TIMER = create_timer()
- TIMER.timeout.connect(close_window)
- def close_window():
- """Close a subwindow"""
- global ACTUAL_WINDOW
- global TIMER
- ACTUAL_WINDOW.close()
- TIMER = create_timer()
- TIMER.timeout.connect(create_window)
- TIMER.timeout.connect(create_window)
- os.sys.exit(qApp.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement