19

I've used PyQt for quite a while, and the entire time I've used it, there has been a pretty consistent programming pattern.

  1. Use Qt Designer to create a .ui file.
  2. Create a python class of the same type as the widget you created in the .ui file.
  3. When initializing the python class, use uic to dynamically load the .ui file onto the class.

Is there any way to do something similar in PySide? I've read through the documentation and examples and the closest thing I could find was a calculator example that pre-rendered the .ui file out to python code, which is the super old way of doing it in PyQt (why bake it to python when you can just parse the ui?)

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • I always figured it was better to remove an entire parsing step from the application startup, and that having the dynamic loadUi was more of a development convenience. – jdi Sep 30 '13 at 20:06
  • @jdi "Better" is subjective. Slightly faster... maybe..., It's not as if the python ui code doesn't have to be parsed, you're merely substituting one type of parsing for another. One of the major benefits of pyqt is the fast iterative dev workflow. PySide adding another step to that is going backwards IMHO. – Brendan Abel Oct 01 '13 at 22:41
  • I agree with your correction. I should have phrased it as "I personally have just found it more straightforward". And I think there is more parsing involved anyways in the dynamic loading. You first have to parse and convert the UI xml -> python code, and then build the objects out of that. – jdi Oct 02 '13 at 02:26

1 Answers1

28

I'm doing exactly that with PySide. :)

You use this https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 (original by Sebastian Wiesner was at https://github.com/lunaryorn/snippets/blob/master/qt4/designer/pyside_dynamic.py but has disappeared) - which overrides PySide.QtUiTools.QUiLoader and supplies a new loadUi() method so that you can do this:

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        loadUi('mainwindow.ui', self)

When you instantiate MyMainWindow, it will have the UI that you designed with the Qt Designer.

If you also need to use custom widgets ("Promote To" in Qt Designer), see this answer: https://stackoverflow.com/a/14877624/532513

Community
  • 1
  • 1
Charl Botha
  • 4,373
  • 34
  • 53