0

我遇到了一个问题。将子窗口添加到mdiarea时,整个程序占用的内存会逐渐增加,但是当mdiarea中的子窗口关闭时,资源管理器无法回收它的内存,下面是一个演示代码,我已经创建了一个主窗口tabwidget,其中包含一系列选项卡,每个选项卡包含一个 mdiArea 小部件,在 mdiarea 中可以添加或删除多个子窗口:

# -*- coding: utf-8 -*-

"""

"""

import sys
from PyQt5 import QtWidgets, QtCore, sip

import matplotlib

import weakref

matplotlib.use("Qt5Agg")


class SubWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(300, 100)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)


class TabWidget(QtWidgets.QWidget):
    """
    """
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        self.vLayout = QtWidgets.QVBoxLayout(self)
        self.mdiArea = QtWidgets.QMdiArea(self)
        self.vLayout.addWidget(self.mdiArea)
        self.subWindows = []

    def insertSubWindow(self):
        sub_window = SubWindow(self)

        sub_window.destroyed.connect(self.removeSubWindow)

        self.subWindows.append(weakref.ref(sub_window, self.subWindows.remove))

        self.mdiArea.addSubWindow(sub_window)  # if this line is removed, then memory can be reclaimed.

        sub_window.show()

    def removeSubWindow(self):
        print(self.mdiArea.subWindowList())


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(800, 400)
        v_layout = QtWidgets.QVBoxLayout()

        self.tabWidget = QtWidgets.QTabWidget(self)
        self.tabWidget.setTabsClosable(True)
        self.tabWidget.tabCloseRequested.connect(self.removeTab)
        self.tabs = list()
        self.tabIndex = 0

        h_layout = QtWidgets.QHBoxLayout()
        self.pushButton_AddTab = QtWidgets.QPushButton('Add Tab', self)
        h_layout.addWidget(self.pushButton_AddTab)
        self.pushButton_AddFig = QtWidgets.QPushButton('Add Fig', self)
        h_layout.addWidget(self.pushButton_AddFig)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        h_layout.addItem(spacerItem)

        v_layout.addLayout(h_layout)
        v_layout.addWidget(self.tabWidget)

        widget = QtWidgets.QWidget()
        widget.setLayout(v_layout)
        self.setCentralWidget(widget)

        self.pushButton_AddTab.clicked.connect(self.insertTab)
        self.pushButton_AddFig.clicked.connect(self.insertFig)

    def insertTab(self):
        tab_name = 'Tab ' + str(self.tabIndex)
        self.tabIndex += 1
        new_tab = TabWidget(self)
        # new_tab.destroyed.connect(self.removeTab)
        self.tabs.append(weakref.ref(new_tab, self.tabs.remove))
        self.tabWidget.addTab(new_tab, tab_name)

    def removeTab(self, index):
        self.tabWidget.removeTab(index)

    def insertFig(self):
        tab_current = self.tabWidget.currentWidget()
        tab_current.insertSubWindow()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    mw = MainWindow()
    mw.show()
    app.exec_()
4

0 回答 0