title: How to use custom widget and uic.loadUi without importing custom widget class package?
body: <p>Using PyQt4's <a href="http://pyqt.sourceforge.net/Docs/PyQt4/designer.html#PyQt4.uic.loadUi" rel="nofollow">uic.loadUi</a>, I’d like to load a .ui file and use a custom widget in it. This means using the third <code>package</code> argument of <code>uic.loadUi</code> which will import the package with the custom widget's class inside.</p>
  
  <p>However, I wish to define the custom widget's class in the same file as where I'm calling <code>uic.loadUi</code>. I’m trying to achieve this like so:</p>
  
  <pre><code>class MyCustomClass(QtWidgets.QPushButton):
      """ This is my custom class for my custom widget """
      def __init__(self, *args):
          QtWidgets.QPushButton.__init__(self, *args)
  
  ...
  
  sys.modules['mycustompackage'] = MyCustomClass
  uic.loadUi('my_ui.ui', self, 'mycustompackage')  # Loads .ui file which contains the MyCustomWidget widget
  </code></pre>
  
  <p>However, this returns the following error:</p>
  
  <pre><code>AttributeError: type object 'MyCustomClass' has no attribute 'MyCustomWidget'
  </code></pre>
  
  <p><strong>Is there anything I could do to make this actually work?</strong> I suspect that <code>MyCustomClass</code> isn't defined in the manner <code>uic.loadUi</code> expects it.</p>
  
  <p>In Qt Designer, I've promoted <code>MyCustomWidget</code>:</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;widget class="MyCustomWidget" name="customWidget"&gt;
      &lt;property name="geometry"&gt;
       &lt;rect&gt;
        &lt;x&gt;50&lt;/x&gt;
        &lt;y&gt;70&lt;/y&gt;
        &lt;width&gt;113&lt;/width&gt;
        &lt;height&gt;32&lt;/height&gt;
       &lt;/rect&gt;
      &lt;/property&gt;
      &lt;property name="text"&gt;
       &lt;string&gt;PushButton&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&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;22&lt;/height&gt;
      &lt;/rect&gt;
     &lt;/property&gt;
    &lt;/widget&gt;
    &lt;widget class="QStatusBar" name="statusbar"/&gt;
   &lt;/widget&gt;
   &lt;customwidgets&gt;
    &lt;customwidget&gt;
     &lt;class&gt;MyCustomWidget&lt;/class&gt;
     &lt;extends&gt;QPushButton&lt;/extends&gt;
     &lt;header&gt;MyCustomClass&lt;/header&gt;
    &lt;/customwidget&gt;
   &lt;/customwidgets&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <hr>
  
  <h1>Solution</h1>
  
  <p>I solved it using the above .ui file like this:</p>
  
  <pre><code>class MyCustomClasses(object):
      class MyCustomWidget(QtWidgets.QPushButton):
          def __init__(self, *args):
              QtWidgets.QPushButton.__init__(self, *args)
  
  ...
  
  sys.modules['MyCustomClasses'] = MyCustomClasses
  uic.loadUi('my_ui.ui', self)  # Loads .ui file which contains MyCustomWidget
  </code></pre>