code: def closeEvent(self, event):
          
          self.plugin.dockwidget.setWidget(self.plugin)
          self.plugin.dockwidget.setVisible(True)
          self.plugin.switch_to_plugin()
          QMainWindow.closeEvent(self, event)
          self.plugin.undocked_window = None
docs: Reimplement Qt method.