2

QMdiArea で構成される MainWindow を含む PyQt 4 を使用して作成している単純なアプリケーションがあります。ユーザーが新しいセッション (新しい実験) を開始すると、2 つのウィンドウが生成されます。ユーザーは、実行する分析モジュールのリストから選択することもできます。

ウィンドウの複製を許可したくありません (つまり、以下のコード例では、ユーザーは 1 つの file_tree_window、1 つの data_plot_window、および/または 1 つの cell_monitor_window しか持つことができません)。

したがって、たとえば、ユーザーが「新しい実験」を 1 回選択すると、2 つのウィンドウが生成されます。その後、再度「新しい実験」を選択すると、元の 2 つのウィンドウが閉じられ、新しい 2 つのウィンドウが開かれます。どの「分析モジュール」も同様です。(つまり、既に開いているものを選択すると、開いているものが閉じられ、新しいものが開きます)。

ただし、この機能の生成に問題があります。

まず、コード:

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」ウィンドウを開いてから「New Experiment」を開始しようとした後、「Cell Health Monitor」ウィンドウを開こうとすると、Python がクラッシュします。

close() の代わりに remove() を使用しようとしました:

すなわち:

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 がクラッシュするか、次の 2 つのエラー メッセージのいずれかが表示されます。

(これは実際には単なる警告です):

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

これらの問題を発生させずにサブウィンドウを削除するにはどうすればよいか、私は途方に暮れています。

ありがとう

4

0 に答える 0