Connecting signals to slots up the directory tree with PySide
<p>I am trying to separate UI components from functional code as much as I can, so my PySide is application structured like this</p>

<pre><code>main.py
package\
   app.py
   __init__.py
   ui\
      mainwindow.py
      __init__.py   
</code></pre>

<p>Main is just a simple starter</p>

<pre><code>if __name__ == '__main__':
    import sys
    from package import app
    sys.exit(app.run())
</code></pre>

<p>and app is where all the functionality should reside. I start the UI from app.py</p>

<pre><code>from PySide import QtCore, QtGui

@QtCore.Slot()
def start_button_clicked():
    print "started"

def run():
    import sys
    from ui.mainwindow import Ui_MainWindow
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
</code></pre>

<p>Now, from the user interface I want to connect the emitted signals up to the app.py to avoid having a lot of functionality cluttering the mainwindow file, but the UI file is not aware of app.py. How should I go forth to do this and avoid all slots being in mainwindow.py? app.py should easily be able to do stuff on the UI since it has a object reference to it, but I have no clue on the other way around. </p>

<p>Create a subclass for the top-level widget from Qt Designer. Using this approach, all of child widgets from Qt Designer will become attributes of the subclass:</p>

<pre><code>import sys
from PySide import QtCore, QtGui
from ui.mainwindow import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.start_button.clicked.connect(self.start_button_clicked)

    def start_button_clicked(self):
        print "started"

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
</code></pre>

python pyside signals-slots qt-designer