34

I am looking for a simple example of how to directly load a QtDesigner generated .ui file into a Python application.

I simply would like to avoid using pyuic4.

1

5 Answers 5

53

For the complete noobs at PySide and .ui files, here is a complete example:

from PySide import QtCore, QtGui, QtUiTools


def loadUiWidget(uifilename, parent=None):
    loader = QtUiTools.QUiLoader()
    uifile = QtCore.QFile(uifilename)
    uifile.open(QtCore.QFile.ReadOnly)
    ui = loader.load(uifile, parent)
    uifile.close()
    return ui


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = loadUiWidget(":/forms/myform.ui")
    MainWindow.show()
    sys.exit(app.exec_())
3
  • Try this @Necronomicron
    – 101
    Dec 10, 2014 at 21:03
  • How can I access UI widgets once I've loaded the UI this way?
    – texnic
    Feb 23, 2019 at 11:48
  • Found an answer here: window = loader.load('filename.ui', parent); my_widget = window.my_widget_name.
    – texnic
    Feb 23, 2019 at 22:14
41

PySide, unlike PyQt, has implemented the QUiLoader class to directly read in .ui files. From the linked documentation,

loader = QUiLoader()
file = QFile(":/forms/myform.ui")
file.open(QFile.ReadOnly)
myWidget = loader.load(file, self)
file.close()
3
  • 24
    Just to help noobs along: QUiLoader is from PySide.QtUiTools.QUiLoader
    – brews
    Mar 15, 2013 at 18:07
  • 1
    All I get is this: QPixmap: Must construct a QApplication before a QPaintDevice Aborted (core dumped)
    – Lucio
    Jun 2, 2013 at 3:08
  • 3
    @Lucio The code snippet in this answer cannot be used in isolation. Follow BarryPye's answer for a complete example.
    – JBentley
    May 5, 2014 at 20:30
6

Another variant, based on a shorter load directive, found on https://askubuntu.com/questions/140740/should-i-use-pyqt-or-pyside-for-a-new-qt-project#comment248297_141641. (Basically, you can avoid all that file opening and closing.)

import sys
from PySide import QtUiTools
from PySide.QtGui import *

app = QApplication(sys.argv)
window = QtUiTools.QUiLoader().load("filename.ui")
window.show()
sys.exit(app.exec_())

Notes:

  • filename.ui should be in the same folder as your .py file.
  • You may want to use if __name__ == "__main__": as outlined in BarryPye's answer
1
  • This one actually works with PySide2. Just add from PySide2.QtWidgets import QApplication
    – texnic
    Feb 23, 2019 at 10:52
1

For people coming from PyQt5/6 who are thoroughly confused by this:

PySide does not have the same functionality that we're used to, which is to load the ui file at the top of the window/widget subclass like so:

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        
        '''Load ui'''
        uic.loadUi("mainwindow.ui", self)

There is nothing very similar to this in PySide.

Instead, the best thing to do is embrace the ui-file compilation that you've avoided because loading the ui file is so easy in PyQt. This has a couple of advantages

  • There is a performance advantage - the ui file does not need to be compiled at run time
  • You get type hints for all your widgets without needing to manually add them

The disadvantage is that you have to use pyside6-uic to compile the *.ui files each time you edit them, but this can be made less painful by using scripts to automate the process - either setting it up in VSCode, a batch file or a powershell script. After you've done this, the code is nice:

#ui_mainwindow is the ui_mainwindow.py file
#Ui_MainWindow is the class that was produced within that .py file
from ui_mainwindow import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        '''Load the ui'''
        self.setupUi(self)

3
  • See this answer for a fairly simple implementation of loadUi for PySide.
    – ekhumoro
    Oct 19, 2022 at 13:01
  • "You get type hints for all your widgets without needing to manually add them" Does this work for type checkers like mypy? I don't see how they would pick up on the typehints if it's all in a setupUi() method instead of __init__()
    – Ben Jones
    May 10 at 15:44
  • Hi Ben, I haven't tried mpy myself but the classes are all inherited from the ui*.py file that uic generates so it's just like any other python code. If mpy can handle pyside classes then it should work.
    – Alex
    May 19 at 8:38
0

Here is some example for PySide6 and Windows. (For linux you need use /, not \\)

from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import QFile
from PySide6.QtWidgets import QApplication
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    loader = QUiLoader()
    file = QFile("gui.ui")
    file.open(QFile.ReadOnly)
    ui = loader.load(file)
    file.close()
    ui.show()
    sys.exit(app.exec_())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.