title: Using a custom PySide2 widget in Qt Designer
body: <p>I'm searching for a way to use custom widgets in Qt Designer written with Qt for Python (PySide2) effectively.</p>
  
  <p>I found out, that it is possible to design the GUI with the base widget, then just swap out the class to my custom widget in the UI File and informing the <code>QUiLoader</code> about the subclass <code>loader.registerCustomWidget(MyMainWindow)</code>, but then opening it again in Qt Designer doesn't work that well.</p>
  
  <p>I read in <a href="https://stackoverflow.com/questions/16030170/custom-qt-widgets-with-python-for-qt-designer">this similar question for PyQt</a> that you have to write a Plugin for the custom widget. Does this possibility also exist for PySide2?</p>
  
  <p>Some example code:</p>
  
  <p><strong>custom_widget.py:</strong></p>
  
  <pre><code>import sys
  
  from PySide2.QtUiTools import QUiLoader
  from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QMessageBox, QFileDialog, QTextBrowser
  from PySide2.QtCore import QFile
  
  
  class MyMainWindow(QMainWindow):
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.setWindowTitle("Demo QtWidget App")
  
      def closeEvent(self, event):
          msgBox = QMessageBox()
          msgBox.setWindowTitle("Quit?")
          msgBox.setText("Exit application?")
          msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
          msgBox.setDefaultButton(QMessageBox.No)
          ret = msgBox.exec_()
          if ret == QMessageBox.Yes:
              event.accept()
          else:
              event.ignore()
  
  
  if __name__ == '__main__':
      app = QApplication([])
      file = QFile("custom_widget_original.ui")
      #file = QFile("custom_widget_modified.ui")
      file.open(QFile.ReadOnly)
      loader = QUiLoader()
      loader.registerCustomWidget(MyMainWindow)
      main_window = loader.load(file)
      main_window.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p><strong>custom_widget_original.ui</strong></p>
  
  <p>With this version the application is closed without question.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;800&lt;/width&gt;
      &lt;height&gt;600&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralwidget"&gt;
     &lt;layout class="QGridLayout" name="gridLayout"&gt;
      &lt;item row="0" column="0"&gt;
       &lt;widget class="QLabel" name="label_2"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item row="0" column="2"&gt;
       &lt;widget class="QLabel" name="label"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item row="0" column="1"&gt;
       &lt;widget class="QLabel" name="label_3"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
     &lt;/layout&gt;
    &lt;/widget&gt;
    &lt;widget class="QMenuBar" name="menubar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;800&lt;/width&gt;
       &lt;height&gt;21&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
     &lt;widget class="QMenu" name="menuFile"&gt;
      &lt;property name="title"&gt;
       &lt;string&gt;File&lt;/string&gt;
      &lt;/property&gt;
      &lt;addaction name="actionExit"/&gt;
     &lt;/widget&gt;
     &lt;addaction name="menuFile"/&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusbar"/&gt;
    &lt;action name="actionExit"&gt;
     &lt;property name="text"&gt;
      &lt;string&gt;Exit&lt;/string&gt;
     &lt;/property&gt;
    &lt;/action&gt;
   &lt;/widget&gt;
   &lt;resources/&gt;
   &lt;connections&gt;
    &lt;connection&gt;
     &lt;sender&gt;actionExit&lt;/sender&gt;
     &lt;signal&gt;triggered()&lt;/signal&gt;
     &lt;receiver&gt;MainWindow&lt;/receiver&gt;
     &lt;slot&gt;close()&lt;/slot&gt;
     &lt;hints&gt;
      &lt;hint type="sourcelabel"&gt;
       &lt;x&gt;-1&lt;/x&gt;
       &lt;y&gt;-1&lt;/y&gt;
      &lt;/hint&gt;
      &lt;hint type="destinationlabel"&gt;
       &lt;x&gt;399&lt;/x&gt;
       &lt;y&gt;299&lt;/y&gt;
      &lt;/hint&gt;
     &lt;/hints&gt;
    &lt;/connection&gt;
   &lt;/connections&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <p><strong>custom_widget_modified.ui</strong></p>
  
  <p>With this version you are asked if you really want to quit.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="MyMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;800&lt;/width&gt;
      &lt;height&gt;600&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralwidget"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;155&lt;/width&gt;
       &lt;height&gt;15&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
     &lt;layout class="QGridLayout" name="gridLayout"&gt;
      &lt;item row="0" column="0"&gt;
       &lt;widget class="QLabel" name="label_2"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item row="0" column="2"&gt;
       &lt;widget class="QLabel" name="label"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item row="0" column="1"&gt;
       &lt;widget class="QLabel" name="label_3"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;TextLabel&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
     &lt;/layout&gt;
    &lt;/widget&gt;
    &lt;widget class="QMenuBar" name="menubar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;800&lt;/width&gt;
       &lt;height&gt;21&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
     &lt;widget class="QMenu" name="menuFile"&gt;
      &lt;property name="title"&gt;
       &lt;string&gt;File&lt;/string&gt;
      &lt;/property&gt;
      &lt;addaction name="actionExit"/&gt;
     &lt;/widget&gt;
     &lt;addaction name="menuFile"/&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusbar"&gt;
     &lt;property name="geometry"&gt;
      &lt;rect&gt;
       &lt;x&gt;0&lt;/x&gt;
       &lt;y&gt;0&lt;/y&gt;
       &lt;width&gt;3&lt;/width&gt;
       &lt;height&gt;18&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
    &lt;action name="actionExit"&gt;
     &lt;property name="text"&gt;
      &lt;string&gt;Exit&lt;/string&gt;
     &lt;/property&gt;
    &lt;/action&gt;
   &lt;/widget&gt;
   &lt;customwidgets&gt;
    &lt;customwidget&gt;
     &lt;class&gt;MyMainWindow&lt;/class&gt;
     &lt;extends&gt;QWidget&lt;/extends&gt;
     &lt;header&gt;mymainwindow.h&lt;/header&gt;
     &lt;container&gt;1&lt;/container&gt;
    &lt;/customwidget&gt;
   &lt;/customwidgets&gt;
   &lt;resources/&gt;
   &lt;connections&gt;
    &lt;connection&gt;
     &lt;sender&gt;actionExit&lt;/sender&gt;
     &lt;signal&gt;triggered()&lt;/signal&gt;
     &lt;receiver&gt;MainWindow&lt;/receiver&gt;
     &lt;slot&gt;close()&lt;/slot&gt;
     &lt;hints&gt;
      &lt;hint type="sourcelabel"&gt;
       &lt;x&gt;-1&lt;/x&gt;
       &lt;y&gt;-1&lt;/y&gt;
      &lt;/hint&gt;
      &lt;hint type="destinationlabel"&gt;
       &lt;x&gt;399&lt;/x&gt;
       &lt;y&gt;299&lt;/y&gt;
      &lt;/hint&gt;
     &lt;/hints&gt;
    &lt;/connection&gt;
   &lt;/connections&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <p>The <code>&lt;customwidgets&gt;</code> segment is added by Qt Designer after opening it again.</p>
  
  <p>After modification Qt Designer doesn't place the three labels correctly.</p>