-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyside_dynamicLoader.py
143 lines (124 loc) · 6.49 KB
/
pyside_dynamicLoader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
This File is part of bLUe software.
Copyright (C) 2017 Bernard Virot <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Lesser Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
#!/usr/bin/python2
# -*- coding: utf-8 -*-
# Copyright (c) 2011 Sebastian Wiesner <[email protected]>
# Modifications by Charl Botha <[email protected]>
# Modifications by Bernard Virot <bernard [email protected]>
# * customWidgets support (registerCustomWidget() causes segfault in
# pyside 1.1.2 on Ubuntu 12.04 x86_64)
# * workingDirectory support in loadUi
# found this here:
# https://github.com/lunaryorn/snippets/blob/master/qt4/designer/pyside_dynamic.py
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""
How to load a user interface dynamically with PySide.
.. moduleauthor:: Sebastian Wiesner <[email protected]>
"""
import os
import sys
from PySide2.QtCore import Slot, QMetaObject
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QMainWindow, QMessageBox
class UiLoader(QUiLoader):
"""
Subclass :QUiLoader` to create the user interface
Unlike QUiLoader itself this class does not
create a new instance of the top-level widget, but creates the user
interface in an existing instance of the top-level class.
"""
def __init__(self, baseinstance, customWidgets=None):
"""
Create a loader for the given baseinstance.
The user interface is created in ``baseinstance``, which must be an
instance of the top-level class in the user interface to load, or a
subclass thereof.
customWidgets is a dictionary mapping from class name to class object
for widgets that you've promoted in the Qt Designer interface. Usually,
this should be done by calling registerCustomWidget on the QUiLoader, but
with PySide 1.1.2 on Ubuntu 12.04 x86_64 this causes a segfault.
"""
QUiLoader.__init__(self, baseinstance)
self.baseinstance = baseinstance
self.customWidgets = customWidgets
def createWidget(self, class_name, parent=None, name=''):
"""
overridden function.
It is called for each widget defined in the ui file.
"""
if parent is None and self.baseinstance:
# supposed to create the top-level widget, return the base instance
# instead
return self.baseinstance
else:
if class_name in self.availableWidgets() and class_name != 'QLabel':
# create a new widget for child widgets
widget = QUiLoader.createWidget(self, class_name, parent, name)
else:
# if not in the list of availableWidgets, must be a custom widget
# this will raise KeyError if the user has not supplied the
# relevant class_name in the dictionary, or TypeError, if
# customWidgets is None
try:
widget = self.customWidgets[class_name](parent=parent)
except (TypeError, KeyError) as e:
raise Exception('No custom widget ' + class_name + ' found in customWidgets param of UiLoader __init__.')
if self.baseinstance:
# set an attribute for the new child widget on the base
# instance.
setattr(self.baseinstance, name, widget)
return widget
def loadUi(uifile, baseinstance=None, customWidgets=None,
workingDirectory=None):
"""
Dynamically load a user interface from the given ``uifile``.
``uifile`` is a string containing a file name of the UI file to load.
If ``baseinstance`` is ``None``, the a new instance of the top-level widget
will be created. Otherwise, the user interface is created within the given
``baseinstance``. In this case ``baseinstance`` must be an instance of the
top-level widget class in the UI file to load, or a subclass thereof. In
other words, if you've created a ``QMainWindow`` interface in the designer,
``baseinstance`` must be a ``QMainWindow`` or a subclass thereof, too. You
cannot load a ``QMainWindow`` UI file with a plain
:class:`~PySide.QtGui.QWidget` as ``baseinstance``.
``customWidgets`` is a dictionary mapping from class name to class object
for widgets that you've promoted in the Qt Designer interface. Usually,
this should be done by calling registerCustomWidget on the QUiLoader, but
with PySide 1.1.2 on Ubuntu 12.04 x86_64 this causes a segfault.
:method:`~PySide.QtCore.QMetaObject.connectSlotsByName()` is called on the
created user interface, so you can implement your slots according to its
conventions in your widget class.
Return ``baseinstance``, if ``baseinstance`` is not ``None``. Otherwise
return the newly created instance of the user interface.
"""
loader = UiLoader(baseinstance, customWidgets)
if workingDirectory is not None:
loader.setWorkingDirectory(workingDirectory)
widget = loader.load(uifile)
QMetaObject.connectSlotsByName(widget)
return widget