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.

denysonique
  • 16,235
  • 6
  • 37
  • 40
  • For PySide implemenation of loadUi, see: [How do I load children from .ui file in PySide](https://stackoverflow.com/q/27603350/984421). – ekhumoro Oct 19 '22 at 13:03

5 Answers5

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_())
BarryPye
  • 1,975
  • 2
  • 18
  • 19
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()
qasfux
  • 207
  • 1
  • 8
Stephen Terry
  • 6,169
  • 1
  • 28
  • 31
  • 24
    Just to help noobs along: QUiLoader is from `PySide.QtUiTools.QUiLoader` – brews Mar 15 '13 at 18:07
  • 1
    All I get is this: `QPixmap: Must construct a QApplication before a QPaintDevice Aborted (core dumped)` – Lucio Jun 02 '13 at 03:08
  • 3
    @Lucio The code snippet in this answer cannot be used in isolation. Follow BarryPye's answer for a complete example. – JBentley May 05 '14 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
Community
  • 1
  • 1
lofidevops
  • 15,528
  • 14
  • 79
  • 119
  • This one actually works with PySide2. Just add `from PySide2.QtWidgets import QApplication` – texnic Feb 23 '19 at 10:52
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_())
Marvin Noll
  • 585
  • 1
  • 7
  • 23
Emrexdy
  • 156
  • 1
  • 2
  • 8
0

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)

Alex
  • 172
  • 9
  • See [this answer](https://stackoverflow.com/a/27610822/984421) for a fairly simple implementation of loadUi for PySide. – ekhumoro Oct 19 '22 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 '23 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 '23 at 08:38