denysonique
denysonique

Reputation: 16574

Loading QtDesigner's .ui files in PySide

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.

Upvotes: 35

Views: 37496

Answers (5)

Alex
Alex

Reputation: 374

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)

Upvotes: 1

Emrexdy
Emrexdy

Reputation: 166

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

Upvotes: 0

lofidevops
lofidevops

Reputation: 16932

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

Upvotes: 7

BarryPye
BarryPye

Reputation: 2082

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

Upvotes: 54

Stephen Terry
Stephen Terry

Reputation: 6279

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

Upvotes: 41

Related Questions