Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 978
ninjamosh Avatar asked Dec 13 '11 16:12

ninjamosh


People also ask

How to create UI File in Qt?

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.

What is .UI file in Qt?

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.

What is a UI file?

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.


1 Answers

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.

like image 61
Dusty Campbell Avatar answered Oct 01 '22 22:10

Dusty Campbell