正确关闭/删除QMdiArea子窗口的方法?

时间:2015-01-19 19:42:59

标签: python qt pyqt qmdiarea

我有一个简单的应用程序,我正在使用PyQt 4编写,其中包含一个由QMdiArea组成的MainWindow。当用户启动新会话(新实验)时,会生成两个窗口。用户还可以从分析模块列表中进行选择以进行运行。

我不希望允许任何窗口的任何重复(即,在下面的示例代码中,用户只能有一个file_tree_window,一个data_plot_window和/或一个cell_monitor_window)。

因此,例如,如果用户选择“新实验”一次,则生成两个窗口。如果他/她再次选择“新实验”,则应关闭原来的两个窗口并打开两个新窗口。任何“分析模块”都是如此。 (即选择一个已经打开的将导致打开的一个关闭而一个新的打开)。

虽然我无法生成此功能。

首先,代码:

class MainWindow(QtGui.QMainWindow):

    def __init__(self, model):
        super().__init__()
        self.resize(1400, 800)

        self.model = model

        menubar = self.menuBar()
        new_experiment_action = QtGui.QAction("New Experiment", self)
        new_experiment_action.triggered.connect(self.setup_new_experiment)

        file_menu = menubar.addMenu("File")
        file_menu.addAction(new_experiment_action)

        cell_health_action = QtGui.QAction("Cell Health Monitor", self)
        cell_health_action.triggered.connect(self.setup_cell_health_window)

        analysis_menu = menubar.addMenu("Analysis Modules")
        analysis_menu.addAction(cell_health_action)

        self.file_tree = None
        self.file_tree_window = None

        self.data_plot = None
        self.data_plot_window = None

        self.cell_health = None
        self.cell_health_window = None

        self.mdi = QtGui.QMdiArea()
        self.setCentralWidget(self.mdi)

    def setup_new_experiment(self):
        self.mdi.closeAllSubWindows()

        self.file_tree = FileTree(self.model)
        self.file_tree.setMinimumSize(QtCore.QSize(200, 300))

        self.data_plot = DataPlot(self.model)
        self.data_plot.setMinimumSize(self.size()*.4)

        self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
        self.file_tree_window.show()

        self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
        self.data_plot_window.show()

    def setup_cell_health_window(self):
        if self.cell_health_window:
            self.mdi.removeSubWindow(self.cell_health_window)

        self.cell_health = CellHealthMonitor(self.model)
        self.cell_health.setMinimumSize(self.size()*.3)

        self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
        self.cell_health_window.show() 

这在许多方面都不起作用:

  • 如果我打开“Cell Health Monitor”窗口,请将其关闭,然后尝试 重新打开它,Python崩溃
  • 如果我打开了“Cell Health Monitor”窗口,然后尝试启动“新实验”,然后打开“Cell Health Monitor”窗口,则Python崩溃

我尝试使用remove()而不是close():

即:

def setup_new_experiment(self):
    for window in self.mdi.subWindowList():
        self.mdi.removeSubWindow(window)

    self.file_tree = FileTree(self.model)
    self.file_tree.setMinimumSize(QtCore.QSize(200, 300))

    self.data_plot = DataPlot(self.model)
    self.data_plot.setMinimumSize(self.size()*.4)

    self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
    self.file_tree_window.show()

    self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
    self.data_plot_window.show()

def setup_cell_health_window(self):
    if self.cell_health_window:
        self.mdi.removeSubWindow(self.cell_health_window)

    self.cell_health = CellHealthMonitor(self.model)
    self.cell_health.setMinimumSize(self.size()*.3)

    self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
    self.cell_health_window.show()

根据一系列操作,Python会崩溃,或者我收到以下两条错误消息之一:

(这个实际上只是一个警告):

QMdiArea::removeSubWindow: window is not inside workspace

Traceback (most recent call last):
  File "MainWindow.py", line 61, in setup_cell_health_window
    self.mdi.removeSubWindow(self.cell_health_window)
RuntimeError: wrapped C/C++ object of type QMdiSubWindow has been deleted

关于如何以不同的方式删除子窗口而不产生这些问题,我感到很茫然。

由于

0 个答案:

没有答案