How is QMdiArea supposed to be used?

Heads up! You've already completed this tutorial.

Hodor | 2021-05-27 21:55:38 UTC | #1

First off, Martin Fitzpatrick, if you here and are reading this, THANK YOU! I've been coding with PyQt for about a year or so before I came across this site last night I was surprised by how much I got for the price of your book.. This is probably going to be my go-to resource for quite some time now.

That being said, I have a question about the use of QMdiArea. I couldn't find anything on it in the books or anything on your site about it. What I am trying to figure out is how to have a QMdiSubWindow be closeable but not delete itself when using a ui created in QtDesigner...

I am instantiating and subclassing the UI by doing:

python
class GUI(QMainWindow, MyUiDesignerCreatedObject):
    def __init__(self):
        super()__init__()
        self.setupUi(self)

I created the QMdiArea in QtDesigner and had found that in the generated code of the compiled ui file, the QMdiSubWindows I made are being instantiated as QWidget but the Qt Documentation says that anything but QMdiSubWindows will get deleted on close which explains why I am not able to re-open any of my subwindows whenever I(or my users) close them...

Is there a workaround for this or is there something I am using improperly with PyQt or should I be creating my subwindows seperately as a seperate file instead of adding them as subwindows in QtDesigner which doesn't seem to get added as a subwindow in the generated code of the compiled ui file?

The subwindows will reopen fine if I call my_subwindow.show() but self.my_mdi_area.subWindowList() returns an empty list until I run sel.my_mdi_area.addSubWindow(self.my_subwindow) but doing so will cause the subwindow to get deleted because of the whole being instantiated as QWidget and not QMdiSubWindow thing...

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

Also, adding the subwindow seems to also trigger a .show() signal for it when all I am trying to do is attatch all my subwindows.

The expected behavior is for the QMdiArea of my UI to have all its pre-designed subwindows attatched but defaulted as hidden and only show when triggered by an action...


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

How is QMdiArea supposed to be used? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.