ROBOTechnics
ROBOTechnics

Reputation: 21

PYQT5 mdiarea how to check if Subwindow is inside workspace

I am currently writing a pyqt5 application with several widgets using a qmdiarea and mdisubwindows. I have a main subwindow showing an image and several widgets associated to that main subwindow. Eventually, I want to get rid of all accesory widgets. Although I receive a warning message, not an error, I would like to get rid of this warning message.

This is the way I create the Subwindows and how I close them:

Subwindow Creation:

        self.LUTobj         = QMdiSubWindow()    
        self.DATAobj        = QMdiSubWindow()    
        self.MEASUREobj     = QMdiSubWindow()    
        self.REFERENCEobj   = QMdiSubWindow()    
        self.SPATIALRESOLobj= QMdiSubWindow()    
        self.LINEPROFILEobj = QMdiSubWindow()    
        self.SNRobj         = QMdiSubWindow()   
        self.CNRobj         = QMdiSubWindow()   

Subwindow delete:

def closeSubwindowObject(self):

        if self.LUTobj is not None:
            self.mdiArea.removeSubWindow (self.LUTobj)

        if self.DATAobj is not None:
            self.mdiArea.removeSubWindow (self.DATAobj)

        if self.MEASUREobj is not None:
            self.mdiArea.removeSubWindow (self.MEASUREobj)

        if self.REFERENCEobj is not None:
            self.mdiArea.removeSubWindow (self.REFERENCEobj)

        if self.SPATIALRESOLobj is not None:
            self.mdiArea.removeSubWindow (self.SPATIALRESOLobj)

        if self.LINEPROFILEobj is not None:
            self.mdiArea.removeSubWindow (self.LINEPROFILEobj)

        if self.SNRobj is not None:
            self.mdiArea.removeSubWindow (self.SNRobj)

        if self.CNRobj is not None:
            self.mdiArea.removeSubWindow (self.CNRobj)

This is the warning message that it is shown:

QMdiArea::removeSubWindow: window is not inside workspace

Any clue on how to check is the window is inside the workspace?

Upvotes: 2

Views: 666

Answers (1)

eyllanesc
eyllanesc

Reputation: 244013

The warning message indicates that you want to remove a QMdiSubWindow that has already been removed or has never been part of the QMdiArea. So to avoid these errors you should verify that the QMdiSubWindow is in the list of QMdiSubWindows added using the subWindowList() method:

if self.LUTobj in self.mdiArea.subWindowList():
    self.mdiArea.removeSubWindow(self.LUTobj)

If you want to remove all the QMdiSubWindow then you just have to iterate over the previous list:

for w in self.mdiArea.subWindowList(): 
    self.mdiArea.removeSubWindow(w)

On the other hand, if you want to remove it by pressing the "X" button, you can enable the Qt::WA_DeleteOnClose attribute that will cause the QMdiSubWindow to be removed by notifying the QMdiArea by having it also removed from its list

sub_window.setAttribute(Qt.WA_DeleteOnClose)

Upvotes: 1

Related Questions