[PyQt] QMdiArea persistence issue with closed windows

Hans-Peter Jansen hpj at urpla.net
Wed Oct 13 14:44:58 BST 2010


Hi Phil,

while looking at dynamic resizing widgets on irc, David Boddie created a 
small script, that I improved a little bit, which shows a small bug 
with mdi windows. According to the doc of QMdiArea::addSubWindow, 
closing windows should stay hidden, but if you close the mdi window, 
and adds another button, it tracebacks with:

Traceback (most recent call last):
  File "dynresize.py", line 29, in addRow
    if self.subwindow.isHidden():
RuntimeError: underlying C/C++ object has been deleted

Here the code:

#!/usr/bin/env python

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import *
import os, sys

class Window(QWidget):

    def __init__(self):

        QWidget.__init__(self)
        self._buttonNr = 1

        mdi = QMdiArea()
        frame = QFrame()
        self.frameLayout = QVBoxLayout(frame)
        self.subwindow = mdi.addSubWindow(frame)

        button = QPushButton("Add button")

        button.clicked.connect(self.addRow)

        layout = QVBoxLayout(self)
        layout.addWidget(mdi)
        layout.addWidget(button)

    def addRow(self):
        if self.subwindow.isHidden():
            self.subwindow.show()

        button = QPushButton("Remove button %i" % self._buttonNr)
        button.clicked.connect(self.delRow)

        self._buttonNr += 1
        self.frameLayout.addWidget(button)
        QTimer.singleShot(0, self.subwindow.adjustSize)

    def delRow(self):

        button = self.sender()
        button.hide()
        self.frameLayout.removeWidget(button)
        button.deleteLater()
        QTimer.singleShot(0, self.subwindow.adjustSize)



if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())


Pete


More information about the PyQt mailing list