PyQt: How do I load a ui file from a resource?
<p>In general, I load all my ui files via the <code>loadui()</code> method, and this works fine for me. This looks like this:</p>

<pre><code>#!/usr/bin/env python
#-*- coding:utf-8 -*-

'''
The modules for Qt are imported.
PyQt are a set of Python bindings for Qt.
'''
from PyQt4.QtGui import QDialog
from PyQt4.uic import loadUi
from PyQt4.QtCore import Qt, QFile
from PyQt4 import QtCore

class My_Window(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)

        UI_PATH = QFile(":/ui_file/test.ui")

        UI_PATH.open(QFile.ReadOnly)

        self.ui_test = loadUi(UI_PATH, self)

        UI_PATH.close()
</code></pre>

<p>Now I try to load the ui file via <code>loaduiType()</code>, but it doesn't work. I tried with this code:</p>

<pre><code>from PyQt4.uic import loadUiType

UI_PATH = QFile(":/ui_file/test.ui")

Ui_Class, _ = loadUiType(UI_PATH)

class Test_Window(QDialog, UiClass):
    def __init__(self, parent):
        QDialog.__init__(self, parent)

        self.setupUi(self)
</code></pre>

<p>What is the correct and best why to load the ui file with the <code>loadUiType()</code> method?</p>

<p>It's not really much different from what you were already doing:</p>

<pre><code>from PyQt4.QtCore import QFile
from PyQt4.uic import loadUiType

import resources_rc

def loadUiClass(path):
    stream = QFile(path)
    stream.open(QFile.ReadOnly)
    try:
        return loadUiType(stream)[0]
    finally:
        stream.close()

Ui_Class = loadUiClass(':/ui_file/test.ui')
</code></pre>

python qt pyqt pyqt4