Loading QtDesigner's .ui files in PySide Loading QtDesigner's .ui files in PySide python python

Loading QtDesigner's .ui files in PySide


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

from PySide import QtCore, QtGui, QtUiToolsdef loadUiWidget(uifilename, parent=None):    loader = QtUiTools.QUiLoader()    uifile = QtCore.QFile(uifilename)    uifile.open(QtCore.QFile.ReadOnly)    ui = loader.load(uifile, parent)    uifile.close()    return uiif __name__ == "__main__":    import sys    app = QtGui.QApplication(sys.argv)    MainWindow = loadUiWidget(":/forms/myform.ui")    MainWindow.show()    sys.exit(app.exec_())


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()


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 sysfrom PySide import QtUiToolsfrom 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