-
Notifications
You must be signed in to change notification settings - Fork 256
Add support secondary arguments support to load_ui #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d286429
b9a5d96
1b068a1
dfa8587
174833e
c61b33d
73920a9
c6dc043
ce6fb63
cae6aff
6dad71f
a01dee1
9636bf2
cf6b7d7
0efaa10
1075d02
211db11
e07eaab
1ef25b4
57b7956
a440883
231819d
b443923
41dbb76
9f25323
9c4f7d7
189c0ed
9fb8646
bfd9cae
adf4c2b
0f4ee5a
f24fc29
69bb70f
fe4889c
cd656b7
84bfc84
b17413e
1d363a6
cb6d835
ee30a6d
c231100
b4ed328
7703fda
2de9542
58b7e4e
02bc2ea
049213a
8929b63
ecf2780
4cf7294
7dd4d6c
99b1e99
0ea392a
87c7dae
7bfb97e
3daabe9
55f464f
f9abc92
a545edd
61bcb86
5e5825b
a44a466
cfe625c
7c18457
b3a3cf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,60 +118,184 @@ def _pyside(): | |
| return PySide | ||
|
|
||
|
|
||
| def pyside_load_ui(fname): | ||
| def pyside_load_ui(fname, base_instance=None, custom_widgets=None): | ||
| """Read Qt Designer .ui `fname` | ||
|
|
||
| Args: | ||
| fname (str): Absolute path to .ui file | ||
| base_instance (widget): Optional instance of the Qt base class. | ||
| custom_widgets (widget): ? | ||
|
|
||
| Usage: | ||
| >> from Qt import load_ui | ||
| >> class MyWindow(QtWidgets.QWidget): | ||
| .. fname = 'my_ui.ui' | ||
|
||
| .. self.ui = load_ui(fname) | ||
| .. load_ui(fname, self) | ||
| .. | ||
| >> window = MyWindow() | ||
|
|
||
| """ | ||
|
|
||
| from PySide import QtUiTools | ||
| return QtUiTools.QUiLoader().load(fname) | ||
| Note: | ||
| This function is based on the gist: | ||
| https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 | ||
|
|
||
| """ | ||
|
|
||
| def pyside2_load_ui(fname): | ||
| from PySide import QtUiTools, QtCore | ||
|
|
||
| class UiLoader(QtUiTools.QUiLoader): | ||
|
||
| def __init__(self, base_instance, custom_widgets=None): | ||
| QtUiTools.QUiLoader.__init__(self, base_instance) | ||
| self.base_instance = base_instance | ||
| self.custom_widgets = custom_widgets | ||
|
|
||
| def createWidget(self, class_name, parent=None, name=''): | ||
| if parent is None and self.base_instance: | ||
| return self.base_instance | ||
| else: | ||
| if class_name in self.availableWidgets(): | ||
| widget = QtUiTools.QUiLoader.createWidget(self, | ||
| class_name, | ||
| parent, name) | ||
| else: | ||
| try: | ||
| widget = self.custom_widgets[class_name](parent) | ||
| except (TypeError, KeyError): | ||
| raise Exception('No custom widget ' + | ||
| class_name + | ||
| ' found in custom_widgets' + | ||
| ' param of UiLoader __init__.') | ||
| if self.base_instance: | ||
| setattr(self.base_instance, name, widget) | ||
| return widget | ||
|
|
||
| def loadUi(fname, base_instance=None, custom_widgets=None): | ||
| loader = UiLoader(base_instance, custom_widgets) | ||
| widget = loader.load(fname) | ||
| QtCore.QMetaObject.connectSlotsByName(widget) | ||
| return widget | ||
|
|
||
| return loadUi(fname, base_instance=base_instance, | ||
| custom_widgets=custom_widgets) | ||
|
|
||
|
|
||
| def pyside2_load_ui(fname, base_instance=None, custom_widgets=None): | ||
| """Read Qt Designer .ui `fname` | ||
|
|
||
| Args: | ||
| fname (str): Absolute path to .ui file | ||
| base_instance (widget): Optional instance of the Qt base class. | ||
| custom_widgets (widget): ? | ||
|
|
||
| """ | ||
| Usage: | ||
| >> from Qt import load_ui | ||
| >> class MyWindow(QtWidgets.QWidget): | ||
| .. fname = 'my_ui.ui' | ||
| .. load_ui(fname, self) | ||
| .. | ||
| >> window = MyWindow() | ||
|
|
||
| from PySide2 import QtUiTools | ||
| return QtUiTools.QUiLoader().load(fname) | ||
| Note: | ||
| This function is based on the gist: | ||
| https://gist.github.com/cpbotha/1b42a20c8f3eb9bb7cb8 | ||
|
|
||
| """ | ||
|
|
||
| def pyqt4_load_ui(fname): | ||
| from PySide2 import QtUiTools, QtCore | ||
|
|
||
| class UiLoader(QtUiTools.QUiLoader): | ||
| def __init__(self, base_instance, custom_widgets=None): | ||
| QtUiTools.QUiLoader.__init__(self, base_instance) | ||
| self.base_instance = base_instance | ||
| self.custom_widgets = custom_widgets | ||
|
|
||
| def createWidget(self, class_name, parent=None, name=''): | ||
| if parent is None and self.base_instance: | ||
| return self.base_instance | ||
| else: | ||
| if class_name in self.availableWidgets(): | ||
| widget = QtUiTools.QUiLoader.createWidget(self, | ||
| class_name, | ||
| parent, name) | ||
| else: | ||
| try: | ||
| widget = self.custom_widgets[class_name](parent) | ||
| except (TypeError, KeyError): | ||
| raise Exception('No custom widget ' + | ||
| class_name + | ||
| ' found in custom_widgets' + | ||
| ' param of UiLoader __init__.') | ||
| if self.base_instance: | ||
| setattr(self.base_instance, name, widget) | ||
| return widget | ||
|
|
||
| def loadUi(fname, base_instance=None, custom_widgets=None): | ||
| loader = UiLoader(base_instance, custom_widgets) | ||
| widget = loader.load(fname) | ||
| QtCore.QMetaObject.connectSlotsByName(widget) | ||
| return widget | ||
|
|
||
| return loadUi(fname, base_instance=base_instance, | ||
| custom_widgets=custom_widgets) | ||
|
|
||
|
|
||
| def pyqt4_load_ui(fname, base_instance=None, custom_widgets=None): | ||
| """Read Qt Designer .ui `fname` | ||
|
|
||
| Args: | ||
| fname (str): Absolute path to .ui file | ||
| base_instance (widget): Optional instance of the Qt base class. | ||
| custom_widgets (widget): ? | ||
|
|
||
| Usage: | ||
| >> from Qt import load_ui | ||
| >> class MyWindow(QtWidgets.QWidget): | ||
| .. fname = 'my_ui.ui' | ||
| .. load_ui(fname, self) | ||
| .. | ||
| >> window = MyWindow() | ||
|
|
||
| """ | ||
|
|
||
| from PyQt4 import uic | ||
| return uic.loadUi(fname) | ||
|
|
||
| if isinstance(base_instance, type(None)) and \ | ||
| isinstance(custom_widgets, type(None)): | ||
| return uic.loadUi(fname) | ||
| elif not isinstance(base_instance, type(None)) and \ | ||
| isinstance(custom_widgets, type(None)): | ||
| return uic.loadUi(fname, base_instance) | ||
| else: | ||
| return uic.loadUi(fname, base_instance, custom_widgets) | ||
|
|
||
|
|
||
| def pyqt5_load_ui(fname): | ||
| def pyqt5_load_ui(fname, base_instance=None, custom_widgets=None): | ||
| """Read Qt Designer .ui `fname` | ||
|
|
||
| Args: | ||
| fname (str): Absolute path to .ui file | ||
| base_instance (widget): Optional instance of the Qt base class. | ||
| custom_widgets (widget): ? | ||
|
|
||
| Usage: | ||
| >> from Qt import load_ui | ||
| >> class MyWindow(QtWidgets.QWidget): | ||
| .. fname = 'my_ui.ui' | ||
| .. load_ui(fname, self) | ||
| .. | ||
| >> window = MyWindow() | ||
|
|
||
| """ | ||
|
|
||
| from PyQt5 import uic | ||
| return uic.loadUi(fname) | ||
|
|
||
| if isinstance(base_instance, type(None)) and \ | ||
| isinstance(custom_widgets, type(None)): | ||
| return uic.loadUi(fname) | ||
| elif not isinstance(base_instance, type(None)) and \ | ||
| isinstance(custom_widgets, type(None)): | ||
| return uic.loadUi(fname, base_instance) | ||
| else: | ||
| return uic.loadUi(fname, base_instance, custom_widgets) | ||
|
|
||
|
|
||
| def _log(text, verbose): | ||
|
|
@@ -189,18 +313,18 @@ def _init(): | |
| this has executed. | ||
|
|
||
| """ | ||
|
|
||
| preferred = os.getenv("QT_PREFERRED_BINDING") | ||
| verbose = os.getenv("QT_VERBOSE") is not None | ||
| bindings = (_pyside2, _pyqt5, _pyside, _pyqt4) | ||
|
|
||
| if preferred: | ||
|
|
||
| # Internal flag (used in installer) | ||
| if preferred == "None": | ||
| sys.modules[__name__].__wrapper_version__ = __version__ | ||
| return | ||
|
|
||
| preferred = preferred.split(os.pathsep) | ||
| available = { | ||
| "PySide2": _pyside2, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be.