2 How to load a user interface dynamically with PySide.
3 .. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
5 from PySide.QtCore import Slot, QMetaObject
6 from PySide.QtUiTools import QUiLoader
8 class UiLoader(QUiLoader):
10 Subclass :class:`~PySide.QtUiTools.QUiLoader` to create the user interface
13 Unlike :class:`~PySide.QtUiTools.QUiLoader` itself this class does not
14 create a new instance of the top-level widget, but creates the user
15 interface in an existing instance of the top-level class.
17 This mimics the behaviour of :func:`PyQt5.uic.loadUi`.
19 def __init__(self, baseinstance, customWidgets=None):
21 Create a loader for the given ``baseinstance``.
23 The user interface is created in ``baseinstance``, which must be an
24 instance of the top-level class in the user interface to load, or a
27 ``customWidgets`` is a dictionary mapping from class name to class object
28 for widgets that you've promoted in the Qt Designer interface. Usually,
29 this should be done by calling registerCustomWidget on the QUiLoader, but
30 with PySide 1.1.2 on Ubuntu 12.04 x86_64 this causes a segfault.
32 ``parent`` is the parent object of this loader.
34 QUiLoader.__init__(self, baseinstance)
35 self.baseinstance = baseinstance
36 self.customWidgets = customWidgets
38 def createWidget(self, class_name, parent=None, name=''):
40 Function that is called for each widget defined in ui file,
41 overridden here to populate baseinstance instead.
43 if parent is None and self.baseinstance:
44 return self.baseinstance
46 if class_name in self.availableWidgets():
47 # create a new widget for child widgets
48 widget = QUiLoader.createWidget(self, class_name, parent, name)
51 widget = self.customWidgets[class_name](parent)
52 except (TypeError, KeyError) as e:
53 raise Exception('No custom widget ' + class_name + ' found in customWidgets param of UiLoader __init__.')
56 setattr(self.baseinstance, name, widget)
60 def loadUi(uifile, baseinstance=None, customWidgets=None):
62 Dynamically load a user interface from the given ``uifile``.
63 ``uifile`` is a string containing a file name of the UI file to load.
65 If ``baseinstance`` is ``None``, the a new instance of the top-level widget
66 will be created. Otherwise, the user interface is created within the given
67 ``baseinstance``. In this case ``baseinstance`` must be an instance of the
68 top-level widget class in the UI file to load, or a subclass thereof. .
70 ``customWidgets`` is a dictionary mapping from class name to class object
71 for widgets that you've promoted in the Qt Designer interface. Usually,
72 this should be done by calling registerCustomWidget on the QUiLoader, but
73 with PySide 1.1.2 on Ubuntu 12.04 x86_64 this causes a segfault.
75 :method:`~PySide.QtCore.QMetaObject.connectSlotsByName()` is called on the
76 created user interface, so you can implemented your slots according to its
77 conventions in your widget class.
79 Return ``baseinstance``, if ``baseinstance`` is not ``None``. Otherwise
80 return the newly created instance of the user interface.
83 loader = UiLoader(baseinstance, customWidgets)
84 widget = loader.load(uifile)
85 QMetaObject.connectSlotsByName(widget)