Search code examples
pythonqtpyside2

Is it possible to choose at runtime to import uic compiled files or dynamically load the ui with QUiLoader()?


As stated in the official documentation there are 2 ways of importing .ui files in your code:

In my project I'm using Option A, but now I'm wondering if it would be possible to choose at a project level Option A or Option B at runtime, because it would avoid having to compile the widgets after each change while in development


Solution

  • In the case of Qt for Python the option is to use loadUiType:

    ui_class, qt_class = loadUiType("filename.ui")
    
    class FooWidget(QFooWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.ui = ui_class()
            self.ui.setupUi(self)