[Python] PyQt: How do I load a ui file from a resource?

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    In general, I load all my ui files via the loadui() method, and this works fine for me. This looks like this:

    #!/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()


    Now I try to load the ui file via loaduiType(), but it doesn't work. I tried with this 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)


    What is the correct and best why to load the ui file with the loadUiType() method?

    Continue reading...

Compartilhe esta Página