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")
To create a . ui file go to File -> New File or Project... In the window that appears select Qt under Files and Classes on the left, then select Qt Designer Form on the right. You'll notice the icon has "ui" on it, showing the type of file you're creating.
ui file is used to create a ui_calculatorform. h file that can be used by any file listed in the SOURCES declaration. Note: You can use Qt Creator to create the Calculator Form project. It automatically generates the main.
Stores the user interface configuration for a program; saved in an XML format and contains definitions of Qt widgets with slots and signals; can be viewed in a basic text editor or opened with a UI designer program.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With