Search code examples
pythonpysideqt-designer

Using QUiLoader and UI files in PySide to dynamically create user interface at run-time


I'm really having a hard time connecting slots from Python to Qt Designer UI files. I've been through all tutorials I could find on PySide (ex: http://zetcode.com/gui/pysidetutorial/eventsandsignals/)

Its quite easy when you set up the GUI in code, but we really would like to use Qt Designer and UI files.

Some other threads just points to the use of pyuic to convert .ui to .py files, but if its possible I would really like to do this at run-time.

Here is my code so far. I have no clue how to connect the connectBtn to the Connect in the UI file :

def initUI(self):      

    loader = QUiLoader()
    file = QFile("designer_test.ui")
    file.open(QFile.ReadOnly)
    myWidget = loader.load(file, self)
    #print(dir(myWidget))
    file.close()

    layout = QtGui.QVBoxLayout()
    layout.addWidget(myWidget)
    self.setLayout(layout)

    connectBtn = QtGui.QPushButton("Connect", self)

    connectBtn.clicked.connect(self.connectClicked)

    myWidget.setWindowTitle('Window')
    myWidget.show()

def connectClicked(self):
    print("works")

Solution

  • Have you checked this page: Using a Designer UI File in Your Application

    It is for C++, but I think the concepts are the same as what you're trying to do in python.

    According to that page, to get the widgets that are created by the Ui file you need to call findChild().

    Also, this question.