6

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")
Stan
  • 8,710
  • 2
  • 29
  • 31
ninjamosh
  • 63
  • 1
  • 3

2 Answers2

3

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.

Community
  • 1
  • 1
Dusty Campbell
  • 3,146
  • 31
  • 34
0

I've made this auto-connector to help me with this... please take a look at it.

Gustavo Vargas
  • 2,497
  • 2
  • 24
  • 32
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Uyghur Lives Matter Jul 20 '16 at 13:09